Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8152529

Origin generic method which becomes a default method (once overridden) looses information about all annotations

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.8.0_74"
      Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
      Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      If you override a method from with a generic type an interface with a concrete type (again in an interface) - you will still receive the 'generic method' via reflection (that is fine). The bug is, that all annotations on that method are gone. This did not happen with java 7.

      REGRESSION. Last worked in version 7u80

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1) Create an interface with a generic type and add a simple method to it where you use the generic type.
      2) Add a runtime annotation to the method, like jax-rs @GET.
      3) Create a second interface which extends the prior one and declare a concrete type.
      4) Override the method from the first interface with the concrete type and also put a runtime annotation on it.
      5) try to read all annotations from all methods via reflection from the second interface - you will find two methods, but only the method with the concrete type will have an annotation (if you try to read them via reflection).

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Both methods should have the annotation fetched via reflection
      ACTUAL -
      Only the method with the concrete type has an annotation according to the reflection method.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.lang.reflect.Method;
      import java.util.Arrays;

      import javax.ws.rs.GET;

      import org.junit.Assert;
      import org.junit.Test;

      public class TestAnnotations {

          interface A<T> {

              @GET
              void methodA(T param);
          }

          interface B extends A<Integer> {

              @GET
              @Override
              void methodA(Integer param);

          }

          @Test
          public void showAnnotations() {
              for (Method method : B.class.getMethods()) {
                  System.out.println(method + " " + Arrays.toString(method.getAnnotations()));
                  Assert.assertEquals(1, method.getAnnotations().length);
              }
          }
      }

      ---------- END SOURCE ----------

        1. JI9032471.java
          0.7 kB
          Pallavi Sonal

            robm Robert Mckenna
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: