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

Fri, 28 Sep 2012 11:39:29 -0700

author
jfranck
date
Fri, 28 Sep 2012 11:39:29 -0700
changeset 1344
73312ec2cf7c
parent 1322
324b98626f58
child 1374
c002fdee76fd
permissions
-rw-r--r--

7199925: Separate compilation breaks check that elements have a default for the containing annotation
Reviewed-by: jjg, mcimadamore

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

mercurial