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

Same annotation for method and return type not possible

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8, 9
    • tools
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_60"
      Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
      Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      It is currently not possible to have an annotation present on both a method and that method's return type, although since Java 8 there should be a distinction between the two (according to http://docs.oracle.com/javase/8/docs/api/java/lang/annotation/ElementType.html, ElementType.TYPE_USE and https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.11).

      Note that the JLS is ambiguous on this if no type parameters are present. According to https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4, an annotation can both be the first part of Result or the last part of MethodModifiers. Strangely enough, if type parameters are present, the annotation is still seen as part of MethodModifiers and not of Result.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try to compile the attached source code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Code compiles successfully. The methods and return types are all annotated with @ThreadSafe.
      ACTUAL -
      Code does not compile because of duplicate non-repeatable annotation.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      D:\AnnotationTest.java:12: error: ThreadSafe is not a repeatable annotation type
          public @ThreadSafe List<String> getList() {
                 ^
      D:\AnnotationTest.java:17: error: ThreadSafe is not a repeatable annotation type
          public <T> @ThreadSafe List<T> getGenericList() {
                     ^
      2 errors

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.lang.annotation.*;
      import java.util.*;

      @Target({ ElementType.METHOD, ElementType.TYPE_USE })
      @interface ThreadSafe {
      }

      class AnnotationTest {
          // method annotation indicates the method is thread safe
          // return type annotation indicates the return type is thread safe
          @ThreadSafe
          public @ThreadSafe List<String> getList() {
              return Collections.emptyList();
          }

          @ThreadSafe
          public <T> @ThreadSafe List<T> getGenericList() {
              return Collections.emptyList();
          }
      }

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

            sadayapalam Srikanth Adayapalam (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: