langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java
changeset 45500 365640343c55
parent 43269 12f989542165
child 45504 ea7475564d07
equal deleted inserted replaced
45499:16e055f785ef 45500:365640343c55
    40 import com.sun.tools.javac.util.*;
    40 import com.sun.tools.javac.util.*;
    41 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
    41 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
    42 import com.sun.tools.javac.util.List;
    42 import com.sun.tools.javac.util.List;
    43 
    43 
    44 import javax.tools.JavaFileObject;
    44 import javax.tools.JavaFileObject;
       
    45 
    45 import java.util.*;
    46 import java.util.*;
    46 
    47 
    47 import static com.sun.tools.javac.code.Flags.SYNTHETIC;
    48 import static com.sun.tools.javac.code.Flags.SYNTHETIC;
    48 import static com.sun.tools.javac.code.Kinds.Kind.MDL;
    49 import static com.sun.tools.javac.code.Kinds.Kind.MDL;
    49 import static com.sun.tools.javac.code.Kinds.Kind.MTH;
    50 import static com.sun.tools.javac.code.Kinds.Kind.MTH;
    54 import static com.sun.tools.javac.code.TypeTag.CLASS;
    55 import static com.sun.tools.javac.code.TypeTag.CLASS;
    55 import static com.sun.tools.javac.tree.JCTree.Tag.ANNOTATION;
    56 import static com.sun.tools.javac.tree.JCTree.Tag.ANNOTATION;
    56 import static com.sun.tools.javac.tree.JCTree.Tag.ASSIGN;
    57 import static com.sun.tools.javac.tree.JCTree.Tag.ASSIGN;
    57 import static com.sun.tools.javac.tree.JCTree.Tag.IDENT;
    58 import static com.sun.tools.javac.tree.JCTree.Tag.IDENT;
    58 import static com.sun.tools.javac.tree.JCTree.Tag.NEWARRAY;
    59 import static com.sun.tools.javac.tree.JCTree.Tag.NEWARRAY;
       
    60 
    59 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
    61 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
       
    62 
    60 
    63 
    61 /** Enter annotations onto symbols and types (and trees).
    64 /** Enter annotations onto symbols and types (and trees).
    62  *
    65  *
    63  *  This is also a pseudo stage in the compiler taking care of scheduling when annotations are
    66  *  This is also a pseudo stage in the compiler taking care of scheduling when annotations are
    64  *  entered.
    67  *  entered.
   563                 log.error(tree.pos(), "annotation.not.valid.for.type", expectedElementType);
   566                 log.error(tree.pos(), "annotation.not.valid.for.type", expectedElementType);
   564             attributeAnnotation((JCAnnotation)tree, syms.errType, env);
   567             attributeAnnotation((JCAnnotation)tree, syms.errType, env);
   565             return new Attribute.Error(((JCAnnotation)tree).annotationType.type);
   568             return new Attribute.Error(((JCAnnotation)tree).annotationType.type);
   566         }
   569         }
   567 
   570 
       
   571         MemberEnter.InitTreeVisitor initTreeVisitor = new MemberEnter.InitTreeVisitor() {
       
   572             // the methods below are added to allow class literals on top of constant expressions
       
   573             @Override
       
   574             public void visitTypeIdent(JCPrimitiveTypeTree that) {}
       
   575 
       
   576             @Override
       
   577             public void visitTypeArray(JCArrayTypeTree that) {}
       
   578         };
       
   579         tree.accept(initTreeVisitor);
       
   580         if (!initTreeVisitor.result) {
       
   581             log.error(tree.pos(), Errors.ExpressionNotAllowableAsAnnotationValue);
       
   582             return new Attribute.Error(syms.errType);
       
   583         }
       
   584 
   568         if (expectedElementType.isPrimitive() ||
   585         if (expectedElementType.isPrimitive() ||
   569                 (types.isSameType(expectedElementType, syms.stringType) && !expectedElementType.hasTag(TypeTag.ERROR))) {
   586                 (types.isSameType(expectedElementType, syms.stringType) && !expectedElementType.hasTag(TypeTag.ERROR))) {
   570             return getAnnotationPrimitiveValue(expectedElementType, tree, env);
   587             return getAnnotationPrimitiveValue(expectedElementType, tree, env);
   571         }
   588         }
   572 
   589 
   612             } else {
   629             } else {
   613                 return new Attribute.Error(result.getOriginalType());
   630                 return new Attribute.Error(result.getOriginalType());
   614             }
   631             }
   615         }
   632         }
   616 
   633 
   617         // Class literals look like field accesses of a field named class
       
   618         // at the tree level
       
   619         if (TreeInfo.name(tree) != names._class) {
       
   620             log.error(tree.pos(), "annotation.value.must.be.class.literal");
       
   621             return new Attribute.Error(syms.errType);
       
   622         }
       
   623         return new Attribute.Class(types,
   634         return new Attribute.Class(types,
   624                 (((JCFieldAccess) tree).selected).type);
   635                 (((JCFieldAccess) tree).selected).type);
   625     }
   636     }
   626 
   637 
   627     private Attribute getAnnotationPrimitiveValue(Type expectedElementType, JCExpression tree, Env<AttrContext> env) {
   638     private Attribute getAnnotationPrimitiveValue(Type expectedElementType, JCExpression tree, Env<AttrContext> env) {