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

getAnnotations crashes with ClassCastException

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      > java -version
      openjdk version "1.8.0_191"
      OpenJDK Runtime Environment (IcedTea 3.10.0) (build 1.8.0_191-b12 suse-1.1-x86_64)
      OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

      > /usr/lib64/jvm/java-11/bin/java -version
      openjdk version "11.0.1" 2018-10-16
      OpenJDK Runtime Environment (build 11.0.1+13-suse-3.1-x8664)
      OpenJDK 64-Bit Server VM (build 11.0.1+13-suse-3.1-x8664, mixed mode)


      A DESCRIPTION OF THE PROBLEM :
      If a field with a generic type is annotated with annotation with a parameter of type Class<?> and the value of a non-static inner class, getAnnotations crashes.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and execute code provided

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Should not crash. Should print the only annotation.
      ACTUAL -
      ClassCastException

      ---------- BEGIN SOURCE ----------

      import java.lang.annotation.ElementType;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.annotation.Target;
      import java.util.Arrays;


      @Retention( RetentionPolicy.RUNTIME )
      @Target( ElementType.FIELD )
      @interface A {
          Class<?> clz();
      }

      public class AnnBug<T> {
          @A( clz = Inner.class )
          public T f;

          class Inner {}

          public static void main( String[] args ) throws NoSuchFieldException {
              System.out.println( Arrays.toString( AnnBug.class.getField( "f" ).getAnnotations() ) );
          }
      }

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

      FREQUENCY : always


            pmangal Priyanka Mangal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: