JDK-8080370 : Method annotation added in abstract super method
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 8u45
  • Priority: P4
  • Status: Closed
  • Resolution: Incomplete
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2015-04-20
  • Updated: 2021-07-28
  • Resolved: 2015-05-14
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
When I scan my compiled code to find a specific annotation I started to find duplicates when using Java8 in case the method annotated is the implementation of an abstract method where the input argument is included using generics.
Here an example.

My Annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExampleAnnotation {}


The abstract class:
public abstract class AbstractExample<T extends BeanInterface> {
    public abstract void exampleMethod(T bean);
}

The Implementation:
public class ImplementationExample extends AbstractExample<BeanImplementation>{
  @Override
  @ExampleAnnotation
  public void exampleMethod(BeanImplementation bean) {          
  }
}
where BeanImplementation is an implementation of BeanInterface. If I compile the code using Java 8u45 and I list which methods of the ImplementationExample class have the annotation ExampleAnnotation using the following piece of code:

    Class<ImplementationExample> cls = ImplementationExample.class;
    List<Method> methods = new ArrayList<Method>();
    methods.addAll(Arrays.asList(cls.getDeclaredMethods()));

    for (Method method : methods) {
      System.out.println("method: " + method.getName() + " annotation: " + method.getAnnotation(ExampleAnnotation.class));
    }
Then I get that both these methods:

ImplementationExample.exampleMethod(BeanImplementation) ImplementationExample.exampleMethod(BeanInterface)

have the annotation.

If I use Java 7 or the Java 8 Eclipse Compiler then only the first method is annotated and this should be the correct behavior.

Is this a bug in the java 8 compiler?


REPRODUCIBILITY :
This bug can be reproduced always.


Comments
The observed difference in behavior is likely a consequence of JDK-6695379.
15-05-2015