views:

140

answers:

2

I want to verify binary compatibility between 2 JARs.

Following the suggestions in this answer I used jboss tattletale but it can find only missing classes.

How can I find if there are missing methods? Is it possible at all?

E.g.

"Depends - on" class Foo depends on Bar (like many other middle class workers)

import org.overlyusedclassnames.Bar

public class Foo{
    public void someMethod(){
         Bar tender = new Bar();
         tender.getJohnnyRedLabel();
         tender.getJohnnyBlueLabel(); //this method is new in the Bar class
    }
}

"Compile time" class

package org.overlyusedclassnames;

/** 
 * @Since 1992
 * Changes: added blue and gold Johnny Walker labels
 */

public class Bar {
    public Drink getJohnnyRedLabel(){
         return new JohnyWalkerFactory.get(RedLabel.class);
    }

    public Drink getJohnnyBlackLabel(){
         return new JohnyWalkerFactory.get(BlackLabel.class);
    }

    public Drink getJohnnyGoldLabel(){
         return new JohnyWalkerFactory.get(GoldLabel.class);
    }

    public Drink getJohnnyBlueLabel(){
         return new JohnyWalkerFactory.get(BlueLabel.class);
    }

}

Now imagine an old Bar jar is accedently replacing the compiled time bar:

"Runtime time" class

package org.overlyusedclassnames;

/** 
 * @Since 1909
 * Changes: added red and black Johnny Walker labels
 */

public class Bar {
    public Drink getJohnnyRedLabel(){
         return new JohnyWalkerFactory.get(RedLabel.class);
    }

    public Drink getJohnnyBlackLabel(){
         return new JohnyWalkerFactory.get(BlackLabel.class);
    }
}

Is there a way to identify the missing method without running it and getting a NoSuchMethodError?


Disclaimer: This is a major rephrasing of my own related question, which is undeletable. I chose asking a new question because the rephrasing will render the current 2 answers as quite unrelated to the topic.

+1  A: 

There is a tool by the name of Animal Sniffer that allows you to extract a signature for an API. Then it can statically verify that users of the API stick to the signature, and it can statically verify that implementors of the API have everything implemented. I think this would solve your problem nicely.

You can download the jar for Animal Sniffer from the codehaus maven repository: http://repository.codehaus.org/org/codehaus/mojo/animal-sniffer/

Michael Donohue
@Michael - did you find a download link? where does it hide?
Ehrann Mehdan
The tool is written by Maven folks, so I think they just use various maven repositories, which is easy in Maven. Not so easy in 'ant' unfortunately.
Michael Donohue
I haven't tested it yet, but this is the best answer so far, I'll add a comment once I give it a go.
Ehrann Mehdan
A: 

Do you need a to check a specific class or a generic tool for comparing jar's? If it's for 1 class, simply load the class in a custom class loader, check the methods signature by using reflection and that's it. If you need it for many JAR's/Classes this will be too much work.

Shlomo Schwarcz
@Shlomo - Thanks, I need it for many classes in many JARs
Ehrann Mehdan