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

Compiler generates wrong code if Annotation TYPE_USE on anonymous class

XMLWordPrintable

    • x86_64
    • windows_7

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      If I use an Annotation with ElementType.TYPE_USE on an anonymous class,
      this class becomes unusable.
      For example, initialization of fields results in a NoSuchFieldError at runtime.

      Basically, this bug is described in an OpenJDK-bug:
      https://bugs.openjdk.java.net/browse/JDK-8059531



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Declare an Annotation type "A" with ElementType.TYPE_USE
      2. Write an expression that instantiates an anonymous class with at least one field, and annotates it with "A"
      3. Run the program

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The anonymous class is instantiated and is fully usable.
      ACTUAL -
      The initializer of the class fails with a NoSuchFieldError

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package com.neppert;

      import org.junit.Test;

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

      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.TYPE_USE)
      @interface Alias {
      String value();
      }

      public class TestAnnotationTypeUse {


      public static void main(String[] argc) {
      Object annotatedObject = new @Alias("Test") Object() {
      public final Object member = "Hello";
      };
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      No workaround possible

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: