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

Tue, 12 Mar 2013 17:39:34 +0100

author
jfranck
date
Tue, 12 Mar 2013 17:39:34 +0100
changeset 1629
f427043f8c65
parent 1521
71f35e4b93a5
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

7196531: Duplicate error messages on repeating annotations
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) {
duke@1 219 // The annotation might have had its type attributed (but not checked)
duke@1 220 // by attr.attribAnnotationTypes during MemberEnter, in which case we do not
duke@1 221 // need to do it again.
duke@1 222 Type at = (a.annotationType.type != null ? a.annotationType.type
duke@1 223 : attr.attribType(a.annotationType, env));
duke@1 224 a.type = chk.checkType(a.annotationType.pos(), at, expected);
duke@1 225 if (a.type.isErroneous())
duke@1 226 return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
duke@1 227 if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) {
duke@1 228 log.error(a.annotationType.pos(),
duke@1 229 "not.annotation.type", a.type.toString());
duke@1 230 return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil());
duke@1 231 }
duke@1 232 List<JCExpression> args = a.args;
jjg@1127 233 if (args.length() == 1 && !args.head.hasTag(ASSIGN)) {
duke@1 234 // special case: elided "value=" assumed
duke@1 235 args.head = make.at(args.head.pos).
duke@1 236 Assign(make.Ident(names.value), args.head);
duke@1 237 }
duke@1 238 ListBuffer<Pair<MethodSymbol,Attribute>> buf =
duke@1 239 new ListBuffer<Pair<MethodSymbol,Attribute>>();
duke@1 240 for (List<JCExpression> tl = args; tl.nonEmpty(); tl = tl.tail) {
duke@1 241 JCExpression t = tl.head;
jjg@1127 242 if (!t.hasTag(ASSIGN)) {
duke@1 243 log.error(t.pos(), "annotation.value.must.be.name.value");
duke@1 244 continue;
duke@1 245 }
duke@1 246 JCAssign assign = (JCAssign)t;
jjg@1127 247 if (!assign.lhs.hasTag(IDENT)) {
duke@1 248 log.error(t.pos(), "annotation.value.must.be.name.value");
duke@1 249 continue;
duke@1 250 }
duke@1 251 JCIdent left = (JCIdent)assign.lhs;
duke@1 252 Symbol method = rs.resolveQualifiedMethod(left.pos(),
mcimadamore@992 253 env,
mcimadamore@992 254 a.type,
mcimadamore@992 255 left.name,
mcimadamore@992 256 List.<Type>nil(),
mcimadamore@992 257 null);
duke@1 258 left.sym = method;
duke@1 259 left.type = method.type;
duke@1 260 if (method.owner != a.type.tsym)
duke@1 261 log.error(left.pos(), "no.annotation.member", left.name, a.type);
duke@1 262 Type result = method.type.getReturnType();
duke@1 263 Attribute value = enterAttributeValue(result, assign.rhs, env);
duke@1 264 if (!method.type.isErroneous())
duke@1 265 buf.append(new Pair<MethodSymbol,Attribute>
duke@1 266 ((MethodSymbol)method, value));
mcimadamore@676 267 t.type = result;
duke@1 268 }
jjg@1521 269 // TODO: this should be a TypeCompound if "a" is a JCTypeAnnotation.
jjg@1521 270 // However, how do we find the correct position?
jjg@1521 271 Attribute.Compound ac = new Attribute.Compound(a.type, buf.toList());
jjg@1521 272 // TODO: is this something we want? Who would use it?
jjg@1521 273 // a.attribute = ac;
jjg@1521 274 return ac;
duke@1 275 }
duke@1 276
duke@1 277 Attribute enterAttributeValue(Type expected,
duke@1 278 JCExpression tree,
duke@1 279 Env<AttrContext> env) {
mcimadamore@992 280 //first, try completing the attribution value sym - if a completion
mcimadamore@992 281 //error is thrown, we should recover gracefully, and display an
mcimadamore@992 282 //ordinary resolution diagnostic.
mcimadamore@992 283 try {
mcimadamore@992 284 expected.tsym.complete();
mcimadamore@992 285 } catch(CompletionFailure e) {
mcimadamore@992 286 log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym);
mcimadamore@992 287 return new Attribute.Error(expected);
mcimadamore@992 288 }
duke@1 289 if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) {
duke@1 290 Type result = attr.attribExpr(tree, env, expected);
duke@1 291 if (result.isErroneous())
duke@1 292 return new Attribute.Error(expected);
duke@1 293 if (result.constValue() == null) {
duke@1 294 log.error(tree.pos(), "attribute.value.must.be.constant");
duke@1 295 return new Attribute.Error(expected);
duke@1 296 }
duke@1 297 result = cfolder.coerce(result, expected);
duke@1 298 return new Attribute.Constant(expected, result.constValue());
duke@1 299 }
duke@1 300 if (expected.tsym == syms.classType.tsym) {
duke@1 301 Type result = attr.attribExpr(tree, env, expected);
duke@1 302 if (result.isErroneous())
duke@1 303 return new Attribute.Error(expected);
duke@1 304 if (TreeInfo.name(tree) != names._class) {
duke@1 305 log.error(tree.pos(), "annotation.value.must.be.class.literal");
duke@1 306 return new Attribute.Error(expected);
duke@1 307 }
duke@1 308 return new Attribute.Class(types,
duke@1 309 (((JCFieldAccess) tree).selected).type);
duke@1 310 }
duke@1 311 if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
jjg@1127 312 if (!tree.hasTag(ANNOTATION)) {
duke@1 313 log.error(tree.pos(), "annotation.value.must.be.annotation");
duke@1 314 expected = syms.errorType;
duke@1 315 }
duke@1 316 return enterAnnotation((JCAnnotation)tree, expected, env);
duke@1 317 }
jjg@1374 318 if (expected.hasTag(ARRAY)) { // should really be isArray()
jjg@1127 319 if (!tree.hasTag(NEWARRAY)) {
duke@1 320 tree = make.at(tree.pos).
duke@1 321 NewArray(null, List.<JCExpression>nil(), List.of(tree));
duke@1 322 }
duke@1 323 JCNewArray na = (JCNewArray)tree;
duke@1 324 if (na.elemtype != null) {
duke@1 325 log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
duke@1 326 return new Attribute.Error(expected);
duke@1 327 }
duke@1 328 ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
duke@1 329 for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
duke@1 330 buf.append(enterAttributeValue(types.elemtype(expected),
duke@1 331 l.head,
duke@1 332 env));
duke@1 333 }
mcimadamore@676 334 na.type = expected;
duke@1 335 return new Attribute.
duke@1 336 Array(expected, buf.toArray(new Attribute[buf.length()]));
duke@1 337 }
jjg@1374 338 if (expected.hasTag(CLASS) &&
duke@1 339 (expected.tsym.flags() & Flags.ENUM) != 0) {
duke@1 340 attr.attribExpr(tree, env, expected);
duke@1 341 Symbol sym = TreeInfo.symbol(tree);
duke@1 342 if (sym == null ||
duke@1 343 TreeInfo.nonstaticSelect(tree) ||
duke@1 344 sym.kind != Kinds.VAR ||
duke@1 345 (sym.flags() & Flags.ENUM) == 0) {
duke@1 346 log.error(tree.pos(), "enum.annotation.must.be.enum.constant");
duke@1 347 return new Attribute.Error(expected);
duke@1 348 }
duke@1 349 VarSymbol enumerator = (VarSymbol) sym;
duke@1 350 return new Attribute.Enum(expected, enumerator);
duke@1 351 }
duke@1 352 if (!expected.isErroneous())
duke@1 353 log.error(tree.pos(), "annotation.value.not.allowable.type");
duke@1 354 return new Attribute.Error(attr.attribExpr(tree, env, expected));
duke@1 355 }
jfranck@1313 356
jjg@1521 357 Attribute.TypeCompound enterTypeAnnotation(JCAnnotation a,
jjg@1521 358 Type expected,
jjg@1521 359 Env<AttrContext> env) {
jjg@1521 360 Attribute.Compound c = enterAnnotation(a, expected, env);
jjg@1521 361 Attribute.TypeCompound tc = new Attribute.TypeCompound(c.type, c.values, new TypeAnnotationPosition());
jjg@1521 362 a.attribute = tc;
jjg@1521 363 return tc;
jjg@1521 364 }
jjg@1521 365
jfranck@1313 366 /* *********************************
jfranck@1313 367 * Support for repeating annotations
jfranck@1313 368 ***********************************/
jfranck@1313 369
jfranck@1313 370 /* Process repeated annotations. This method returns the
jfranck@1313 371 * synthesized container annotation or null IFF all repeating
jfranck@1313 372 * annotation are invalid. This method reports errors/warnings.
jfranck@1313 373 */
jjg@1521 374 private <T extends Attribute.Compound> T processRepeatedAnnotations(List<T> annotations,
jjg@1521 375 AnnotateRepeatedContext<T> ctx,
jjg@1521 376 Symbol on) {
jjg@1521 377 T firstOccurrence = annotations.head;
jfranck@1313 378 List<Attribute> repeated = List.nil();
jfranck@1445 379 Type origAnnoType = null;
jfranck@1313 380 Type arrayOfOrigAnnoType = null;
jfranck@1313 381 Type targetContainerType = null;
jfranck@1313 382 MethodSymbol containerValueSymbol = null;
jfranck@1313 383
jfranck@1313 384 Assert.check(!annotations.isEmpty() &&
jfranck@1313 385 !annotations.tail.isEmpty()); // i.e. size() > 1
jfranck@1313 386
jfranck@1629 387 int count = 0;
jjg@1521 388 for (List<T> al = annotations;
jfranck@1313 389 !al.isEmpty();
jfranck@1313 390 al = al.tail)
jfranck@1313 391 {
jfranck@1629 392 count++;
jfranck@1629 393
jfranck@1629 394 // There must be more than a single anno in the annotation list
jfranck@1629 395 Assert.check(count > 1 || !al.tail.isEmpty());
jfranck@1629 396
jjg@1521 397 T currentAnno = al.head;
jfranck@1313 398
jfranck@1313 399 origAnnoType = currentAnno.type;
jfranck@1313 400 if (arrayOfOrigAnnoType == null) {
jfranck@1313 401 arrayOfOrigAnnoType = types.makeArrayType(origAnnoType);
jjg@1521 402 }
jfranck@1313 403
jfranck@1629 404 // Only report errors if this isn't the first occurrence I.E. count > 1
jfranck@1629 405 boolean reportError = count > 1;
jfranck@1629 406 Type currentContainerType = getContainingType(currentAnno, ctx.pos.get(currentAnno), reportError);
jfranck@1313 407 if (currentContainerType == null) {
jfranck@1313 408 continue;
jfranck@1313 409 }
jfranck@1313 410 // Assert that the target Container is == for all repeated
jfranck@1313 411 // annos of the same annotation type, the types should
jfranck@1313 412 // come from the same Symbol, i.e. be '=='
jfranck@1313 413 Assert.check(targetContainerType == null || currentContainerType == targetContainerType);
jfranck@1313 414 targetContainerType = currentContainerType;
jfranck@1313 415
jfranck@1313 416 containerValueSymbol = validateContainer(targetContainerType, origAnnoType, ctx.pos.get(currentAnno));
jfranck@1313 417
jfranck@1313 418 if (containerValueSymbol == null) { // Check of CA type failed
jfranck@1313 419 // errors are already reported
jfranck@1313 420 continue;
jfranck@1313 421 }
jfranck@1313 422
jfranck@1313 423 repeated = repeated.prepend(currentAnno);
jfranck@1313 424 }
jfranck@1313 425
jfranck@1313 426 if (!repeated.isEmpty()) {
jfranck@1313 427 repeated = repeated.reverse();
jfranck@1313 428 TreeMaker m = make.at(ctx.pos.get(firstOccurrence));
jfranck@1313 429 Pair<MethodSymbol, Attribute> p =
jfranck@1313 430 new Pair<MethodSymbol, Attribute>(containerValueSymbol,
jfranck@1313 431 new Attribute.Array(arrayOfOrigAnnoType, repeated));
jjg@1521 432 if (ctx.isTypeCompound) {
jjg@1521 433 /* TODO: the following code would be cleaner:
jjg@1521 434 Attribute.TypeCompound at = new Attribute.TypeCompound(targetContainerType, List.of(p),
jjg@1521 435 ((Attribute.TypeCompound)annotations.head).position);
jjg@1521 436 JCTypeAnnotation annoTree = m.TypeAnnotation(at);
jjg@1521 437 at = enterTypeAnnotation(annoTree, targetContainerType, ctx.env);
jjg@1521 438 */
jjg@1521 439 // However, we directly construct the TypeCompound to keep the
jjg@1521 440 // direct relation to the contained TypeCompounds.
jjg@1521 441 Attribute.TypeCompound at = new Attribute.TypeCompound(targetContainerType, List.of(p),
jjg@1521 442 ((Attribute.TypeCompound)annotations.head).position);
jfranck@1445 443
jjg@1521 444 // TODO: annotation applicability checks from below?
jfranck@1445 445
jjg@1521 446 at.setSynthesized(true);
jfranck@1445 447
jjg@1521 448 @SuppressWarnings("unchecked")
jjg@1521 449 T x = (T) at;
jjg@1521 450 return x;
jjg@1521 451 } else {
jjg@1521 452 Attribute.Compound c = new Attribute.Compound(targetContainerType, List.of(p));
jjg@1521 453 JCAnnotation annoTree = m.Annotation(c);
jjg@1521 454
jjg@1521 455 if (!chk.annotationApplicable(annoTree, on))
jjg@1521 456 log.error(annoTree.pos(), "invalid.repeatable.annotation.incompatible.target", targetContainerType, origAnnoType);
jjg@1521 457
jjg@1521 458 if (!chk.validateAnnotationDeferErrors(annoTree))
jjg@1521 459 log.error(annoTree.pos(), "duplicate.annotation.invalid.repeated", origAnnoType);
jjg@1521 460
jjg@1521 461 c = enterAnnotation(annoTree, targetContainerType, ctx.env);
jjg@1521 462 c.setSynthesized(true);
jjg@1521 463
jjg@1521 464 @SuppressWarnings("unchecked")
jjg@1521 465 T x = (T) c;
jjg@1521 466 return x;
jjg@1521 467 }
jfranck@1313 468 } else {
jfranck@1313 469 return null; // errors should have been reported elsewhere
jfranck@1313 470 }
jfranck@1313 471 }
jfranck@1313 472
jfranck@1313 473 /** Fetches the actual Type that should be the containing annotation. */
jfranck@1313 474 private Type getContainingType(Attribute.Compound currentAnno,
jfranck@1629 475 DiagnosticPosition pos,
jfranck@1629 476 boolean reportError)
jfranck@1313 477 {
jfranck@1313 478 Type origAnnoType = currentAnno.type;
jfranck@1313 479 TypeSymbol origAnnoDecl = origAnnoType.tsym;
jfranck@1313 480
jjg@1492 481 // Fetch the Repeatable annotation from the current
jfranck@1313 482 // annotation's declaration, or null if it has none
jjg@1492 483 Attribute.Compound ca = origAnnoDecl.attribute(syms.repeatableType.tsym);
jjg@1492 484 if (ca == null) { // has no Repeatable annotation
jfranck@1629 485 if (reportError)
jfranck@1629 486 log.error(pos, "duplicate.annotation.missing.container", origAnnoType, syms.repeatableType);
jfranck@1313 487 return null;
jfranck@1313 488 }
jfranck@1313 489
jfranck@1313 490 return filterSame(extractContainingType(ca, pos, origAnnoDecl),
jfranck@1313 491 origAnnoType);
jfranck@1313 492 }
jfranck@1313 493
jfranck@1313 494 // returns null if t is same as 's', returns 't' otherwise
jfranck@1313 495 private Type filterSame(Type t, Type s) {
jfranck@1313 496 if (t == null || s == null) {
jfranck@1313 497 return t;
jfranck@1313 498 }
jfranck@1313 499
jfranck@1313 500 return types.isSameType(t, s) ? null : t;
jfranck@1313 501 }
jfranck@1313 502
jfranck@1313 503 /** Extract the actual Type to be used for a containing annotation. */
jfranck@1313 504 private Type extractContainingType(Attribute.Compound ca,
jfranck@1313 505 DiagnosticPosition pos,
jfranck@1313 506 TypeSymbol annoDecl)
jfranck@1313 507 {
jjg@1492 508 // The next three checks check that the Repeatable annotation
jfranck@1313 509 // on the declaration of the annotation type that is repeating is
jfranck@1313 510 // valid.
jfranck@1313 511
jjg@1492 512 // Repeatable must have at least one element
jfranck@1313 513 if (ca.values.isEmpty()) {
jjg@1492 514 log.error(pos, "invalid.repeatable.annotation", annoDecl);
jfranck@1313 515 return null;
jfranck@1313 516 }
jfranck@1313 517 Pair<MethodSymbol,Attribute> p = ca.values.head;
jfranck@1313 518 Name name = p.fst.name;
jfranck@1313 519 if (name != names.value) { // should contain only one element, named "value"
jjg@1492 520 log.error(pos, "invalid.repeatable.annotation", annoDecl);
jfranck@1313 521 return null;
jfranck@1313 522 }
jfranck@1313 523 if (!(p.snd instanceof Attribute.Class)) { // check that the value of "value" is an Attribute.Class
jjg@1492 524 log.error(pos, "invalid.repeatable.annotation", annoDecl);
jfranck@1313 525 return null;
jfranck@1313 526 }
jfranck@1313 527
jfranck@1313 528 return ((Attribute.Class)p.snd).getValue();
jfranck@1313 529 }
jfranck@1313 530
jfranck@1313 531 /* Validate that the suggested targetContainerType Type is a valid
jfranck@1313 532 * container type for repeated instances of originalAnnoType
jfranck@1313 533 * annotations. Return null and report errors if this is not the
jfranck@1313 534 * case, return the MethodSymbol of the value element in
jfranck@1313 535 * targetContainerType if it is suitable (this is needed to
jfranck@1313 536 * synthesize the container). */
jfranck@1313 537 private MethodSymbol validateContainer(Type targetContainerType,
jfranck@1313 538 Type originalAnnoType,
jfranck@1313 539 DiagnosticPosition pos) {
jfranck@1313 540 MethodSymbol containerValueSymbol = null;
jfranck@1313 541 boolean fatalError = false;
jfranck@1313 542
jfranck@1313 543 // Validate that there is a (and only 1) value method
jfranck@1313 544 Scope scope = targetContainerType.tsym.members();
jfranck@1313 545 int nr_value_elems = 0;
jfranck@1313 546 boolean error = false;
jfranck@1313 547 for(Symbol elm : scope.getElementsByName(names.value)) {
jfranck@1313 548 nr_value_elems++;
jfranck@1313 549
jfranck@1313 550 if (nr_value_elems == 1 &&
jfranck@1313 551 elm.kind == Kinds.MTH) {
jfranck@1313 552 containerValueSymbol = (MethodSymbol)elm;
jfranck@1313 553 } else {
jfranck@1313 554 error = true;
jfranck@1313 555 }
jfranck@1313 556 }
jfranck@1313 557 if (error) {
jfranck@1313 558 log.error(pos,
jjg@1492 559 "invalid.repeatable.annotation.multiple.values",
jfranck@1313 560 targetContainerType,
jfranck@1313 561 nr_value_elems);
jfranck@1313 562 return null;
jfranck@1313 563 } else if (nr_value_elems == 0) {
jfranck@1313 564 log.error(pos,
jjg@1492 565 "invalid.repeatable.annotation.no.value",
jfranck@1313 566 targetContainerType);
jfranck@1313 567 return null;
jfranck@1313 568 }
jfranck@1313 569
jfranck@1313 570 // validate that the 'value' element is a method
jfranck@1313 571 // probably "impossible" to fail this
jfranck@1313 572 if (containerValueSymbol.kind != Kinds.MTH) {
jfranck@1313 573 log.error(pos,
jjg@1492 574 "invalid.repeatable.annotation.invalid.value",
jfranck@1313 575 targetContainerType);
jfranck@1313 576 fatalError = true;
jfranck@1313 577 }
jfranck@1313 578
jfranck@1313 579 // validate that the 'value' element has the correct return type
jfranck@1313 580 // i.e. array of original anno
jfranck@1313 581 Type valueRetType = containerValueSymbol.type.getReturnType();
jfranck@1313 582 Type expectedType = types.makeArrayType(originalAnnoType);
jfranck@1313 583 if (!(types.isArray(valueRetType) &&
jfranck@1313 584 types.isSameType(expectedType, valueRetType))) {
jfranck@1313 585 log.error(pos,
jjg@1492 586 "invalid.repeatable.annotation.value.return",
jfranck@1313 587 targetContainerType,
jfranck@1313 588 valueRetType,
jfranck@1313 589 expectedType);
jfranck@1313 590 fatalError = true;
jfranck@1313 591 }
jfranck@1313 592 if (error) {
jfranck@1313 593 fatalError = true;
jfranck@1313 594 }
jfranck@1313 595
jjg@1492 596 // The conditions for a valid containing annotation are made
jfranck@1313 597 // in Check.validateRepeatedAnnotaton();
jfranck@1313 598
jfranck@1313 599 return fatalError ? null : containerValueSymbol;
jfranck@1313 600 }
jfranck@1313 601 }

mercurial