langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java
changeset 45500 365640343c55
parent 43269 12f989542165
child 45504 ea7475564d07
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java	Fri Jun 09 21:34:47 2017 +0000
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java	Tue Jun 13 11:21:09 2017 -0700
@@ -42,6 +42,7 @@
 import com.sun.tools.javac.util.List;
 
 import javax.tools.JavaFileObject;
+
 import java.util.*;
 
 import static com.sun.tools.javac.code.Flags.SYNTHETIC;
@@ -56,8 +57,10 @@
 import static com.sun.tools.javac.tree.JCTree.Tag.ASSIGN;
 import static com.sun.tools.javac.tree.JCTree.Tag.IDENT;
 import static com.sun.tools.javac.tree.JCTree.Tag.NEWARRAY;
+
 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
 
+
 /** Enter annotations onto symbols and types (and trees).
  *
  *  This is also a pseudo stage in the compiler taking care of scheduling when annotations are
@@ -565,6 +568,20 @@
             return new Attribute.Error(((JCAnnotation)tree).annotationType.type);
         }
 
+        MemberEnter.InitTreeVisitor initTreeVisitor = new MemberEnter.InitTreeVisitor() {
+            // the methods below are added to allow class literals on top of constant expressions
+            @Override
+            public void visitTypeIdent(JCPrimitiveTypeTree that) {}
+
+            @Override
+            public void visitTypeArray(JCArrayTypeTree that) {}
+        };
+        tree.accept(initTreeVisitor);
+        if (!initTreeVisitor.result) {
+            log.error(tree.pos(), Errors.ExpressionNotAllowableAsAnnotationValue);
+            return new Attribute.Error(syms.errType);
+        }
+
         if (expectedElementType.isPrimitive() ||
                 (types.isSameType(expectedElementType, syms.stringType) && !expectedElementType.hasTag(TypeTag.ERROR))) {
             return getAnnotationPrimitiveValue(expectedElementType, tree, env);
@@ -614,12 +631,6 @@
             }
         }
 
-        // Class literals look like field accesses of a field named class
-        // at the tree level
-        if (TreeInfo.name(tree) != names._class) {
-            log.error(tree.pos(), "annotation.value.must.be.class.literal");
-            return new Attribute.Error(syms.errType);
-        }
         return new Attribute.Class(types,
                 (((JCFieldAccess) tree).selected).type);
     }