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