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

Thu, 25 Oct 2012 11:09:36 -0700

author
jjg
date
Thu, 25 Oct 2012 11:09:36 -0700
changeset 1374
c002fdee76fd
parent 1344
73312ec2cf7c
child 1445
376d6c1b49e5
permissions
-rw-r--r--

7200915: convert TypeTags from a series of small ints to an enum
Reviewed-by: jjg, mcimadamore
Contributed-by: vicente.romero@oracle.com

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

mercurial