src/share/classes/com/sun/tools/javac/comp/Annotate.java

Print this page




 131      *  Attribute. Used from MemberEnter for attaching the attributes
 132      *  to the annotated symbol.
 133      */
 134     Attribute.Compound enterAnnotation(JCAnnotation a,
 135                                        Type expected,
 136                                        Env<AttrContext> env) {
 137         // The annotation might have had its type attributed (but not checked)
 138         // by attr.attribAnnotationTypes during MemberEnter, in which case we do not
 139         // need to do it again.
 140         Type at = (a.annotationType.type != null ? a.annotationType.type
 141                   : attr.attribType(a.annotationType, env));
 142         a.type = chk.checkType(a.annotationType.pos(), at, expected);
 143         if (a.type.isErroneous())
 144             return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
 145         if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) {
 146             log.error(a.annotationType.pos(),
 147                       "not.annotation.type", a.type.toString());
 148             return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
 149         }
 150         List<JCExpression> args = a.args;
 151         if (args.length() == 1 && args.head.getTag() != JCTree.ASSIGN) {
 152             // special case: elided "value=" assumed
 153             args.head = make.at(args.head.pos).
 154                 Assign(make.Ident(names.value), args.head);
 155         }
 156         ListBuffer<Pair<MethodSymbol,Attribute>> buf =
 157             new ListBuffer<Pair<MethodSymbol,Attribute>>();
 158         for (List<JCExpression> tl = args; tl.nonEmpty(); tl = tl.tail) {
 159             JCExpression t = tl.head;
 160             if (t.getTag() != JCTree.ASSIGN) {
 161                 log.error(t.pos(), "annotation.value.must.be.name.value");
 162                 continue;
 163             }
 164             JCAssign assign = (JCAssign)t;
 165             if (assign.lhs.getTag() != JCTree.IDENT) {
 166                 log.error(t.pos(), "annotation.value.must.be.name.value");
 167                 continue;
 168             }
 169             JCIdent left = (JCIdent)assign.lhs;
 170             Symbol method = rs.resolveQualifiedMethod(left.pos(),
 171                                                           env,
 172                                                           a.type,
 173                                                           left.name,
 174                                                           List.<Type>nil(),
 175                                                           null);
 176             left.sym = method;
 177             left.type = method.type;
 178             if (method.owner != a.type.tsym)
 179                 log.error(left.pos(), "no.annotation.member", left.name, a.type);
 180             Type result = method.type.getReturnType();
 181             Attribute value = enterAttributeValue(result, assign.rhs, env);
 182             if (!method.type.isErroneous())
 183                 buf.append(new Pair<MethodSymbol,Attribute>
 184                            ((MethodSymbol)method, value));
 185             t.type = result;


 205                 return new Attribute.Error(expected);
 206             if (result.constValue() == null) {
 207                 log.error(tree.pos(), "attribute.value.must.be.constant");
 208                 return new Attribute.Error(expected);
 209             }
 210             result = cfolder.coerce(result, expected);
 211             return new Attribute.Constant(expected, result.constValue());
 212         }
 213         if (expected.tsym == syms.classType.tsym) {
 214             Type result = attr.attribExpr(tree, env, expected);
 215             if (result.isErroneous())
 216                 return new Attribute.Error(expected);
 217             if (TreeInfo.name(tree) != names._class) {
 218                 log.error(tree.pos(), "annotation.value.must.be.class.literal");
 219                 return new Attribute.Error(expected);
 220             }
 221             return new Attribute.Class(types,
 222                                        (((JCFieldAccess) tree).selected).type);
 223         }
 224         if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
 225             if (tree.getTag() != JCTree.ANNOTATION) {
 226                 log.error(tree.pos(), "annotation.value.must.be.annotation");
 227                 expected = syms.errorType;
 228             }
 229             return enterAnnotation((JCAnnotation)tree, expected, env);
 230         }
 231         if (expected.tag == TypeTags.ARRAY) { // should really be isArray()
 232             if (tree.getTag() != JCTree.NEWARRAY) {
 233                 tree = make.at(tree.pos).
 234                     NewArray(null, List.<JCExpression>nil(), List.of(tree));
 235             }
 236             JCNewArray na = (JCNewArray)tree;
 237             if (na.elemtype != null) {
 238                 log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
 239                 return new Attribute.Error(expected);
 240             }
 241             ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
 242             for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
 243                 buf.append(enterAttributeValue(types.elemtype(expected),
 244                                                l.head,
 245                                                env));
 246             }
 247             na.type = expected;
 248             return new Attribute.
 249                 Array(expected, buf.toArray(new Attribute[buf.length()]));
 250         }
 251         if (expected.tag == TypeTags.CLASS &&
 252             (expected.tsym.flags() & Flags.ENUM) != 0) {


 131      *  Attribute. Used from MemberEnter for attaching the attributes
 132      *  to the annotated symbol.
 133      */
 134     Attribute.Compound enterAnnotation(JCAnnotation a,
 135                                        Type expected,
 136                                        Env<AttrContext> env) {
 137         // The annotation might have had its type attributed (but not checked)
 138         // by attr.attribAnnotationTypes during MemberEnter, in which case we do not
 139         // need to do it again.
 140         Type at = (a.annotationType.type != null ? a.annotationType.type
 141                   : attr.attribType(a.annotationType, env));
 142         a.type = chk.checkType(a.annotationType.pos(), at, expected);
 143         if (a.type.isErroneous())
 144             return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
 145         if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) {
 146             log.error(a.annotationType.pos(),
 147                       "not.annotation.type", a.type.toString());
 148             return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
 149         }
 150         List<JCExpression> args = a.args;
 151         if (args.length() == 1 && args.head.getTag() != JCTree.Tag.ASSIGN) {
 152             // special case: elided "value=" assumed
 153             args.head = make.at(args.head.pos).
 154                 Assign(make.Ident(names.value), args.head);
 155         }
 156         ListBuffer<Pair<MethodSymbol,Attribute>> buf =
 157             new ListBuffer<Pair<MethodSymbol,Attribute>>();
 158         for (List<JCExpression> tl = args; tl.nonEmpty(); tl = tl.tail) {
 159             JCExpression t = tl.head;
 160             if (t.getTag() != JCTree.Tag.ASSIGN) {
 161                 log.error(t.pos(), "annotation.value.must.be.name.value");
 162                 continue;
 163             }
 164             JCAssign assign = (JCAssign)t;
 165             if (assign.lhs.getTag() != JCTree.Tag.IDENT) {
 166                 log.error(t.pos(), "annotation.value.must.be.name.value");
 167                 continue;
 168             }
 169             JCIdent left = (JCIdent)assign.lhs;
 170             Symbol method = rs.resolveQualifiedMethod(left.pos(),
 171                                                           env,
 172                                                           a.type,
 173                                                           left.name,
 174                                                           List.<Type>nil(),
 175                                                           null);
 176             left.sym = method;
 177             left.type = method.type;
 178             if (method.owner != a.type.tsym)
 179                 log.error(left.pos(), "no.annotation.member", left.name, a.type);
 180             Type result = method.type.getReturnType();
 181             Attribute value = enterAttributeValue(result, assign.rhs, env);
 182             if (!method.type.isErroneous())
 183                 buf.append(new Pair<MethodSymbol,Attribute>
 184                            ((MethodSymbol)method, value));
 185             t.type = result;


 205                 return new Attribute.Error(expected);
 206             if (result.constValue() == null) {
 207                 log.error(tree.pos(), "attribute.value.must.be.constant");
 208                 return new Attribute.Error(expected);
 209             }
 210             result = cfolder.coerce(result, expected);
 211             return new Attribute.Constant(expected, result.constValue());
 212         }
 213         if (expected.tsym == syms.classType.tsym) {
 214             Type result = attr.attribExpr(tree, env, expected);
 215             if (result.isErroneous())
 216                 return new Attribute.Error(expected);
 217             if (TreeInfo.name(tree) != names._class) {
 218                 log.error(tree.pos(), "annotation.value.must.be.class.literal");
 219                 return new Attribute.Error(expected);
 220             }
 221             return new Attribute.Class(types,
 222                                        (((JCFieldAccess) tree).selected).type);
 223         }
 224         if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
 225             if (tree.getTag() != JCTree.Tag.ANNOTATION) {
 226                 log.error(tree.pos(), "annotation.value.must.be.annotation");
 227                 expected = syms.errorType;
 228             }
 229             return enterAnnotation((JCAnnotation)tree, expected, env);
 230         }
 231         if (expected.tag == TypeTags.ARRAY) { // should really be isArray()
 232             if (tree.getTag() != JCTree.Tag.NEWARRAY) {
 233                 tree = make.at(tree.pos).
 234                     NewArray(null, List.<JCExpression>nil(), List.of(tree));
 235             }
 236             JCNewArray na = (JCNewArray)tree;
 237             if (na.elemtype != null) {
 238                 log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
 239                 return new Attribute.Error(expected);
 240             }
 241             ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
 242             for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
 243                 buf.append(enterAttributeValue(types.elemtype(expected),
 244                                                l.head,
 245                                                env));
 246             }
 247             na.type = expected;
 248             return new Attribute.
 249                 Array(expected, buf.toArray(new Attribute[buf.length()]));
 250         }
 251         if (expected.tag == TypeTags.CLASS &&
 252             (expected.tsym.flags() & Flags.ENUM) != 0) {