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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial