langtools/src/share/classes/com/sun/tools/javac/comp/Annotate.java
changeset 21015 f3bec12a63e7
parent 20599 5201f7144a3c
child 21018 95d225149128
equal deleted inserted replaced
21014:57913337d634 21015:f3bec12a63e7
    35 import com.sun.tools.javac.tree.JCTree.*;
    35 import com.sun.tools.javac.tree.JCTree.*;
    36 
    36 
    37 import static com.sun.tools.javac.code.TypeTag.ARRAY;
    37 import static com.sun.tools.javac.code.TypeTag.ARRAY;
    38 import static com.sun.tools.javac.code.TypeTag.CLASS;
    38 import static com.sun.tools.javac.code.TypeTag.CLASS;
    39 import static com.sun.tools.javac.tree.JCTree.Tag.*;
    39 import static com.sun.tools.javac.tree.JCTree.Tag.*;
       
    40 import javax.lang.model.type.ErrorType;
    40 
    41 
    41 /** Enter annotations on symbols.  Annotations accumulate in a queue,
    42 /** Enter annotations on symbols.  Annotations accumulate in a queue,
    42  *  which is processed at the top level of any set of recursive calls
    43  *  which is processed at the top level of any set of recursive calls
    43  *  requesting it be processed.
    44  *  requesting it be processed.
    44  *
    45  *
   308     }
   309     }
   309 
   310 
   310     Attribute enterAttributeValue(Type expected,
   311     Attribute enterAttributeValue(Type expected,
   311                                   JCExpression tree,
   312                                   JCExpression tree,
   312                                   Env<AttrContext> env) {
   313                                   Env<AttrContext> env) {
   313         Type original = expected;
       
   314         //first, try completing the attribution value sym - if a completion
   314         //first, try completing the attribution value sym - if a completion
   315         //error is thrown, we should recover gracefully, and display an
   315         //error is thrown, we should recover gracefully, and display an
   316         //ordinary resolution diagnostic.
   316         //ordinary resolution diagnostic.
   317         try {
   317         try {
   318             expected.tsym.complete();
   318             expected.tsym.complete();
   349             for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
   349             for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
   350                 enterAttributeValue(syms.errType,
   350                 enterAttributeValue(syms.errType,
   351                                     l.head,
   351                                     l.head,
   352                                     env);
   352                                     env);
   353             }
   353             }
   354             return new Attribute.Error(original);
   354             return new Attribute.Error(syms.errType);
   355         }
   355         }
   356         if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
   356         if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
   357             if (tree.hasTag(ANNOTATION)) {
   357             if (tree.hasTag(ANNOTATION)) {
   358                 return enterAnnotation((JCAnnotation)tree, expected, env);
   358                 return enterAnnotation((JCAnnotation)tree, expected, env);
   359             } else {
   359             } else {
   363         }
   363         }
   364         if (tree.hasTag(ANNOTATION)) { //error recovery
   364         if (tree.hasTag(ANNOTATION)) { //error recovery
   365             if (!expected.isErroneous())
   365             if (!expected.isErroneous())
   366                 log.error(tree.pos(), "annotation.not.valid.for.type", expected);
   366                 log.error(tree.pos(), "annotation.not.valid.for.type", expected);
   367             enterAnnotation((JCAnnotation)tree, syms.errType, env);
   367             enterAnnotation((JCAnnotation)tree, syms.errType, env);
   368             return new Attribute.Error(original);
   368             return new Attribute.Error(((JCAnnotation)tree).annotationType.type);
   369         }
   369         }
   370         if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) {
   370         if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) {
   371             Type result = attr.attribExpr(tree, env, expected);
   371             Type result = attr.attribExpr(tree, env, expected);
   372             if (result.isErroneous())
   372             if (result.isErroneous())
   373                 return new Attribute.Error(expected);
   373                 return new Attribute.Error(result.getOriginalType());
   374             if (result.constValue() == null) {
   374             if (result.constValue() == null) {
   375                 log.error(tree.pos(), "attribute.value.must.be.constant");
   375                 log.error(tree.pos(), "attribute.value.must.be.constant");
   376                 return new Attribute.Error(expected);
   376                 return new Attribute.Error(expected);
   377             }
   377             }
   378             result = cfolder.coerce(result, expected);
   378             result = cfolder.coerce(result, expected);
   379             return new Attribute.Constant(expected, result.constValue());
   379             return new Attribute.Constant(expected, result.constValue());
   380         }
   380         }
   381         if (expected.tsym == syms.classType.tsym) {
   381         if (expected.tsym == syms.classType.tsym) {
   382             Type result = attr.attribExpr(tree, env, expected);
   382             Type result = attr.attribExpr(tree, env, expected);
   383             if (result.isErroneous()) {
   383             if (result.isErroneous()) {
   384                 // Does it look like a class literal?
   384                 // Does it look like an unresolved class literal?
   385                 if (TreeInfo.name(tree) == names._class) {
   385                 if (TreeInfo.name(tree) == names._class &&
       
   386                     ((JCFieldAccess) tree).selected.type.isErroneous()) {
   386                     Name n = (((JCFieldAccess) tree).selected).type.tsym.flatName();
   387                     Name n = (((JCFieldAccess) tree).selected).type.tsym.flatName();
   387                     return new Attribute.UnresolvedClass(expected,
   388                     return new Attribute.UnresolvedClass(expected,
   388                             types.createErrorType(n,
   389                             types.createErrorType(n,
   389                                     syms.unknownSymbol, syms.classType));
   390                                     syms.unknownSymbol, syms.classType));
   390                 } else {
   391                 } else {
   391                     return new Attribute.Error(expected);
   392                     return new Attribute.Error(result.getOriginalType());
   392                 }
   393                 }
   393             }
   394             }
   394 
   395 
   395             // Class literals look like field accesses of a field named class
   396             // Class literals look like field accesses of a field named class
   396             // at the tree level
   397             // at the tree level
   397             if (TreeInfo.name(tree) != names._class) {
   398             if (TreeInfo.name(tree) != names._class) {
   398                 log.error(tree.pos(), "annotation.value.must.be.class.literal");
   399                 log.error(tree.pos(), "annotation.value.must.be.class.literal");
   399                 return new Attribute.Error(expected);
   400                 return new Attribute.Error(syms.errType);
   400             }
   401             }
   401             return new Attribute.Class(types,
   402             return new Attribute.Class(types,
   402                                        (((JCFieldAccess) tree).selected).type);
   403                                        (((JCFieldAccess) tree).selected).type);
   403         }
   404         }
   404         if (expected.hasTag(CLASS) &&
   405         if (expected.hasTag(CLASS) &&
   405             (expected.tsym.flags() & Flags.ENUM) != 0) {
   406             (expected.tsym.flags() & Flags.ENUM) != 0) {
   406             attr.attribExpr(tree, env, expected);
   407             Type result = attr.attribExpr(tree, env, expected);
   407             Symbol sym = TreeInfo.symbol(tree);
   408             Symbol sym = TreeInfo.symbol(tree);
   408             if (sym == null ||
   409             if (sym == null ||
   409                 TreeInfo.nonstaticSelect(tree) ||
   410                 TreeInfo.nonstaticSelect(tree) ||
   410                 sym.kind != Kinds.VAR ||
   411                 sym.kind != Kinds.VAR ||
   411                 (sym.flags() & Flags.ENUM) == 0) {
   412                 (sym.flags() & Flags.ENUM) == 0) {
   412                 log.error(tree.pos(), "enum.annotation.must.be.enum.constant");
   413                 log.error(tree.pos(), "enum.annotation.must.be.enum.constant");
   413                 return new Attribute.Error(expected);
   414                 return new Attribute.Error(result.getOriginalType());
   414             }
   415             }
   415             VarSymbol enumerator = (VarSymbol) sym;
   416             VarSymbol enumerator = (VarSymbol) sym;
   416             return new Attribute.Enum(expected, enumerator);
   417             return new Attribute.Enum(expected, enumerator);
   417         }
   418         }
   418         //error recovery:
   419         //error recovery: