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

Tue, 14 May 2013 15:04:06 -0700

author
jjg
date
Tue, 14 May 2013 15:04:06 -0700
changeset 1755
ddb4a2bfcd82
parent 1629
f427043f8c65
child 1914
0a9f5cbe37d9
permissions
-rw-r--r--

8013852: update reference impl for type-annotations
Reviewed-by: jjg
Contributed-by: wdietl@gmail.com, steve.sides@oracle.com, joel.franck@oracle.com, alex.buckley@oracle.com

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;
duke@1 276 Symbol method = rs.resolveQualifiedMethod(left.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);
duke@1 335 if (result.isErroneous())
duke@1 336 return new Attribute.Error(expected);
duke@1 337 if (TreeInfo.name(tree) != names._class) {
duke@1 338 log.error(tree.pos(), "annotation.value.must.be.class.literal");
duke@1 339 return new Attribute.Error(expected);
duke@1 340 }
duke@1 341 return new Attribute.Class(types,
duke@1 342 (((JCFieldAccess) tree).selected).type);
duke@1 343 }
duke@1 344 if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
jjg@1127 345 if (!tree.hasTag(ANNOTATION)) {
duke@1 346 log.error(tree.pos(), "annotation.value.must.be.annotation");
duke@1 347 expected = syms.errorType;
duke@1 348 }
duke@1 349 return enterAnnotation((JCAnnotation)tree, expected, env);
duke@1 350 }
jjg@1374 351 if (expected.hasTag(ARRAY)) { // should really be isArray()
jjg@1127 352 if (!tree.hasTag(NEWARRAY)) {
duke@1 353 tree = make.at(tree.pos).
duke@1 354 NewArray(null, List.<JCExpression>nil(), List.of(tree));
duke@1 355 }
duke@1 356 JCNewArray na = (JCNewArray)tree;
duke@1 357 if (na.elemtype != null) {
duke@1 358 log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
duke@1 359 return new Attribute.Error(expected);
duke@1 360 }
duke@1 361 ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
duke@1 362 for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
duke@1 363 buf.append(enterAttributeValue(types.elemtype(expected),
duke@1 364 l.head,
duke@1 365 env));
duke@1 366 }
mcimadamore@676 367 na.type = expected;
duke@1 368 return new Attribute.
duke@1 369 Array(expected, buf.toArray(new Attribute[buf.length()]));
duke@1 370 }
jjg@1374 371 if (expected.hasTag(CLASS) &&
duke@1 372 (expected.tsym.flags() & Flags.ENUM) != 0) {
duke@1 373 attr.attribExpr(tree, env, expected);
duke@1 374 Symbol sym = TreeInfo.symbol(tree);
duke@1 375 if (sym == null ||
duke@1 376 TreeInfo.nonstaticSelect(tree) ||
duke@1 377 sym.kind != Kinds.VAR ||
duke@1 378 (sym.flags() & Flags.ENUM) == 0) {
duke@1 379 log.error(tree.pos(), "enum.annotation.must.be.enum.constant");
duke@1 380 return new Attribute.Error(expected);
duke@1 381 }
duke@1 382 VarSymbol enumerator = (VarSymbol) sym;
duke@1 383 return new Attribute.Enum(expected, enumerator);
duke@1 384 }
duke@1 385 if (!expected.isErroneous())
duke@1 386 log.error(tree.pos(), "annotation.value.not.allowable.type");
duke@1 387 return new Attribute.Error(attr.attribExpr(tree, env, expected));
duke@1 388 }
jfranck@1313 389
jfranck@1313 390 /* *********************************
jfranck@1313 391 * Support for repeating annotations
jfranck@1313 392 ***********************************/
jfranck@1313 393
jfranck@1313 394 /* Process repeated annotations. This method returns the
jfranck@1313 395 * synthesized container annotation or null IFF all repeating
jfranck@1313 396 * annotation are invalid. This method reports errors/warnings.
jfranck@1313 397 */
jjg@1521 398 private <T extends Attribute.Compound> T processRepeatedAnnotations(List<T> annotations,
jjg@1521 399 AnnotateRepeatedContext<T> ctx,
jjg@1521 400 Symbol on) {
jjg@1521 401 T firstOccurrence = annotations.head;
jfranck@1313 402 List<Attribute> repeated = List.nil();
jfranck@1445 403 Type origAnnoType = null;
jfranck@1313 404 Type arrayOfOrigAnnoType = null;
jfranck@1313 405 Type targetContainerType = null;
jfranck@1313 406 MethodSymbol containerValueSymbol = null;
jfranck@1313 407
jfranck@1313 408 Assert.check(!annotations.isEmpty() &&
jfranck@1313 409 !annotations.tail.isEmpty()); // i.e. size() > 1
jfranck@1313 410
jfranck@1629 411 int count = 0;
jjg@1521 412 for (List<T> al = annotations;
jfranck@1313 413 !al.isEmpty();
jfranck@1313 414 al = al.tail)
jfranck@1313 415 {
jfranck@1629 416 count++;
jfranck@1629 417
jfranck@1629 418 // There must be more than a single anno in the annotation list
jfranck@1629 419 Assert.check(count > 1 || !al.tail.isEmpty());
jfranck@1629 420
jjg@1521 421 T currentAnno = al.head;
jfranck@1313 422
jfranck@1313 423 origAnnoType = currentAnno.type;
jfranck@1313 424 if (arrayOfOrigAnnoType == null) {
jfranck@1313 425 arrayOfOrigAnnoType = types.makeArrayType(origAnnoType);
jjg@1521 426 }
jfranck@1313 427
jfranck@1629 428 // Only report errors if this isn't the first occurrence I.E. count > 1
jfranck@1629 429 boolean reportError = count > 1;
jfranck@1629 430 Type currentContainerType = getContainingType(currentAnno, ctx.pos.get(currentAnno), reportError);
jfranck@1313 431 if (currentContainerType == null) {
jfranck@1313 432 continue;
jfranck@1313 433 }
jfranck@1313 434 // Assert that the target Container is == for all repeated
jfranck@1313 435 // annos of the same annotation type, the types should
jfranck@1313 436 // come from the same Symbol, i.e. be '=='
jfranck@1313 437 Assert.check(targetContainerType == null || currentContainerType == targetContainerType);
jfranck@1313 438 targetContainerType = currentContainerType;
jfranck@1313 439
jfranck@1313 440 containerValueSymbol = validateContainer(targetContainerType, origAnnoType, ctx.pos.get(currentAnno));
jfranck@1313 441
jfranck@1313 442 if (containerValueSymbol == null) { // Check of CA type failed
jfranck@1313 443 // errors are already reported
jfranck@1313 444 continue;
jfranck@1313 445 }
jfranck@1313 446
jfranck@1313 447 repeated = repeated.prepend(currentAnno);
jfranck@1313 448 }
jfranck@1313 449
jfranck@1313 450 if (!repeated.isEmpty()) {
jfranck@1313 451 repeated = repeated.reverse();
jfranck@1313 452 TreeMaker m = make.at(ctx.pos.get(firstOccurrence));
jfranck@1313 453 Pair<MethodSymbol, Attribute> p =
jfranck@1313 454 new Pair<MethodSymbol, Attribute>(containerValueSymbol,
jfranck@1313 455 new Attribute.Array(arrayOfOrigAnnoType, repeated));
jjg@1521 456 if (ctx.isTypeCompound) {
jjg@1521 457 /* TODO: the following code would be cleaner:
jjg@1521 458 Attribute.TypeCompound at = new Attribute.TypeCompound(targetContainerType, List.of(p),
jjg@1521 459 ((Attribute.TypeCompound)annotations.head).position);
jjg@1521 460 JCTypeAnnotation annoTree = m.TypeAnnotation(at);
jjg@1521 461 at = enterTypeAnnotation(annoTree, targetContainerType, ctx.env);
jjg@1521 462 */
jjg@1521 463 // However, we directly construct the TypeCompound to keep the
jjg@1521 464 // direct relation to the contained TypeCompounds.
jjg@1521 465 Attribute.TypeCompound at = new Attribute.TypeCompound(targetContainerType, List.of(p),
jjg@1521 466 ((Attribute.TypeCompound)annotations.head).position);
jfranck@1445 467
jjg@1521 468 // TODO: annotation applicability checks from below?
jfranck@1445 469
jjg@1521 470 at.setSynthesized(true);
jfranck@1445 471
jjg@1521 472 @SuppressWarnings("unchecked")
jjg@1521 473 T x = (T) at;
jjg@1521 474 return x;
jjg@1521 475 } else {
jjg@1521 476 Attribute.Compound c = new Attribute.Compound(targetContainerType, List.of(p));
jjg@1521 477 JCAnnotation annoTree = m.Annotation(c);
jjg@1521 478
jjg@1521 479 if (!chk.annotationApplicable(annoTree, on))
jjg@1521 480 log.error(annoTree.pos(), "invalid.repeatable.annotation.incompatible.target", targetContainerType, origAnnoType);
jjg@1521 481
jjg@1521 482 if (!chk.validateAnnotationDeferErrors(annoTree))
jjg@1521 483 log.error(annoTree.pos(), "duplicate.annotation.invalid.repeated", origAnnoType);
jjg@1521 484
jjg@1521 485 c = enterAnnotation(annoTree, targetContainerType, ctx.env);
jjg@1521 486 c.setSynthesized(true);
jjg@1521 487
jjg@1521 488 @SuppressWarnings("unchecked")
jjg@1521 489 T x = (T) c;
jjg@1521 490 return x;
jjg@1521 491 }
jfranck@1313 492 } else {
jfranck@1313 493 return null; // errors should have been reported elsewhere
jfranck@1313 494 }
jfranck@1313 495 }
jfranck@1313 496
jfranck@1313 497 /** Fetches the actual Type that should be the containing annotation. */
jfranck@1313 498 private Type getContainingType(Attribute.Compound currentAnno,
jfranck@1629 499 DiagnosticPosition pos,
jfranck@1629 500 boolean reportError)
jfranck@1313 501 {
jfranck@1313 502 Type origAnnoType = currentAnno.type;
jfranck@1313 503 TypeSymbol origAnnoDecl = origAnnoType.tsym;
jfranck@1313 504
jjg@1492 505 // Fetch the Repeatable annotation from the current
jfranck@1313 506 // annotation's declaration, or null if it has none
jjg@1492 507 Attribute.Compound ca = origAnnoDecl.attribute(syms.repeatableType.tsym);
jjg@1492 508 if (ca == null) { // has no Repeatable annotation
jfranck@1629 509 if (reportError)
jfranck@1629 510 log.error(pos, "duplicate.annotation.missing.container", origAnnoType, syms.repeatableType);
jfranck@1313 511 return null;
jfranck@1313 512 }
jfranck@1313 513
jfranck@1313 514 return filterSame(extractContainingType(ca, pos, origAnnoDecl),
jfranck@1313 515 origAnnoType);
jfranck@1313 516 }
jfranck@1313 517
jfranck@1313 518 // returns null if t is same as 's', returns 't' otherwise
jfranck@1313 519 private Type filterSame(Type t, Type s) {
jfranck@1313 520 if (t == null || s == null) {
jfranck@1313 521 return t;
jfranck@1313 522 }
jfranck@1313 523
jfranck@1313 524 return types.isSameType(t, s) ? null : t;
jfranck@1313 525 }
jfranck@1313 526
jfranck@1313 527 /** Extract the actual Type to be used for a containing annotation. */
jfranck@1313 528 private Type extractContainingType(Attribute.Compound ca,
jfranck@1313 529 DiagnosticPosition pos,
jfranck@1313 530 TypeSymbol annoDecl)
jfranck@1313 531 {
jjg@1492 532 // The next three checks check that the Repeatable annotation
jfranck@1313 533 // on the declaration of the annotation type that is repeating is
jfranck@1313 534 // valid.
jfranck@1313 535
jjg@1492 536 // Repeatable must have at least one element
jfranck@1313 537 if (ca.values.isEmpty()) {
jjg@1492 538 log.error(pos, "invalid.repeatable.annotation", annoDecl);
jfranck@1313 539 return null;
jfranck@1313 540 }
jfranck@1313 541 Pair<MethodSymbol,Attribute> p = ca.values.head;
jfranck@1313 542 Name name = p.fst.name;
jfranck@1313 543 if (name != names.value) { // should contain only one element, named "value"
jjg@1492 544 log.error(pos, "invalid.repeatable.annotation", annoDecl);
jfranck@1313 545 return null;
jfranck@1313 546 }
jfranck@1313 547 if (!(p.snd instanceof Attribute.Class)) { // check that the value of "value" is an Attribute.Class
jjg@1492 548 log.error(pos, "invalid.repeatable.annotation", annoDecl);
jfranck@1313 549 return null;
jfranck@1313 550 }
jfranck@1313 551
jfranck@1313 552 return ((Attribute.Class)p.snd).getValue();
jfranck@1313 553 }
jfranck@1313 554
jfranck@1313 555 /* Validate that the suggested targetContainerType Type is a valid
jfranck@1313 556 * container type for repeated instances of originalAnnoType
jfranck@1313 557 * annotations. Return null and report errors if this is not the
jfranck@1313 558 * case, return the MethodSymbol of the value element in
jfranck@1313 559 * targetContainerType if it is suitable (this is needed to
jfranck@1313 560 * synthesize the container). */
jfranck@1313 561 private MethodSymbol validateContainer(Type targetContainerType,
jfranck@1313 562 Type originalAnnoType,
jfranck@1313 563 DiagnosticPosition pos) {
jfranck@1313 564 MethodSymbol containerValueSymbol = null;
jfranck@1313 565 boolean fatalError = false;
jfranck@1313 566
jfranck@1313 567 // Validate that there is a (and only 1) value method
jfranck@1313 568 Scope scope = targetContainerType.tsym.members();
jfranck@1313 569 int nr_value_elems = 0;
jfranck@1313 570 boolean error = false;
jfranck@1313 571 for(Symbol elm : scope.getElementsByName(names.value)) {
jfranck@1313 572 nr_value_elems++;
jfranck@1313 573
jfranck@1313 574 if (nr_value_elems == 1 &&
jfranck@1313 575 elm.kind == Kinds.MTH) {
jfranck@1313 576 containerValueSymbol = (MethodSymbol)elm;
jfranck@1313 577 } else {
jfranck@1313 578 error = true;
jfranck@1313 579 }
jfranck@1313 580 }
jfranck@1313 581 if (error) {
jfranck@1313 582 log.error(pos,
jjg@1492 583 "invalid.repeatable.annotation.multiple.values",
jfranck@1313 584 targetContainerType,
jfranck@1313 585 nr_value_elems);
jfranck@1313 586 return null;
jfranck@1313 587 } else if (nr_value_elems == 0) {
jfranck@1313 588 log.error(pos,
jjg@1492 589 "invalid.repeatable.annotation.no.value",
jfranck@1313 590 targetContainerType);
jfranck@1313 591 return null;
jfranck@1313 592 }
jfranck@1313 593
jfranck@1313 594 // validate that the 'value' element is a method
jfranck@1313 595 // probably "impossible" to fail this
jfranck@1313 596 if (containerValueSymbol.kind != Kinds.MTH) {
jfranck@1313 597 log.error(pos,
jjg@1492 598 "invalid.repeatable.annotation.invalid.value",
jfranck@1313 599 targetContainerType);
jfranck@1313 600 fatalError = true;
jfranck@1313 601 }
jfranck@1313 602
jfranck@1313 603 // validate that the 'value' element has the correct return type
jfranck@1313 604 // i.e. array of original anno
jfranck@1313 605 Type valueRetType = containerValueSymbol.type.getReturnType();
jfranck@1313 606 Type expectedType = types.makeArrayType(originalAnnoType);
jfranck@1313 607 if (!(types.isArray(valueRetType) &&
jfranck@1313 608 types.isSameType(expectedType, valueRetType))) {
jfranck@1313 609 log.error(pos,
jjg@1492 610 "invalid.repeatable.annotation.value.return",
jfranck@1313 611 targetContainerType,
jfranck@1313 612 valueRetType,
jfranck@1313 613 expectedType);
jfranck@1313 614 fatalError = true;
jfranck@1313 615 }
jfranck@1313 616 if (error) {
jfranck@1313 617 fatalError = true;
jfranck@1313 618 }
jfranck@1313 619
jjg@1492 620 // The conditions for a valid containing annotation are made
jfranck@1313 621 // in Check.validateRepeatedAnnotaton();
jfranck@1313 622
jfranck@1313 623 return fatalError ? null : containerValueSymbol;
jfranck@1313 624 }
jfranck@1313 625 }

mercurial