dimanche 13 septembre 2015

Method.getAnnotations() method giving different outputs for java 1.7.0_79 and java 1.7.0_80 versions

I am getting different output for the same code while using java 1.7.0_79 and 1.7.0_80 versions for compilation and running. The test code snippet looks like this :

interface Constraint<T> extends Serializable {

    T getValue();
}


@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@interface NonZero {


}

public class ReflectionTest {

    @SuppressWarnings("serial")
    public static void main(String[] args) {

        Constraint<Integer> c = new Constraint<Integer>() {

            @Override
            @NonZero
            public Integer getValue() {
                return null;
            }
        };

        for(Method m : c.getClass().getDeclaredMethods()) {
            System.out.println("Method :: " + m.toGenericString());

            if(m.getAnnotations().length == 0){
                System.out.println("No annotations detected");
            } else {
                for(Annotation o: m.getAnnotations()) {
                    System.out.println("Annotation :: " + o);
                }
            }
        }
    }
}

The output for java 1.7.0_79 is :

Method :: public java.lang.Integer ReflectionTest$1.getValue()
Annotation :: @NonZero() 
Method :: public java.lang.Object
ReflectionTest$1.getValue() No annotations detected

And the output for 1.7.0_80 is :

Method :: public java.lang.Integer ReflectionTest$1.getValue()
Annotation :: @NonZero()
Method :: public java.lang.Object ReflectionTest$1.getValue()
Annotation :: @NonZero()

In the code compiled with jdk-1.7.0_80 version, annotation is applied to the parent method while this was not the case in the earlier versions. Why is this difference?





Aucun commentaire:

Enregistrer un commentaire