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

Type annotations in initializers and lambda bodies not written to class file

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 9
    • 8
    • tools
    • b93
    • Verified

    Description

      Described at http://mail.openjdk.java.net/pipermail/jigsaw-dev/2015-September/004478.html

      Take a simple annotation type, Foo.java:

      import java.lang.annotation.*;
      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.TYPE_USE)
      public @interface Foo {
          java.math.RoundingMode rounding();
      }

      Write an @Foo annotation in the constructor of a simple class, Test.java:

      public class Test {
        Test() {
          @Foo(rounding=java.math.RoundingMode.UNNECESSARY)
          int x = 0;
        }
      }

      Great, javap reveals a RuntimeVisibleTypeAnnotations attribute in the Code attribute of Test::<init>.

      Now, remove the "Test()" header so that the class just has an instance initializer:

      public class Test {
        {
          @Foo(rounding=java.math.RoundingMode.UNNECESSARY)
          int x = 0;
        }
      }

      No RuntimeVisibleTypeAnnotations attribute appears. Same for a static initializer.

      The problem appears connected with the local variable declaration itself. That is, there's no attribute from this:
        {
          @Foo(rounding=java.math.RoundingMode.UNNECESSARY) List x = new ArrayList();
        }
      but there is an attribute for this:
        {
          List x = new @Foo(rounding=java.math.RoundingMode.UNNECESSARY) ArrayList();
        }

      It also appears that type annotations on types in a lambda body -- () -> new ArrayList<@Foo String>() -- are not preserved in an attribute of the synthetic method generated for the lambda expression. While the names of the lambda expression's formal parameters should not be preserved on the synthetic method (when javac -parameters is used), appropriate annotations on the types of the lambda expression's formal parameters or on types in the lambda expression's body should be preserved.

      Attachments

        Issue Links

          Activity

            People

              sadayapalam Srikanth Adayapalam (Inactive)
              abuckley Alex Buckley
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: