Wed, 21 Sep 2011 21:56:53 -0700
7092965: javac should not close processorClassLoader before end of compilation
Reviewed-by: darcy
duke@1 | 1 | /* |
ohair@798 | 2 | * Copyright (c) 2003, 2010, Oracle and/or its affiliates. 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 |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle 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 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * 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 | * |
jjg@581 | 38 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 39 | * If 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(), |
mcimadamore@992 | 171 | env, |
mcimadamore@992 | 172 | a.type, |
mcimadamore@992 | 173 | left.name, |
mcimadamore@992 | 174 | List.<Type>nil(), |
mcimadamore@992 | 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)); |
mcimadamore@676 | 185 | t.type = result; |
duke@1 | 186 | } |
duke@1 | 187 | return new Attribute.Compound(a.type, buf.toList()); |
duke@1 | 188 | } |
duke@1 | 189 | |
duke@1 | 190 | Attribute enterAttributeValue(Type expected, |
duke@1 | 191 | JCExpression tree, |
duke@1 | 192 | Env<AttrContext> env) { |
mcimadamore@992 | 193 | //first, try completing the attribution value sym - if a completion |
mcimadamore@992 | 194 | //error is thrown, we should recover gracefully, and display an |
mcimadamore@992 | 195 | //ordinary resolution diagnostic. |
mcimadamore@992 | 196 | try { |
mcimadamore@992 | 197 | expected.tsym.complete(); |
mcimadamore@992 | 198 | } catch(CompletionFailure e) { |
mcimadamore@992 | 199 | log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym); |
mcimadamore@992 | 200 | return new Attribute.Error(expected); |
mcimadamore@992 | 201 | } |
duke@1 | 202 | if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) { |
duke@1 | 203 | Type result = attr.attribExpr(tree, env, expected); |
duke@1 | 204 | if (result.isErroneous()) |
duke@1 | 205 | return new Attribute.Error(expected); |
duke@1 | 206 | if (result.constValue() == null) { |
duke@1 | 207 | log.error(tree.pos(), "attribute.value.must.be.constant"); |
duke@1 | 208 | return new Attribute.Error(expected); |
duke@1 | 209 | } |
duke@1 | 210 | result = cfolder.coerce(result, expected); |
duke@1 | 211 | return new Attribute.Constant(expected, result.constValue()); |
duke@1 | 212 | } |
duke@1 | 213 | if (expected.tsym == syms.classType.tsym) { |
duke@1 | 214 | Type result = attr.attribExpr(tree, env, expected); |
duke@1 | 215 | if (result.isErroneous()) |
duke@1 | 216 | return new Attribute.Error(expected); |
duke@1 | 217 | if (TreeInfo.name(tree) != names._class) { |
duke@1 | 218 | log.error(tree.pos(), "annotation.value.must.be.class.literal"); |
duke@1 | 219 | return new Attribute.Error(expected); |
duke@1 | 220 | } |
duke@1 | 221 | return new Attribute.Class(types, |
duke@1 | 222 | (((JCFieldAccess) tree).selected).type); |
duke@1 | 223 | } |
duke@1 | 224 | if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) { |
duke@1 | 225 | if (tree.getTag() != JCTree.ANNOTATION) { |
duke@1 | 226 | log.error(tree.pos(), "annotation.value.must.be.annotation"); |
duke@1 | 227 | expected = syms.errorType; |
duke@1 | 228 | } |
duke@1 | 229 | return enterAnnotation((JCAnnotation)tree, expected, env); |
duke@1 | 230 | } |
duke@1 | 231 | if (expected.tag == TypeTags.ARRAY) { // should really be isArray() |
duke@1 | 232 | if (tree.getTag() != JCTree.NEWARRAY) { |
duke@1 | 233 | tree = make.at(tree.pos). |
duke@1 | 234 | NewArray(null, List.<JCExpression>nil(), List.of(tree)); |
duke@1 | 235 | } |
duke@1 | 236 | JCNewArray na = (JCNewArray)tree; |
duke@1 | 237 | if (na.elemtype != null) { |
duke@1 | 238 | log.error(na.elemtype.pos(), "new.not.allowed.in.annotation"); |
duke@1 | 239 | return new Attribute.Error(expected); |
duke@1 | 240 | } |
duke@1 | 241 | ListBuffer<Attribute> buf = new ListBuffer<Attribute>(); |
duke@1 | 242 | for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) { |
duke@1 | 243 | buf.append(enterAttributeValue(types.elemtype(expected), |
duke@1 | 244 | l.head, |
duke@1 | 245 | env)); |
duke@1 | 246 | } |
mcimadamore@676 | 247 | na.type = expected; |
duke@1 | 248 | return new Attribute. |
duke@1 | 249 | Array(expected, buf.toArray(new Attribute[buf.length()])); |
duke@1 | 250 | } |
duke@1 | 251 | if (expected.tag == TypeTags.CLASS && |
duke@1 | 252 | (expected.tsym.flags() & Flags.ENUM) != 0) { |
duke@1 | 253 | attr.attribExpr(tree, env, expected); |
duke@1 | 254 | Symbol sym = TreeInfo.symbol(tree); |
duke@1 | 255 | if (sym == null || |
duke@1 | 256 | TreeInfo.nonstaticSelect(tree) || |
duke@1 | 257 | sym.kind != Kinds.VAR || |
duke@1 | 258 | (sym.flags() & Flags.ENUM) == 0) { |
duke@1 | 259 | log.error(tree.pos(), "enum.annotation.must.be.enum.constant"); |
duke@1 | 260 | return new Attribute.Error(expected); |
duke@1 | 261 | } |
duke@1 | 262 | VarSymbol enumerator = (VarSymbol) sym; |
duke@1 | 263 | return new Attribute.Enum(expected, enumerator); |
duke@1 | 264 | } |
duke@1 | 265 | if (!expected.isErroneous()) |
duke@1 | 266 | log.error(tree.pos(), "annotation.value.not.allowable.type"); |
duke@1 | 267 | return new Attribute.Error(attr.attribExpr(tree, env, expected)); |
duke@1 | 268 | } |
duke@1 | 269 | } |