JDK-8223116 : Class#getDeclaredMethods and AccessibleObject#isAnnotationPresent print wrong values
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 8,11,13
  • Priority: P4
  • Status: Resolved
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2019-04-26
  • Updated: 2019-05-02
  • Resolved: 2019-04-30
Related Reports
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
my code is follow:
public interface IFly {

    String fly1();

    Object fly2();
}

public class FlyImpl implements IFly {
    @Override
    @FlyEnable
    public String fly1() {
        return null;
    }

    @Override
    @FlyEnable
    public String fly2() {
        return null;
    }
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface  FlyEnable {
}

public class Main {
    public static void main(String[] args) {
        //out 3, but i want 2
        //com.zj.IFly.fly2 should not be count
        System.out.println(FlyImpl.class.getDeclaredMethods().length);
        //is this is a bug? if this is not a bug, what should i do, then it out 2.

        for(Method method: FlyImpl.class.getDeclaredMethods()){
            if( method.isAnnotationPresent( FlyEnable.class)){
                // this out 3 times, but i want 2
                // com.zj.IFly.fly2 call isAnnotationPresent( FlyEnable.class) should not return true
                System.out.println("method:" + method.getName() + " is annotated by FlyEnable");
                //is this is a bug? if this is not a bug, what should i do, com.zj.IFly.fly2 call isAnnotationPresent( FlyEnable.class) return false
            }
        }
    }
}

i'm not good at English, 
If I say something wrong, don't be angry.

REGRESSION : Last worked in version 8u202


FREQUENCY : always



Comments
Also note bridge methods can be screened out using Method.isBridge: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/reflect/Method.html#isBridge() Annotations were copied onto bridge methods by javac since the fix for JDK-6695379 in JDK 8.
02-05-2019

A bridge method has been created by the compiler in FlyImpl because the interface declares fly2() returning Object and FlyImpl declares fly2() returning String. Correcting the implementation to return Object will remove the necessity for a bridge method.
30-04-2019

Output in eclipse against JDK 11 == 3 method:fly1 is annotated by FlyEnable method:fly2 is annotated by FlyEnable == Output in command prompt against 8u,11,13 ea b17 == 3 method:fly2 is annotated by FlyEnable method:fly2 is annotated by FlyEnable method:fly1 is annotated by FlyEnable == Output is not consistent. This is not a regression, removed regression label. There are 2 issues, as i understand from the bug report Issue 1: FlyImpl.class.getDeclaredMethods().length should print 2, where as eclipse and java both print 3. Issue 2: method.isAnnotationPresent( FlyEnable.class) should not print "method:fly2 is annotated by FlyEnable" twice. Eclipse handles it but java running from command prompt prints the same line again.
30-04-2019