Mon, 14 Oct 2013 23:07:43 -0700
8025998: Missing LV table in lambda bodies
Reviewed-by: vromero
duke@1 | 1 | /* |
jjg@1492 | 2 | * Copyright (c) 2003, 2013, 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 | |
jfranck@1313 | 28 | import java.util.Map; |
jjg@1521 | 29 | |
duke@1 | 30 | import com.sun.tools.javac.util.*; |
jfranck@1313 | 31 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
duke@1 | 32 | import com.sun.tools.javac.code.*; |
duke@1 | 33 | import com.sun.tools.javac.code.Symbol.*; |
duke@1 | 34 | import com.sun.tools.javac.tree.*; |
duke@1 | 35 | import com.sun.tools.javac.tree.JCTree.*; |
duke@1 | 36 | |
jjg@1374 | 37 | import static com.sun.tools.javac.code.TypeTag.ARRAY; |
jjg@1374 | 38 | import static com.sun.tools.javac.code.TypeTag.CLASS; |
jjg@1127 | 39 | import static com.sun.tools.javac.tree.JCTree.Tag.*; |
jlahoda@2108 | 40 | import javax.lang.model.type.ErrorType; |
jjg@1127 | 41 | |
duke@1 | 42 | /** Enter annotations on symbols. Annotations accumulate in a queue, |
duke@1 | 43 | * which is processed at the top level of any set of recursive calls |
duke@1 | 44 | * requesting it be processed. |
duke@1 | 45 | * |
jjg@581 | 46 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 47 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 48 | * This code and its internal interfaces are subject to change or |
duke@1 | 49 | * deletion without notice.</b> |
duke@1 | 50 | */ |
duke@1 | 51 | public class Annotate { |
duke@1 | 52 | protected static final Context.Key<Annotate> annotateKey = |
duke@1 | 53 | new Context.Key<Annotate>(); |
duke@1 | 54 | |
duke@1 | 55 | public static Annotate instance(Context context) { |
duke@1 | 56 | Annotate instance = context.get(annotateKey); |
duke@1 | 57 | if (instance == null) |
duke@1 | 58 | instance = new Annotate(context); |
duke@1 | 59 | return instance; |
duke@1 | 60 | } |
duke@1 | 61 | |
duke@1 | 62 | final Attr attr; |
duke@1 | 63 | final TreeMaker make; |
duke@1 | 64 | final Log log; |
duke@1 | 65 | final Symtab syms; |
jjg@113 | 66 | final Names names; |
duke@1 | 67 | final Resolve rs; |
duke@1 | 68 | final Types types; |
duke@1 | 69 | final ConstFold cfolder; |
duke@1 | 70 | final Check chk; |
duke@1 | 71 | |
duke@1 | 72 | protected Annotate(Context context) { |
duke@1 | 73 | context.put(annotateKey, this); |
duke@1 | 74 | attr = Attr.instance(context); |
duke@1 | 75 | make = TreeMaker.instance(context); |
duke@1 | 76 | log = Log.instance(context); |
duke@1 | 77 | syms = Symtab.instance(context); |
jjg@113 | 78 | names = Names.instance(context); |
duke@1 | 79 | rs = Resolve.instance(context); |
duke@1 | 80 | types = Types.instance(context); |
duke@1 | 81 | cfolder = ConstFold.instance(context); |
duke@1 | 82 | chk = Check.instance(context); |
duke@1 | 83 | } |
duke@1 | 84 | |
duke@1 | 85 | /* ******************************************************************** |
duke@1 | 86 | * Queue maintenance |
duke@1 | 87 | *********************************************************************/ |
duke@1 | 88 | |
duke@1 | 89 | private int enterCount = 0; |
duke@1 | 90 | |
duke@1 | 91 | ListBuffer<Annotator> q = new ListBuffer<Annotator>(); |
jjg@1521 | 92 | ListBuffer<Annotator> typesQ = new ListBuffer<Annotator>(); |
jfranck@1313 | 93 | ListBuffer<Annotator> repeatedQ = new ListBuffer<Annotator>(); |
jjg@1521 | 94 | ListBuffer<Annotator> afterRepeatedQ = new ListBuffer<Annotator>(); |
jlahoda@2111 | 95 | ListBuffer<Annotator> validateQ = new ListBuffer<Annotator>(); |
jjg@1521 | 96 | |
jjg@1521 | 97 | public void earlier(Annotator a) { |
jjg@1521 | 98 | q.prepend(a); |
jjg@1521 | 99 | } |
duke@1 | 100 | |
jfranck@1313 | 101 | public void normal(Annotator a) { |
duke@1 | 102 | q.append(a); |
duke@1 | 103 | } |
duke@1 | 104 | |
jjg@1521 | 105 | public void typeAnnotation(Annotator a) { |
jjg@1521 | 106 | typesQ.append(a); |
duke@1 | 107 | } |
duke@1 | 108 | |
jfranck@1313 | 109 | public void repeated(Annotator a) { |
jfranck@1313 | 110 | repeatedQ.append(a); |
jfranck@1313 | 111 | } |
jfranck@1313 | 112 | |
jjg@1521 | 113 | public void afterRepeated(Annotator a) { |
jjg@1521 | 114 | afterRepeatedQ.append(a); |
jjg@1521 | 115 | } |
jjg@1521 | 116 | |
jlahoda@2111 | 117 | public void validate(Annotator a) { |
jlahoda@2111 | 118 | validateQ.append(a); |
jlahoda@2111 | 119 | } |
jlahoda@2111 | 120 | |
duke@1 | 121 | /** Called when the Enter phase starts. */ |
duke@1 | 122 | public void enterStart() { |
duke@1 | 123 | enterCount++; |
duke@1 | 124 | } |
duke@1 | 125 | |
duke@1 | 126 | /** Called after the Enter phase completes. */ |
duke@1 | 127 | public void enterDone() { |
duke@1 | 128 | enterCount--; |
duke@1 | 129 | flush(); |
duke@1 | 130 | } |
duke@1 | 131 | |
duke@1 | 132 | public void flush() { |
duke@1 | 133 | if (enterCount != 0) return; |
duke@1 | 134 | enterCount++; |
duke@1 | 135 | try { |
jjg@1521 | 136 | while (q.nonEmpty()) { |
duke@1 | 137 | q.next().enterAnnotation(); |
jjg@1521 | 138 | } |
jjg@1521 | 139 | while (typesQ.nonEmpty()) { |
jjg@1521 | 140 | typesQ.next().enterAnnotation(); |
jjg@1521 | 141 | } |
jfranck@1313 | 142 | while (repeatedQ.nonEmpty()) { |
jfranck@1313 | 143 | repeatedQ.next().enterAnnotation(); |
jfranck@1313 | 144 | } |
jjg@1521 | 145 | while (afterRepeatedQ.nonEmpty()) { |
jjg@1521 | 146 | afterRepeatedQ.next().enterAnnotation(); |
jjg@1521 | 147 | } |
jlahoda@2111 | 148 | while (validateQ.nonEmpty()) { |
jlahoda@2111 | 149 | validateQ.next().enterAnnotation(); |
jlahoda@2111 | 150 | } |
duke@1 | 151 | } finally { |
duke@1 | 152 | enterCount--; |
duke@1 | 153 | } |
duke@1 | 154 | } |
duke@1 | 155 | |
duke@1 | 156 | /** A client that has annotations to add registers an annotator, |
duke@1 | 157 | * the method it will use to add the annotation. There are no |
duke@1 | 158 | * parameters; any needed data should be captured by the |
duke@1 | 159 | * Annotator. |
duke@1 | 160 | */ |
duke@1 | 161 | public interface Annotator { |
duke@1 | 162 | void enterAnnotation(); |
duke@1 | 163 | String toString(); |
duke@1 | 164 | } |
duke@1 | 165 | |
jfranck@1313 | 166 | /** |
jfranck@1313 | 167 | * This context contains all the information needed to synthesize new |
jfranck@1313 | 168 | * annotations trees by the completer for repeating annotations. |
jfranck@1313 | 169 | */ |
jjg@1521 | 170 | public class AnnotateRepeatedContext<T extends Attribute.Compound> { |
jfranck@1313 | 171 | public final Env<AttrContext> env; |
jjg@1521 | 172 | public final Map<Symbol.TypeSymbol, ListBuffer<T>> annotated; |
jjg@1521 | 173 | public final Map<T, JCDiagnostic.DiagnosticPosition> pos; |
jfranck@1313 | 174 | public final Log log; |
jjg@1521 | 175 | public final boolean isTypeCompound; |
jfranck@1313 | 176 | |
jfranck@1313 | 177 | public AnnotateRepeatedContext(Env<AttrContext> env, |
jjg@1521 | 178 | Map<Symbol.TypeSymbol, ListBuffer<T>> annotated, |
jjg@1521 | 179 | Map<T, JCDiagnostic.DiagnosticPosition> pos, |
jjg@1521 | 180 | Log log, |
jjg@1521 | 181 | boolean isTypeCompound) { |
jjg@1322 | 182 | Assert.checkNonNull(env); |
jjg@1322 | 183 | Assert.checkNonNull(annotated); |
jjg@1322 | 184 | Assert.checkNonNull(pos); |
jjg@1322 | 185 | Assert.checkNonNull(log); |
jfranck@1313 | 186 | |
jfranck@1313 | 187 | this.env = env; |
jfranck@1313 | 188 | this.annotated = annotated; |
jfranck@1313 | 189 | this.pos = pos; |
jfranck@1313 | 190 | this.log = log; |
jjg@1521 | 191 | this.isTypeCompound = isTypeCompound; |
jfranck@1313 | 192 | } |
jfranck@1313 | 193 | |
jfranck@1313 | 194 | /** |
jfranck@1313 | 195 | * Process a list of repeating annotations returning a new |
jfranck@1313 | 196 | * Attribute.Compound that is the attribute for the synthesized tree |
jfranck@1313 | 197 | * for the container. |
jfranck@1313 | 198 | * |
jfranck@1313 | 199 | * @param repeatingAnnotations a List of repeating annotations |
jfranck@1313 | 200 | * @return a new Attribute.Compound that is the container for the repeatingAnnotations |
jfranck@1313 | 201 | */ |
jjg@1521 | 202 | public T processRepeatedAnnotations(List<T> repeatingAnnotations, Symbol sym) { |
jfranck@1445 | 203 | return Annotate.this.processRepeatedAnnotations(repeatingAnnotations, this, sym); |
jfranck@1313 | 204 | } |
jfranck@1313 | 205 | |
jfranck@1313 | 206 | /** |
jfranck@1313 | 207 | * Queue the Annotator a on the repeating annotations queue of the |
jfranck@1313 | 208 | * Annotate instance this context belongs to. |
jfranck@1313 | 209 | * |
jfranck@1313 | 210 | * @param a the Annotator to enqueue for repeating annotation annotating |
jfranck@1313 | 211 | */ |
jfranck@1313 | 212 | public void annotateRepeated(Annotator a) { |
jfranck@1313 | 213 | Annotate.this.repeated(a); |
jfranck@1313 | 214 | } |
jfranck@1313 | 215 | } |
duke@1 | 216 | |
duke@1 | 217 | /* ******************************************************************** |
duke@1 | 218 | * Compute an attribute from its annotation. |
duke@1 | 219 | *********************************************************************/ |
duke@1 | 220 | |
duke@1 | 221 | /** Process a single compound annotation, returning its |
duke@1 | 222 | * Attribute. Used from MemberEnter for attaching the attributes |
duke@1 | 223 | * to the annotated symbol. |
duke@1 | 224 | */ |
duke@1 | 225 | Attribute.Compound enterAnnotation(JCAnnotation a, |
duke@1 | 226 | Type expected, |
duke@1 | 227 | Env<AttrContext> env) { |
jjg@1755 | 228 | return enterAnnotation(a, expected, env, false); |
jjg@1755 | 229 | } |
jjg@1755 | 230 | |
jjg@1755 | 231 | Attribute.TypeCompound enterTypeAnnotation(JCAnnotation a, |
jjg@1755 | 232 | Type expected, |
jjg@1755 | 233 | Env<AttrContext> env) { |
jjg@1755 | 234 | return (Attribute.TypeCompound) enterAnnotation(a, expected, env, true); |
jjg@1755 | 235 | } |
jjg@1755 | 236 | |
jjg@1755 | 237 | // boolean typeAnnotation determines whether the method returns |
jjg@1755 | 238 | // a Compound (false) or TypeCompound (true). |
jjg@1755 | 239 | Attribute.Compound enterAnnotation(JCAnnotation a, |
jjg@1755 | 240 | Type expected, |
jjg@1755 | 241 | Env<AttrContext> env, |
jjg@1755 | 242 | boolean typeAnnotation) { |
duke@1 | 243 | // The annotation might have had its type attributed (but not checked) |
duke@1 | 244 | // by attr.attribAnnotationTypes during MemberEnter, in which case we do not |
duke@1 | 245 | // need to do it again. |
duke@1 | 246 | Type at = (a.annotationType.type != null ? a.annotationType.type |
duke@1 | 247 | : attr.attribType(a.annotationType, env)); |
duke@1 | 248 | a.type = chk.checkType(a.annotationType.pos(), at, expected); |
jjg@1755 | 249 | if (a.type.isErroneous()) { |
jjg@1755 | 250 | if (typeAnnotation) { |
jjg@1755 | 251 | return new Attribute.TypeCompound(a.type, List.<Pair<MethodSymbol,Attribute>>nil(), null); |
jjg@1755 | 252 | } else { |
jjg@1755 | 253 | return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil()); |
jjg@1755 | 254 | } |
jjg@1755 | 255 | } |
duke@1 | 256 | if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) { |
duke@1 | 257 | log.error(a.annotationType.pos(), |
duke@1 | 258 | "not.annotation.type", a.type.toString()); |
jjg@1755 | 259 | if (typeAnnotation) { |
jjg@1755 | 260 | return new Attribute.TypeCompound(a.type, List.<Pair<MethodSymbol,Attribute>>nil(), null); |
jjg@1755 | 261 | } else { |
jjg@1755 | 262 | return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil()); |
jjg@1755 | 263 | } |
duke@1 | 264 | } |
duke@1 | 265 | List<JCExpression> args = a.args; |
jjg@1127 | 266 | if (args.length() == 1 && !args.head.hasTag(ASSIGN)) { |
duke@1 | 267 | // special case: elided "value=" assumed |
duke@1 | 268 | args.head = make.at(args.head.pos). |
duke@1 | 269 | Assign(make.Ident(names.value), args.head); |
duke@1 | 270 | } |
duke@1 | 271 | ListBuffer<Pair<MethodSymbol,Attribute>> buf = |
duke@1 | 272 | new ListBuffer<Pair<MethodSymbol,Attribute>>(); |
duke@1 | 273 | for (List<JCExpression> tl = args; tl.nonEmpty(); tl = tl.tail) { |
duke@1 | 274 | JCExpression t = tl.head; |
jjg@1127 | 275 | if (!t.hasTag(ASSIGN)) { |
duke@1 | 276 | log.error(t.pos(), "annotation.value.must.be.name.value"); |
duke@1 | 277 | continue; |
duke@1 | 278 | } |
duke@1 | 279 | JCAssign assign = (JCAssign)t; |
jjg@1127 | 280 | if (!assign.lhs.hasTag(IDENT)) { |
duke@1 | 281 | log.error(t.pos(), "annotation.value.must.be.name.value"); |
duke@1 | 282 | continue; |
duke@1 | 283 | } |
duke@1 | 284 | JCIdent left = (JCIdent)assign.lhs; |
ksrini@1914 | 285 | Symbol method = rs.resolveQualifiedMethod(assign.rhs.pos(), |
mcimadamore@992 | 286 | env, |
mcimadamore@992 | 287 | a.type, |
mcimadamore@992 | 288 | left.name, |
mcimadamore@992 | 289 | List.<Type>nil(), |
mcimadamore@992 | 290 | null); |
duke@1 | 291 | left.sym = method; |
duke@1 | 292 | left.type = method.type; |
duke@1 | 293 | if (method.owner != a.type.tsym) |
duke@1 | 294 | log.error(left.pos(), "no.annotation.member", left.name, a.type); |
duke@1 | 295 | Type result = method.type.getReturnType(); |
duke@1 | 296 | Attribute value = enterAttributeValue(result, assign.rhs, env); |
duke@1 | 297 | if (!method.type.isErroneous()) |
duke@1 | 298 | buf.append(new Pair<MethodSymbol,Attribute> |
duke@1 | 299 | ((MethodSymbol)method, value)); |
mcimadamore@676 | 300 | t.type = result; |
duke@1 | 301 | } |
jjg@1755 | 302 | if (typeAnnotation) { |
jjg@1755 | 303 | if (a.attribute == null || !(a.attribute instanceof Attribute.TypeCompound)) { |
jjg@1755 | 304 | // Create a new TypeCompound |
jjg@1755 | 305 | Attribute.TypeCompound tc = new Attribute.TypeCompound(a.type, buf.toList(), new TypeAnnotationPosition()); |
jjg@1755 | 306 | a.attribute = tc; |
jjg@1755 | 307 | return tc; |
jjg@1755 | 308 | } else { |
jjg@1755 | 309 | // Use an existing TypeCompound |
jjg@1755 | 310 | return a.attribute; |
jjg@1755 | 311 | } |
jjg@1755 | 312 | } else { |
jjg@1755 | 313 | Attribute.Compound ac = new Attribute.Compound(a.type, buf.toList()); |
jjg@1755 | 314 | a.attribute = ac; |
jjg@1755 | 315 | return ac; |
jjg@1755 | 316 | } |
duke@1 | 317 | } |
duke@1 | 318 | |
duke@1 | 319 | Attribute enterAttributeValue(Type expected, |
duke@1 | 320 | JCExpression tree, |
duke@1 | 321 | Env<AttrContext> env) { |
mcimadamore@992 | 322 | //first, try completing the attribution value sym - if a completion |
mcimadamore@992 | 323 | //error is thrown, we should recover gracefully, and display an |
mcimadamore@992 | 324 | //ordinary resolution diagnostic. |
mcimadamore@992 | 325 | try { |
mcimadamore@992 | 326 | expected.tsym.complete(); |
mcimadamore@992 | 327 | } catch(CompletionFailure e) { |
mcimadamore@992 | 328 | log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym); |
jlahoda@2070 | 329 | expected = syms.errType; |
jlahoda@2070 | 330 | } |
jlahoda@2070 | 331 | if (expected.hasTag(ARRAY)) { |
jlahoda@2070 | 332 | if (!tree.hasTag(NEWARRAY)) { |
jlahoda@2070 | 333 | tree = make.at(tree.pos). |
jlahoda@2070 | 334 | NewArray(null, List.<JCExpression>nil(), List.of(tree)); |
jlahoda@2070 | 335 | } |
jlahoda@2070 | 336 | JCNewArray na = (JCNewArray)tree; |
jlahoda@2070 | 337 | if (na.elemtype != null) { |
jlahoda@2070 | 338 | log.error(na.elemtype.pos(), "new.not.allowed.in.annotation"); |
jlahoda@2070 | 339 | } |
jlahoda@2070 | 340 | ListBuffer<Attribute> buf = new ListBuffer<Attribute>(); |
jlahoda@2070 | 341 | for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) { |
jlahoda@2070 | 342 | buf.append(enterAttributeValue(types.elemtype(expected), |
jlahoda@2070 | 343 | l.head, |
jlahoda@2070 | 344 | env)); |
jlahoda@2070 | 345 | } |
jlahoda@2070 | 346 | na.type = expected; |
jlahoda@2070 | 347 | return new Attribute. |
jlahoda@2070 | 348 | Array(expected, buf.toArray(new Attribute[buf.length()])); |
jlahoda@2070 | 349 | } |
jlahoda@2070 | 350 | if (tree.hasTag(NEWARRAY)) { //error recovery |
jlahoda@2070 | 351 | if (!expected.isErroneous()) |
jlahoda@2070 | 352 | log.error(tree.pos(), "annotation.value.not.allowable.type"); |
jlahoda@2070 | 353 | JCNewArray na = (JCNewArray)tree; |
jlahoda@2070 | 354 | if (na.elemtype != null) { |
jlahoda@2070 | 355 | log.error(na.elemtype.pos(), "new.not.allowed.in.annotation"); |
jlahoda@2070 | 356 | } |
jlahoda@2070 | 357 | for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) { |
jlahoda@2070 | 358 | enterAttributeValue(syms.errType, |
jlahoda@2070 | 359 | l.head, |
jlahoda@2070 | 360 | env); |
jlahoda@2070 | 361 | } |
jlahoda@2108 | 362 | return new Attribute.Error(syms.errType); |
jlahoda@2070 | 363 | } |
jlahoda@2070 | 364 | if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) { |
jlahoda@2070 | 365 | if (tree.hasTag(ANNOTATION)) { |
jlahoda@2070 | 366 | return enterAnnotation((JCAnnotation)tree, expected, env); |
jlahoda@2070 | 367 | } else { |
jlahoda@2070 | 368 | log.error(tree.pos(), "annotation.value.must.be.annotation"); |
jlahoda@2070 | 369 | expected = syms.errType; |
jlahoda@2070 | 370 | } |
jlahoda@2070 | 371 | } |
jlahoda@2070 | 372 | if (tree.hasTag(ANNOTATION)) { //error recovery |
jlahoda@2070 | 373 | if (!expected.isErroneous()) |
jlahoda@2070 | 374 | log.error(tree.pos(), "annotation.not.valid.for.type", expected); |
jlahoda@2070 | 375 | enterAnnotation((JCAnnotation)tree, syms.errType, env); |
jlahoda@2108 | 376 | return new Attribute.Error(((JCAnnotation)tree).annotationType.type); |
mcimadamore@992 | 377 | } |
duke@1 | 378 | if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) { |
duke@1 | 379 | Type result = attr.attribExpr(tree, env, expected); |
duke@1 | 380 | if (result.isErroneous()) |
jlahoda@2108 | 381 | return new Attribute.Error(result.getOriginalType()); |
duke@1 | 382 | if (result.constValue() == null) { |
duke@1 | 383 | log.error(tree.pos(), "attribute.value.must.be.constant"); |
duke@1 | 384 | return new Attribute.Error(expected); |
duke@1 | 385 | } |
duke@1 | 386 | result = cfolder.coerce(result, expected); |
duke@1 | 387 | return new Attribute.Constant(expected, result.constValue()); |
duke@1 | 388 | } |
duke@1 | 389 | if (expected.tsym == syms.classType.tsym) { |
duke@1 | 390 | Type result = attr.attribExpr(tree, env, expected); |
jfranck@1960 | 391 | if (result.isErroneous()) { |
jlahoda@2108 | 392 | // Does it look like an unresolved class literal? |
jlahoda@2108 | 393 | if (TreeInfo.name(tree) == names._class && |
jlahoda@2108 | 394 | ((JCFieldAccess) tree).selected.type.isErroneous()) { |
jfranck@1960 | 395 | Name n = (((JCFieldAccess) tree).selected).type.tsym.flatName(); |
jfranck@1960 | 396 | return new Attribute.UnresolvedClass(expected, |
jfranck@1960 | 397 | types.createErrorType(n, |
jfranck@1960 | 398 | syms.unknownSymbol, syms.classType)); |
jfranck@1960 | 399 | } else { |
jlahoda@2108 | 400 | return new Attribute.Error(result.getOriginalType()); |
jfranck@1960 | 401 | } |
jfranck@1960 | 402 | } |
jfranck@1960 | 403 | |
jfranck@1960 | 404 | // Class literals look like field accesses of a field named class |
jfranck@1960 | 405 | // at the tree level |
duke@1 | 406 | if (TreeInfo.name(tree) != names._class) { |
duke@1 | 407 | log.error(tree.pos(), "annotation.value.must.be.class.literal"); |
jlahoda@2108 | 408 | return new Attribute.Error(syms.errType); |
duke@1 | 409 | } |
duke@1 | 410 | return new Attribute.Class(types, |
duke@1 | 411 | (((JCFieldAccess) tree).selected).type); |
duke@1 | 412 | } |
jjg@1374 | 413 | if (expected.hasTag(CLASS) && |
duke@1 | 414 | (expected.tsym.flags() & Flags.ENUM) != 0) { |
jlahoda@2108 | 415 | Type result = attr.attribExpr(tree, env, expected); |
duke@1 | 416 | Symbol sym = TreeInfo.symbol(tree); |
duke@1 | 417 | if (sym == null || |
duke@1 | 418 | TreeInfo.nonstaticSelect(tree) || |
duke@1 | 419 | sym.kind != Kinds.VAR || |
duke@1 | 420 | (sym.flags() & Flags.ENUM) == 0) { |
duke@1 | 421 | log.error(tree.pos(), "enum.annotation.must.be.enum.constant"); |
jlahoda@2108 | 422 | return new Attribute.Error(result.getOriginalType()); |
duke@1 | 423 | } |
duke@1 | 424 | VarSymbol enumerator = (VarSymbol) sym; |
duke@1 | 425 | return new Attribute.Enum(expected, enumerator); |
duke@1 | 426 | } |
jlahoda@2070 | 427 | //error recovery: |
duke@1 | 428 | if (!expected.isErroneous()) |
duke@1 | 429 | log.error(tree.pos(), "annotation.value.not.allowable.type"); |
duke@1 | 430 | return new Attribute.Error(attr.attribExpr(tree, env, expected)); |
duke@1 | 431 | } |
jfranck@1313 | 432 | |
jfranck@1313 | 433 | /* ********************************* |
jfranck@1313 | 434 | * Support for repeating annotations |
jfranck@1313 | 435 | ***********************************/ |
jfranck@1313 | 436 | |
jfranck@1313 | 437 | /* Process repeated annotations. This method returns the |
jfranck@1313 | 438 | * synthesized container annotation or null IFF all repeating |
jfranck@1313 | 439 | * annotation are invalid. This method reports errors/warnings. |
jfranck@1313 | 440 | */ |
jjg@1521 | 441 | private <T extends Attribute.Compound> T processRepeatedAnnotations(List<T> annotations, |
jjg@1521 | 442 | AnnotateRepeatedContext<T> ctx, |
jjg@1521 | 443 | Symbol on) { |
jjg@1521 | 444 | T firstOccurrence = annotations.head; |
jfranck@1313 | 445 | List<Attribute> repeated = List.nil(); |
jfranck@1445 | 446 | Type origAnnoType = null; |
jfranck@1313 | 447 | Type arrayOfOrigAnnoType = null; |
jfranck@1313 | 448 | Type targetContainerType = null; |
jfranck@1313 | 449 | MethodSymbol containerValueSymbol = null; |
jfranck@1313 | 450 | |
jfranck@1313 | 451 | Assert.check(!annotations.isEmpty() && |
jfranck@1313 | 452 | !annotations.tail.isEmpty()); // i.e. size() > 1 |
jfranck@1313 | 453 | |
jfranck@1629 | 454 | int count = 0; |
jjg@1521 | 455 | for (List<T> al = annotations; |
jfranck@1313 | 456 | !al.isEmpty(); |
jfranck@1313 | 457 | al = al.tail) |
jfranck@1313 | 458 | { |
jfranck@1629 | 459 | count++; |
jfranck@1629 | 460 | |
jfranck@1629 | 461 | // There must be more than a single anno in the annotation list |
jfranck@1629 | 462 | Assert.check(count > 1 || !al.tail.isEmpty()); |
jfranck@1629 | 463 | |
jjg@1521 | 464 | T currentAnno = al.head; |
jfranck@1313 | 465 | |
jfranck@1313 | 466 | origAnnoType = currentAnno.type; |
jfranck@1313 | 467 | if (arrayOfOrigAnnoType == null) { |
jfranck@1313 | 468 | arrayOfOrigAnnoType = types.makeArrayType(origAnnoType); |
jjg@1521 | 469 | } |
jfranck@1313 | 470 | |
jfranck@1629 | 471 | // Only report errors if this isn't the first occurrence I.E. count > 1 |
jfranck@1629 | 472 | boolean reportError = count > 1; |
jfranck@1629 | 473 | Type currentContainerType = getContainingType(currentAnno, ctx.pos.get(currentAnno), reportError); |
jfranck@1313 | 474 | if (currentContainerType == null) { |
jfranck@1313 | 475 | continue; |
jfranck@1313 | 476 | } |
jfranck@1313 | 477 | // Assert that the target Container is == for all repeated |
jfranck@1313 | 478 | // annos of the same annotation type, the types should |
jfranck@1313 | 479 | // come from the same Symbol, i.e. be '==' |
jfranck@1313 | 480 | Assert.check(targetContainerType == null || currentContainerType == targetContainerType); |
jfranck@1313 | 481 | targetContainerType = currentContainerType; |
jfranck@1313 | 482 | |
jfranck@1313 | 483 | containerValueSymbol = validateContainer(targetContainerType, origAnnoType, ctx.pos.get(currentAnno)); |
jfranck@1313 | 484 | |
jfranck@1313 | 485 | if (containerValueSymbol == null) { // Check of CA type failed |
jfranck@1313 | 486 | // errors are already reported |
jfranck@1313 | 487 | continue; |
jfranck@1313 | 488 | } |
jfranck@1313 | 489 | |
jfranck@1313 | 490 | repeated = repeated.prepend(currentAnno); |
jfranck@1313 | 491 | } |
jfranck@1313 | 492 | |
jfranck@1313 | 493 | if (!repeated.isEmpty()) { |
jfranck@1313 | 494 | repeated = repeated.reverse(); |
jfranck@1313 | 495 | TreeMaker m = make.at(ctx.pos.get(firstOccurrence)); |
jfranck@1313 | 496 | Pair<MethodSymbol, Attribute> p = |
jfranck@1313 | 497 | new Pair<MethodSymbol, Attribute>(containerValueSymbol, |
jfranck@1313 | 498 | new Attribute.Array(arrayOfOrigAnnoType, repeated)); |
jjg@1521 | 499 | if (ctx.isTypeCompound) { |
jjg@1521 | 500 | /* TODO: the following code would be cleaner: |
jjg@1521 | 501 | Attribute.TypeCompound at = new Attribute.TypeCompound(targetContainerType, List.of(p), |
jjg@1521 | 502 | ((Attribute.TypeCompound)annotations.head).position); |
jjg@1521 | 503 | JCTypeAnnotation annoTree = m.TypeAnnotation(at); |
jjg@1521 | 504 | at = enterTypeAnnotation(annoTree, targetContainerType, ctx.env); |
jjg@1521 | 505 | */ |
jjg@1521 | 506 | // However, we directly construct the TypeCompound to keep the |
jjg@1521 | 507 | // direct relation to the contained TypeCompounds. |
jjg@1521 | 508 | Attribute.TypeCompound at = new Attribute.TypeCompound(targetContainerType, List.of(p), |
jjg@1521 | 509 | ((Attribute.TypeCompound)annotations.head).position); |
jfranck@1445 | 510 | |
jjg@1521 | 511 | // TODO: annotation applicability checks from below? |
jfranck@1445 | 512 | |
jjg@1521 | 513 | at.setSynthesized(true); |
jfranck@1445 | 514 | |
jjg@1521 | 515 | @SuppressWarnings("unchecked") |
jjg@1521 | 516 | T x = (T) at; |
jjg@1521 | 517 | return x; |
jjg@1521 | 518 | } else { |
jjg@1521 | 519 | Attribute.Compound c = new Attribute.Compound(targetContainerType, List.of(p)); |
jjg@1521 | 520 | JCAnnotation annoTree = m.Annotation(c); |
jjg@1521 | 521 | |
jjg@1521 | 522 | if (!chk.annotationApplicable(annoTree, on)) |
jjg@1521 | 523 | log.error(annoTree.pos(), "invalid.repeatable.annotation.incompatible.target", targetContainerType, origAnnoType); |
jjg@1521 | 524 | |
jjg@1521 | 525 | if (!chk.validateAnnotationDeferErrors(annoTree)) |
jjg@1521 | 526 | log.error(annoTree.pos(), "duplicate.annotation.invalid.repeated", origAnnoType); |
jjg@1521 | 527 | |
jjg@1521 | 528 | c = enterAnnotation(annoTree, targetContainerType, ctx.env); |
jjg@1521 | 529 | c.setSynthesized(true); |
jjg@1521 | 530 | |
jjg@1521 | 531 | @SuppressWarnings("unchecked") |
jjg@1521 | 532 | T x = (T) c; |
jjg@1521 | 533 | return x; |
jjg@1521 | 534 | } |
jfranck@1313 | 535 | } else { |
jfranck@1313 | 536 | return null; // errors should have been reported elsewhere |
jfranck@1313 | 537 | } |
jfranck@1313 | 538 | } |
jfranck@1313 | 539 | |
jfranck@1313 | 540 | /** Fetches the actual Type that should be the containing annotation. */ |
jfranck@1313 | 541 | private Type getContainingType(Attribute.Compound currentAnno, |
jfranck@1629 | 542 | DiagnosticPosition pos, |
jfranck@1629 | 543 | boolean reportError) |
jfranck@1313 | 544 | { |
jfranck@1313 | 545 | Type origAnnoType = currentAnno.type; |
jfranck@1313 | 546 | TypeSymbol origAnnoDecl = origAnnoType.tsym; |
jfranck@1313 | 547 | |
jjg@1492 | 548 | // Fetch the Repeatable annotation from the current |
jfranck@1313 | 549 | // annotation's declaration, or null if it has none |
jjg@1492 | 550 | Attribute.Compound ca = origAnnoDecl.attribute(syms.repeatableType.tsym); |
jjg@1492 | 551 | if (ca == null) { // has no Repeatable annotation |
jfranck@1629 | 552 | if (reportError) |
jfranck@1629 | 553 | log.error(pos, "duplicate.annotation.missing.container", origAnnoType, syms.repeatableType); |
jfranck@1313 | 554 | return null; |
jfranck@1313 | 555 | } |
jfranck@1313 | 556 | |
jfranck@1313 | 557 | return filterSame(extractContainingType(ca, pos, origAnnoDecl), |
jfranck@1313 | 558 | origAnnoType); |
jfranck@1313 | 559 | } |
jfranck@1313 | 560 | |
jfranck@1313 | 561 | // returns null if t is same as 's', returns 't' otherwise |
jfranck@1313 | 562 | private Type filterSame(Type t, Type s) { |
jfranck@1313 | 563 | if (t == null || s == null) { |
jfranck@1313 | 564 | return t; |
jfranck@1313 | 565 | } |
jfranck@1313 | 566 | |
jfranck@1313 | 567 | return types.isSameType(t, s) ? null : t; |
jfranck@1313 | 568 | } |
jfranck@1313 | 569 | |
jfranck@1313 | 570 | /** Extract the actual Type to be used for a containing annotation. */ |
jfranck@1313 | 571 | private Type extractContainingType(Attribute.Compound ca, |
jfranck@1313 | 572 | DiagnosticPosition pos, |
jfranck@1313 | 573 | TypeSymbol annoDecl) |
jfranck@1313 | 574 | { |
jjg@1492 | 575 | // The next three checks check that the Repeatable annotation |
jfranck@1313 | 576 | // on the declaration of the annotation type that is repeating is |
jfranck@1313 | 577 | // valid. |
jfranck@1313 | 578 | |
jjg@1492 | 579 | // Repeatable must have at least one element |
jfranck@1313 | 580 | if (ca.values.isEmpty()) { |
jjg@1492 | 581 | log.error(pos, "invalid.repeatable.annotation", annoDecl); |
jfranck@1313 | 582 | return null; |
jfranck@1313 | 583 | } |
jfranck@1313 | 584 | Pair<MethodSymbol,Attribute> p = ca.values.head; |
jfranck@1313 | 585 | Name name = p.fst.name; |
jfranck@1313 | 586 | if (name != names.value) { // should contain only one element, named "value" |
jjg@1492 | 587 | log.error(pos, "invalid.repeatable.annotation", annoDecl); |
jfranck@1313 | 588 | return null; |
jfranck@1313 | 589 | } |
jfranck@1313 | 590 | if (!(p.snd instanceof Attribute.Class)) { // check that the value of "value" is an Attribute.Class |
jjg@1492 | 591 | log.error(pos, "invalid.repeatable.annotation", annoDecl); |
jfranck@1313 | 592 | return null; |
jfranck@1313 | 593 | } |
jfranck@1313 | 594 | |
jfranck@1313 | 595 | return ((Attribute.Class)p.snd).getValue(); |
jfranck@1313 | 596 | } |
jfranck@1313 | 597 | |
jfranck@1313 | 598 | /* Validate that the suggested targetContainerType Type is a valid |
jfranck@1313 | 599 | * container type for repeated instances of originalAnnoType |
jfranck@1313 | 600 | * annotations. Return null and report errors if this is not the |
jfranck@1313 | 601 | * case, return the MethodSymbol of the value element in |
jfranck@1313 | 602 | * targetContainerType if it is suitable (this is needed to |
jfranck@1313 | 603 | * synthesize the container). */ |
jfranck@1313 | 604 | private MethodSymbol validateContainer(Type targetContainerType, |
jfranck@1313 | 605 | Type originalAnnoType, |
jfranck@1313 | 606 | DiagnosticPosition pos) { |
jfranck@1313 | 607 | MethodSymbol containerValueSymbol = null; |
jfranck@1313 | 608 | boolean fatalError = false; |
jfranck@1313 | 609 | |
jfranck@1313 | 610 | // Validate that there is a (and only 1) value method |
jfranck@1313 | 611 | Scope scope = targetContainerType.tsym.members(); |
jfranck@1313 | 612 | int nr_value_elems = 0; |
jfranck@1313 | 613 | boolean error = false; |
jfranck@1313 | 614 | for(Symbol elm : scope.getElementsByName(names.value)) { |
jfranck@1313 | 615 | nr_value_elems++; |
jfranck@1313 | 616 | |
jfranck@1313 | 617 | if (nr_value_elems == 1 && |
jfranck@1313 | 618 | elm.kind == Kinds.MTH) { |
jfranck@1313 | 619 | containerValueSymbol = (MethodSymbol)elm; |
jfranck@1313 | 620 | } else { |
jfranck@1313 | 621 | error = true; |
jfranck@1313 | 622 | } |
jfranck@1313 | 623 | } |
jfranck@1313 | 624 | if (error) { |
jfranck@1313 | 625 | log.error(pos, |
jjg@1492 | 626 | "invalid.repeatable.annotation.multiple.values", |
jfranck@1313 | 627 | targetContainerType, |
jfranck@1313 | 628 | nr_value_elems); |
jfranck@1313 | 629 | return null; |
jfranck@1313 | 630 | } else if (nr_value_elems == 0) { |
jfranck@1313 | 631 | log.error(pos, |
jjg@1492 | 632 | "invalid.repeatable.annotation.no.value", |
jfranck@1313 | 633 | targetContainerType); |
jfranck@1313 | 634 | return null; |
jfranck@1313 | 635 | } |
jfranck@1313 | 636 | |
jfranck@1313 | 637 | // validate that the 'value' element is a method |
jfranck@1313 | 638 | // probably "impossible" to fail this |
jfranck@1313 | 639 | if (containerValueSymbol.kind != Kinds.MTH) { |
jfranck@1313 | 640 | log.error(pos, |
jjg@1492 | 641 | "invalid.repeatable.annotation.invalid.value", |
jfranck@1313 | 642 | targetContainerType); |
jfranck@1313 | 643 | fatalError = true; |
jfranck@1313 | 644 | } |
jfranck@1313 | 645 | |
jfranck@1313 | 646 | // validate that the 'value' element has the correct return type |
jfranck@1313 | 647 | // i.e. array of original anno |
jfranck@1313 | 648 | Type valueRetType = containerValueSymbol.type.getReturnType(); |
jfranck@1313 | 649 | Type expectedType = types.makeArrayType(originalAnnoType); |
jfranck@1313 | 650 | if (!(types.isArray(valueRetType) && |
jfranck@1313 | 651 | types.isSameType(expectedType, valueRetType))) { |
jfranck@1313 | 652 | log.error(pos, |
jjg@1492 | 653 | "invalid.repeatable.annotation.value.return", |
jfranck@1313 | 654 | targetContainerType, |
jfranck@1313 | 655 | valueRetType, |
jfranck@1313 | 656 | expectedType); |
jfranck@1313 | 657 | fatalError = true; |
jfranck@1313 | 658 | } |
jfranck@1313 | 659 | if (error) { |
jfranck@1313 | 660 | fatalError = true; |
jfranck@1313 | 661 | } |
jfranck@1313 | 662 | |
jjg@1492 | 663 | // The conditions for a valid containing annotation are made |
jfranck@1313 | 664 | // in Check.validateRepeatedAnnotaton(); |
jfranck@1313 | 665 | |
jfranck@1313 | 666 | return fatalError ? null : containerValueSymbol; |
jfranck@1313 | 667 | } |
jfranck@1313 | 668 | } |