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

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 113
eff38cc97183
child 554
9d9f26857129
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

duke@1 1 /*
xdono@117 2 * Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.comp;
duke@1 27
duke@1 28 import com.sun.tools.javac.util.*;
duke@1 29 import com.sun.tools.javac.code.*;
duke@1 30 import com.sun.tools.javac.code.Symbol.*;
duke@1 31 import com.sun.tools.javac.tree.*;
duke@1 32 import com.sun.tools.javac.tree.JCTree.*;
duke@1 33
duke@1 34 /** Enter annotations on symbols. Annotations accumulate in a queue,
duke@1 35 * which is processed at the top level of any set of recursive calls
duke@1 36 * requesting it be processed.
duke@1 37 *
duke@1 38 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 39 * you write code that depends on this, you do so at your own risk.
duke@1 40 * This code and its internal interfaces are subject to change or
duke@1 41 * deletion without notice.</b>
duke@1 42 */
duke@1 43 public class Annotate {
duke@1 44 protected static final Context.Key<Annotate> annotateKey =
duke@1 45 new Context.Key<Annotate>();
duke@1 46
duke@1 47 public static Annotate instance(Context context) {
duke@1 48 Annotate instance = context.get(annotateKey);
duke@1 49 if (instance == null)
duke@1 50 instance = new Annotate(context);
duke@1 51 return instance;
duke@1 52 }
duke@1 53
duke@1 54 final Attr attr;
duke@1 55 final TreeMaker make;
duke@1 56 final Log log;
duke@1 57 final Symtab syms;
jjg@113 58 final Names names;
duke@1 59 final Resolve rs;
duke@1 60 final Types types;
duke@1 61 final ConstFold cfolder;
duke@1 62 final Check chk;
duke@1 63
duke@1 64 protected Annotate(Context context) {
duke@1 65 context.put(annotateKey, this);
duke@1 66 attr = Attr.instance(context);
duke@1 67 make = TreeMaker.instance(context);
duke@1 68 log = Log.instance(context);
duke@1 69 syms = Symtab.instance(context);
jjg@113 70 names = Names.instance(context);
duke@1 71 rs = Resolve.instance(context);
duke@1 72 types = Types.instance(context);
duke@1 73 cfolder = ConstFold.instance(context);
duke@1 74 chk = Check.instance(context);
duke@1 75 }
duke@1 76
duke@1 77 /* ********************************************************************
duke@1 78 * Queue maintenance
duke@1 79 *********************************************************************/
duke@1 80
duke@1 81 private int enterCount = 0;
duke@1 82
duke@1 83 ListBuffer<Annotator> q = new ListBuffer<Annotator>();
duke@1 84
duke@1 85 public void later(Annotator a) {
duke@1 86 q.append(a);
duke@1 87 }
duke@1 88
duke@1 89 public void earlier(Annotator a) {
duke@1 90 q.prepend(a);
duke@1 91 }
duke@1 92
duke@1 93 /** Called when the Enter phase starts. */
duke@1 94 public void enterStart() {
duke@1 95 enterCount++;
duke@1 96 }
duke@1 97
duke@1 98 /** Called after the Enter phase completes. */
duke@1 99 public void enterDone() {
duke@1 100 enterCount--;
duke@1 101 flush();
duke@1 102 }
duke@1 103
duke@1 104 public void flush() {
duke@1 105 if (enterCount != 0) return;
duke@1 106 enterCount++;
duke@1 107 try {
duke@1 108 while (q.nonEmpty())
duke@1 109 q.next().enterAnnotation();
duke@1 110 } finally {
duke@1 111 enterCount--;
duke@1 112 }
duke@1 113 }
duke@1 114
duke@1 115 /** A client that has annotations to add registers an annotator,
duke@1 116 * the method it will use to add the annotation. There are no
duke@1 117 * parameters; any needed data should be captured by the
duke@1 118 * Annotator.
duke@1 119 */
duke@1 120 public interface Annotator {
duke@1 121 void enterAnnotation();
duke@1 122 String toString();
duke@1 123 }
duke@1 124
duke@1 125
duke@1 126 /* ********************************************************************
duke@1 127 * Compute an attribute from its annotation.
duke@1 128 *********************************************************************/
duke@1 129
duke@1 130 /** Process a single compound annotation, returning its
duke@1 131 * Attribute. Used from MemberEnter for attaching the attributes
duke@1 132 * to the annotated symbol.
duke@1 133 */
duke@1 134 Attribute.Compound enterAnnotation(JCAnnotation a,
duke@1 135 Type expected,
duke@1 136 Env<AttrContext> env) {
duke@1 137 // The annotation might have had its type attributed (but not checked)
duke@1 138 // by attr.attribAnnotationTypes during MemberEnter, in which case we do not
duke@1 139 // need to do it again.
duke@1 140 Type at = (a.annotationType.type != null ? a.annotationType.type
duke@1 141 : attr.attribType(a.annotationType, env));
duke@1 142 a.type = chk.checkType(a.annotationType.pos(), at, expected);
duke@1 143 if (a.type.isErroneous())
duke@1 144 return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
duke@1 145 if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) {
duke@1 146 log.error(a.annotationType.pos(),
duke@1 147 "not.annotation.type", a.type.toString());
duke@1 148 return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
duke@1 149 }
duke@1 150 List<JCExpression> args = a.args;
duke@1 151 if (args.length() == 1 && args.head.getTag() != JCTree.ASSIGN) {
duke@1 152 // special case: elided "value=" assumed
duke@1 153 args.head = make.at(args.head.pos).
duke@1 154 Assign(make.Ident(names.value), args.head);
duke@1 155 }
duke@1 156 ListBuffer<Pair<MethodSymbol,Attribute>> buf =
duke@1 157 new ListBuffer<Pair<MethodSymbol,Attribute>>();
duke@1 158 for (List<JCExpression> tl = args; tl.nonEmpty(); tl = tl.tail) {
duke@1 159 JCExpression t = tl.head;
duke@1 160 if (t.getTag() != JCTree.ASSIGN) {
duke@1 161 log.error(t.pos(), "annotation.value.must.be.name.value");
duke@1 162 continue;
duke@1 163 }
duke@1 164 JCAssign assign = (JCAssign)t;
duke@1 165 if (assign.lhs.getTag() != JCTree.IDENT) {
duke@1 166 log.error(t.pos(), "annotation.value.must.be.name.value");
duke@1 167 continue;
duke@1 168 }
duke@1 169 JCIdent left = (JCIdent)assign.lhs;
duke@1 170 Symbol method = rs.resolveQualifiedMethod(left.pos(),
duke@1 171 env,
duke@1 172 a.type,
duke@1 173 left.name,
duke@1 174 List.<Type>nil(),
duke@1 175 null);
duke@1 176 left.sym = method;
duke@1 177 left.type = method.type;
duke@1 178 if (method.owner != a.type.tsym)
duke@1 179 log.error(left.pos(), "no.annotation.member", left.name, a.type);
duke@1 180 Type result = method.type.getReturnType();
duke@1 181 Attribute value = enterAttributeValue(result, assign.rhs, env);
duke@1 182 if (!method.type.isErroneous())
duke@1 183 buf.append(new Pair<MethodSymbol,Attribute>
duke@1 184 ((MethodSymbol)method, value));
duke@1 185 }
duke@1 186 return new Attribute.Compound(a.type, buf.toList());
duke@1 187 }
duke@1 188
duke@1 189 Attribute enterAttributeValue(Type expected,
duke@1 190 JCExpression tree,
duke@1 191 Env<AttrContext> env) {
duke@1 192 if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) {
duke@1 193 Type result = attr.attribExpr(tree, env, expected);
duke@1 194 if (result.isErroneous())
duke@1 195 return new Attribute.Error(expected);
duke@1 196 if (result.constValue() == null) {
duke@1 197 log.error(tree.pos(), "attribute.value.must.be.constant");
duke@1 198 return new Attribute.Error(expected);
duke@1 199 }
duke@1 200 result = cfolder.coerce(result, expected);
duke@1 201 return new Attribute.Constant(expected, result.constValue());
duke@1 202 }
duke@1 203 if (expected.tsym == syms.classType.tsym) {
duke@1 204 Type result = attr.attribExpr(tree, env, expected);
duke@1 205 if (result.isErroneous())
duke@1 206 return new Attribute.Error(expected);
duke@1 207 if (TreeInfo.name(tree) != names._class) {
duke@1 208 log.error(tree.pos(), "annotation.value.must.be.class.literal");
duke@1 209 return new Attribute.Error(expected);
duke@1 210 }
duke@1 211 return new Attribute.Class(types,
duke@1 212 (((JCFieldAccess) tree).selected).type);
duke@1 213 }
duke@1 214 if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
duke@1 215 if (tree.getTag() != JCTree.ANNOTATION) {
duke@1 216 log.error(tree.pos(), "annotation.value.must.be.annotation");
duke@1 217 expected = syms.errorType;
duke@1 218 }
duke@1 219 return enterAnnotation((JCAnnotation)tree, expected, env);
duke@1 220 }
duke@1 221 if (expected.tag == TypeTags.ARRAY) { // should really be isArray()
duke@1 222 if (tree.getTag() != JCTree.NEWARRAY) {
duke@1 223 tree = make.at(tree.pos).
duke@1 224 NewArray(null, List.<JCExpression>nil(), List.of(tree));
duke@1 225 }
duke@1 226 JCNewArray na = (JCNewArray)tree;
duke@1 227 if (na.elemtype != null) {
duke@1 228 log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
duke@1 229 return new Attribute.Error(expected);
duke@1 230 }
duke@1 231 ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
duke@1 232 for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
duke@1 233 buf.append(enterAttributeValue(types.elemtype(expected),
duke@1 234 l.head,
duke@1 235 env));
duke@1 236 }
duke@1 237 return new Attribute.
duke@1 238 Array(expected, buf.toArray(new Attribute[buf.length()]));
duke@1 239 }
duke@1 240 if (expected.tag == TypeTags.CLASS &&
duke@1 241 (expected.tsym.flags() & Flags.ENUM) != 0) {
duke@1 242 attr.attribExpr(tree, env, expected);
duke@1 243 Symbol sym = TreeInfo.symbol(tree);
duke@1 244 if (sym == null ||
duke@1 245 TreeInfo.nonstaticSelect(tree) ||
duke@1 246 sym.kind != Kinds.VAR ||
duke@1 247 (sym.flags() & Flags.ENUM) == 0) {
duke@1 248 log.error(tree.pos(), "enum.annotation.must.be.enum.constant");
duke@1 249 return new Attribute.Error(expected);
duke@1 250 }
duke@1 251 VarSymbol enumerator = (VarSymbol) sym;
duke@1 252 return new Attribute.Enum(expected, enumerator);
duke@1 253 }
duke@1 254 if (!expected.isErroneous())
duke@1 255 log.error(tree.pos(), "annotation.value.not.allowable.type");
duke@1 256 return new Attribute.Error(attr.attribExpr(tree, env, expected));
duke@1 257 }
duke@1 258 }

mercurial