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

Tue, 20 Aug 2013 17:21:47 +0200

author
jfranck
date
Tue, 20 Aug 2013 17:21:47 +0200
changeset 1960
e811fb09a1dc
parent 1914
0a9f5cbe37d9
child 2070
b7d8b71e1658
permissions
-rw-r--r--

8019243: AnnotationTypeMismatchException instead of MirroredTypeException
Reviewed-by: jjg

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

mercurial