JDK-8078571 : java.lang.reflect.method.isAnnotationPresent backward compatible at jdk-7u80
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 7u80
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2015-04-22
  • Updated: 2015-07-01
  • Resolved: 2015-06-16
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux XXX 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
Microsoft Windows [Version 6.1.7601]


A DESCRIPTION OF THE PROBLEM :
The subclass implements abstract method of abstract superclass and adds a method annotation to that method. At jdk 1.7.0_80 method.isAnnotationPresent returns TRUE for both superclass's and subclass's methods while at jdk 1.7.0_79 I've got TRUE for subclass's method and FALSE for superclass's method.

REGRESSION.  Last worked in version 7u79

ADDITIONAL REGRESSION INFORMATION: 
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create custom method annotation with @Retention(RetentionPolicy.RUNTIME).
2. Create abstract superclass with abstract method.
3. Inherite superclass, implement method and add custom annotation for that method at subclass.
4. Get method array by calling Subclass.class.getDeclaredMethods().
5. Call method.isAnnotationPresent() for custom annotation.
6. Get different results for jdk 1.7.0_79 and jdk 1.7.0_80.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
jdk 1.7.0_79 and prior output:

subclass method is annotated -  TRUE
superclass method is annotated - FALSE
ACTUAL -
jdk 1.7.0_80 output:

subclass method is annotated -  TRUE
superclass method is annotated - TRUE

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
Custom annotation class:

import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
    String value() default "";
}

Superclass:

public abstract class BaseClass {
     public abstract  Number getId();
}

Subclass:

public class InhClass extends BaseClass {
    @Override
    @MyAnnotation("id")
    public Integer getId() {
        return 0;
    }
}

Main class to check method.isAnnotationPresent :

import java.lang.reflect.Method;

public final class Main {

    public static void main(String[] args) {

        try {

            Method[] methods = InhClass.class.getDeclaredMethods();

            for (Method method : methods) {
                System.out.println(method.toString() + " annotated "  + method.isAnnotationPresent(MyAnnotation.class));
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}
---------- END SOURCE ----------


Comments
The JDK-8029563 has description of changes triggered the behavior change - it was intentional and backported to JDK7 (JDK-6695379). Closing as a duplicate of JDK-8029563.
16-06-2015

Update from the submitter: ------------------------------------------------------------------------------- Hi ........! Thank you for your response. I can confirm, that your jar file produces true/true for both JDK. But the problem is that you use a single JDK for building your project. If you use JDK 7u79 for jar building you will get true/false for both java execution and if you use 7u80 for building jar you will get true/true. Thank you for help, ....... -------------------------------------------------------------------------------
30-04-2015

1. Run the attached program (JavaApplication269.zip). 2. Checked this for JDK 7u76, 7u79, 7u80, and 8u45. ----------------------------------------------------------------------- 7u76: OK 7u79: OK 7u80: OK 8u45: OK -------------------------------------------------------------------------- Output with 7u79: public java.lang.Integer javaapplication269.InhClass.getId() annotated true public java.lang.Number javaapplication269.InhClass.getId() annotated true --------------------------------------------------------------------------------- Output with 7u79: public java.lang.Integer javaapplication269.InhClass.getId() annotated true public java.lang.Number javaapplication269.InhClass.getId() annotated true ----------------------------------------------------------------------------------------- Conclusion: Not able to reproduce the issue with any of the above mentioned JDKs. Moving this to up for further evaluation, while writing back to the submitter with the above information for clarification about this issue.
24-04-2015