langtools/src/share/classes/com/sun/tools/javac/comp/Annotate.java
changeset 20599 5201f7144a3c
parent 19507 323f001d6be1
child 21015 f3bec12a63e7
equal deleted inserted replaced
20598:aa7b86f98471 20599:5201f7144a3c
   308     }
   308     }
   309 
   309 
   310     Attribute enterAttributeValue(Type expected,
   310     Attribute enterAttributeValue(Type expected,
   311                                   JCExpression tree,
   311                                   JCExpression tree,
   312                                   Env<AttrContext> env) {
   312                                   Env<AttrContext> env) {
       
   313         Type original = expected;
   313         //first, try completing the attribution value sym - if a completion
   314         //first, try completing the attribution value sym - if a completion
   314         //error is thrown, we should recover gracefully, and display an
   315         //error is thrown, we should recover gracefully, and display an
   315         //ordinary resolution diagnostic.
   316         //ordinary resolution diagnostic.
   316         try {
   317         try {
   317             expected.tsym.complete();
   318             expected.tsym.complete();
   318         } catch(CompletionFailure e) {
   319         } catch(CompletionFailure e) {
   319             log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym);
   320             log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym);
   320             return new Attribute.Error(expected);
   321             expected = syms.errType;
       
   322         }
       
   323         if (expected.hasTag(ARRAY)) {
       
   324             if (!tree.hasTag(NEWARRAY)) {
       
   325                 tree = make.at(tree.pos).
       
   326                     NewArray(null, List.<JCExpression>nil(), List.of(tree));
       
   327             }
       
   328             JCNewArray na = (JCNewArray)tree;
       
   329             if (na.elemtype != null) {
       
   330                 log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
       
   331             }
       
   332             ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
       
   333             for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
       
   334                 buf.append(enterAttributeValue(types.elemtype(expected),
       
   335                                                l.head,
       
   336                                                env));
       
   337             }
       
   338             na.type = expected;
       
   339             return new Attribute.
       
   340                 Array(expected, buf.toArray(new Attribute[buf.length()]));
       
   341         }
       
   342         if (tree.hasTag(NEWARRAY)) { //error recovery
       
   343             if (!expected.isErroneous())
       
   344                 log.error(tree.pos(), "annotation.value.not.allowable.type");
       
   345             JCNewArray na = (JCNewArray)tree;
       
   346             if (na.elemtype != null) {
       
   347                 log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
       
   348             }
       
   349             for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
       
   350                 enterAttributeValue(syms.errType,
       
   351                                     l.head,
       
   352                                     env);
       
   353             }
       
   354             return new Attribute.Error(original);
       
   355         }
       
   356         if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
       
   357             if (tree.hasTag(ANNOTATION)) {
       
   358                 return enterAnnotation((JCAnnotation)tree, expected, env);
       
   359             } else {
       
   360                 log.error(tree.pos(), "annotation.value.must.be.annotation");
       
   361                 expected = syms.errType;
       
   362             }
       
   363         }
       
   364         if (tree.hasTag(ANNOTATION)) { //error recovery
       
   365             if (!expected.isErroneous())
       
   366                 log.error(tree.pos(), "annotation.not.valid.for.type", expected);
       
   367             enterAnnotation((JCAnnotation)tree, syms.errType, env);
       
   368             return new Attribute.Error(original);
   321         }
   369         }
   322         if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) {
   370         if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) {
   323             Type result = attr.attribExpr(tree, env, expected);
   371             Type result = attr.attribExpr(tree, env, expected);
   324             if (result.isErroneous())
   372             if (result.isErroneous())
   325                 return new Attribute.Error(expected);
   373                 return new Attribute.Error(expected);
   351                 return new Attribute.Error(expected);
   399                 return new Attribute.Error(expected);
   352             }
   400             }
   353             return new Attribute.Class(types,
   401             return new Attribute.Class(types,
   354                                        (((JCFieldAccess) tree).selected).type);
   402                                        (((JCFieldAccess) tree).selected).type);
   355         }
   403         }
   356         if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
       
   357             if (!tree.hasTag(ANNOTATION)) {
       
   358                 log.error(tree.pos(), "annotation.value.must.be.annotation");
       
   359                 expected = syms.errorType;
       
   360             }
       
   361             return enterAnnotation((JCAnnotation)tree, expected, env);
       
   362         }
       
   363         if (expected.hasTag(ARRAY)) { // should really be isArray()
       
   364             if (!tree.hasTag(NEWARRAY)) {
       
   365                 tree = make.at(tree.pos).
       
   366                     NewArray(null, List.<JCExpression>nil(), List.of(tree));
       
   367             }
       
   368             JCNewArray na = (JCNewArray)tree;
       
   369             if (na.elemtype != null) {
       
   370                 log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
       
   371                 return new Attribute.Error(expected);
       
   372             }
       
   373             ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
       
   374             for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
       
   375                 buf.append(enterAttributeValue(types.elemtype(expected),
       
   376                                                l.head,
       
   377                                                env));
       
   378             }
       
   379             na.type = expected;
       
   380             return new Attribute.
       
   381                 Array(expected, buf.toArray(new Attribute[buf.length()]));
       
   382         }
       
   383         if (expected.hasTag(CLASS) &&
   404         if (expected.hasTag(CLASS) &&
   384             (expected.tsym.flags() & Flags.ENUM) != 0) {
   405             (expected.tsym.flags() & Flags.ENUM) != 0) {
   385             attr.attribExpr(tree, env, expected);
   406             attr.attribExpr(tree, env, expected);
   386             Symbol sym = TreeInfo.symbol(tree);
   407             Symbol sym = TreeInfo.symbol(tree);
   387             if (sym == null ||
   408             if (sym == null ||
   392                 return new Attribute.Error(expected);
   413                 return new Attribute.Error(expected);
   393             }
   414             }
   394             VarSymbol enumerator = (VarSymbol) sym;
   415             VarSymbol enumerator = (VarSymbol) sym;
   395             return new Attribute.Enum(expected, enumerator);
   416             return new Attribute.Enum(expected, enumerator);
   396         }
   417         }
       
   418         //error recovery:
   397         if (!expected.isErroneous())
   419         if (!expected.isErroneous())
   398             log.error(tree.pos(), "annotation.value.not.allowable.type");
   420             log.error(tree.pos(), "annotation.value.not.allowable.type");
   399         return new Attribute.Error(attr.attribExpr(tree, env, expected));
   421         return new Attribute.Error(attr.attribExpr(tree, env, expected));
   400     }
   422     }
   401 
   423