src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java

Tue, 14 May 2013 15:04:06 -0700

author
jjg
date
Tue, 14 May 2013 15:04:06 -0700
changeset 1755
ddb4a2bfcd82
parent 1709
bae8387d16aa
child 1802
8fb68f73d4b1
permissions
-rw-r--r--

8013852: update reference impl for type-annotations
Reviewed-by: jjg
Contributed-by: wdietl@gmail.com, steve.sides@oracle.com, joel.franck@oracle.com, alex.buckley@oracle.com

jjg@1521 1 /*
jjg@1521 2 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1521 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1521 4 *
jjg@1521 5 * This code is free software; you can redistribute it and/or modify it
jjg@1521 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1521 7 * published by the Free Software Foundation. Oracle designates this
jjg@1521 8 * particular file as subject to the "Classpath" exception as provided
jjg@1521 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@1521 10 *
jjg@1521 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1521 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1521 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1521 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1521 15 * accompanied this code).
jjg@1521 16 *
jjg@1521 17 * You should have received a copy of the GNU General Public License version
jjg@1521 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1521 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1521 20 *
jjg@1521 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1521 22 * or visit www.oracle.com if you need additional information or have any
jjg@1521 23 * questions.
jjg@1521 24 */
jjg@1521 25
jjg@1521 26 package com.sun.tools.javac.code;
jjg@1521 27
jjg@1521 28 import javax.lang.model.element.Element;
jjg@1521 29 import javax.lang.model.element.ElementKind;
jjg@1521 30 import javax.lang.model.type.TypeKind;
jjg@1521 31
jjg@1521 32 import com.sun.tools.javac.code.Attribute;
jjg@1521 33 import com.sun.tools.javac.code.Attribute.TypeCompound;
jjg@1521 34 import com.sun.tools.javac.code.Flags;
jjg@1521 35 import com.sun.tools.javac.code.Kinds;
jjg@1521 36 import com.sun.tools.javac.code.Type.AnnotatedType;
jjg@1521 37 import com.sun.tools.javac.code.Type.ArrayType;
jjg@1521 38 import com.sun.tools.javac.code.Type.CapturedType;
jjg@1521 39 import com.sun.tools.javac.code.Type.ClassType;
jjg@1521 40 import com.sun.tools.javac.code.Type.ErrorType;
jjg@1521 41 import com.sun.tools.javac.code.Type.ForAll;
jjg@1521 42 import com.sun.tools.javac.code.Type.MethodType;
jjg@1521 43 import com.sun.tools.javac.code.Type.PackageType;
jjg@1521 44 import com.sun.tools.javac.code.Type.TypeVar;
jjg@1521 45 import com.sun.tools.javac.code.Type.UndetVar;
jjg@1521 46 import com.sun.tools.javac.code.Type.Visitor;
jjg@1521 47 import com.sun.tools.javac.code.Type.WildcardType;
jjg@1521 48 import com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntry;
jjg@1521 49 import com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntryKind;
jjg@1521 50 import com.sun.tools.javac.code.TypeTag;
jjg@1521 51 import com.sun.tools.javac.code.Symbol.VarSymbol;
jjg@1755 52 import com.sun.tools.javac.code.Symbol.MethodSymbol;
jjg@1755 53 import com.sun.tools.javac.comp.Annotate;
jjg@1521 54 import com.sun.tools.javac.comp.Annotate.Annotator;
jjg@1521 55 import com.sun.tools.javac.tree.JCTree;
jjg@1521 56 import com.sun.tools.javac.tree.JCTree.JCBlock;
jjg@1521 57 import com.sun.tools.javac.tree.JCTree.JCClassDecl;
jjg@1521 58 import com.sun.tools.javac.tree.JCTree.JCExpression;
jjg@1755 59 import com.sun.tools.javac.tree.JCTree.JCLambda;
jjg@1521 60 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
jjg@1755 61 import com.sun.tools.javac.tree.JCTree.JCNewClass;
jjg@1521 62 import com.sun.tools.javac.tree.JCTree.JCTypeApply;
jjg@1521 63 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
jjg@1521 64 import com.sun.tools.javac.tree.TreeScanner;
jjg@1521 65 import com.sun.tools.javac.tree.JCTree.*;
jjg@1521 66 import com.sun.tools.javac.util.Assert;
jjg@1521 67 import com.sun.tools.javac.util.List;
jjg@1521 68 import com.sun.tools.javac.util.ListBuffer;
jjg@1521 69 import com.sun.tools.javac.util.Log;
jjg@1521 70 import com.sun.tools.javac.util.Names;
jjg@1521 71
jjg@1521 72 /**
jjg@1521 73 * Contains operations specific to processing type annotations.
jjg@1521 74 * This class has two functions:
jjg@1521 75 * separate declaration from type annotations and insert the type
jjg@1521 76 * annotations to their types;
jjg@1521 77 * and determine the TypeAnnotationPositions for all type annotations.
jjg@1521 78 */
jjg@1521 79 public class TypeAnnotations {
jjg@1521 80 // Class cannot be instantiated.
jjg@1521 81 private TypeAnnotations() {}
jjg@1521 82
jjg@1521 83 /**
jjg@1521 84 * Separate type annotations from declaration annotations and
jjg@1521 85 * determine the correct positions for type annotations.
jjg@1521 86 * This version only visits types in signatures and should be
jjg@1521 87 * called from MemberEnter.
jjg@1755 88 * The method takes the Annotate object as parameter and
jjg@1755 89 * adds an Annotator to the correct Annotate queue for
jjg@1755 90 * later processing.
jjg@1521 91 */
jjg@1755 92 public static void organizeTypeAnnotationsSignatures(final Symtab syms, final Names names,
jjg@1755 93 final Log log, final JCClassDecl tree, Annotate annotate) {
jjg@1755 94 annotate.afterRepeated( new Annotator() {
jjg@1521 95 @Override
jjg@1521 96 public void enterAnnotation() {
jjg@1521 97 new TypeAnnotationPositions(syms, names, log, true).scan(tree);
jjg@1521 98 }
jjg@1755 99 } );
jjg@1521 100 }
jjg@1521 101
jjg@1521 102 /**
jjg@1521 103 * This version only visits types in bodies, that is, field initializers,
jjg@1521 104 * top-level blocks, and method bodies, and should be called from Attr.
jjg@1521 105 */
jjg@1521 106 public static void organizeTypeAnnotationsBodies(Symtab syms, Names names, Log log, JCClassDecl tree) {
jjg@1521 107 new TypeAnnotationPositions(syms, names, log, false).scan(tree);
jjg@1521 108 }
jjg@1521 109
jjg@1755 110 public enum AnnotationType { DECLARATION, TYPE, BOTH };
jjg@1755 111
jjg@1755 112 /**
jjg@1755 113 * Determine whether an annotation is a declaration annotation,
jjg@1755 114 * a type annotation, or both.
jjg@1755 115 */
jjg@1755 116 public static AnnotationType annotationType(Symtab syms, Names names,
jjg@1755 117 Attribute.Compound a, Symbol s) {
jjg@1755 118 Attribute.Compound atTarget =
jjg@1755 119 a.type.tsym.attribute(syms.annotationTargetType.tsym);
jjg@1755 120 if (atTarget == null) {
jjg@1755 121 return inferTargetMetaInfo(a, s);
jjg@1755 122 }
jjg@1755 123 Attribute atValue = atTarget.member(names.value);
jjg@1755 124 if (!(atValue instanceof Attribute.Array)) {
jjg@1755 125 Assert.error("annotationType(): bad @Target argument " + atValue +
jjg@1755 126 " (" + atValue.getClass() + ")");
jjg@1755 127 return AnnotationType.DECLARATION; // error recovery
jjg@1755 128 }
jjg@1755 129 Attribute.Array arr = (Attribute.Array) atValue;
jjg@1755 130 boolean isDecl = false, isType = false;
jjg@1755 131 for (Attribute app : arr.values) {
jjg@1755 132 if (!(app instanceof Attribute.Enum)) {
jjg@1755 133 Assert.error("annotationType(): unrecognized Attribute kind " + app +
jjg@1755 134 " (" + app.getClass() + ")");
jjg@1755 135 isDecl = true;
jjg@1755 136 continue;
jjg@1755 137 }
jjg@1755 138 Attribute.Enum e = (Attribute.Enum) app;
jjg@1755 139 if (e.value.name == names.TYPE) {
jjg@1755 140 if (s.kind == Kinds.TYP)
jjg@1755 141 isDecl = true;
jjg@1755 142 } else if (e.value.name == names.FIELD) {
jjg@1755 143 if (s.kind == Kinds.VAR &&
jjg@1755 144 s.owner.kind != Kinds.MTH)
jjg@1755 145 isDecl = true;
jjg@1755 146 } else if (e.value.name == names.METHOD) {
jjg@1755 147 if (s.kind == Kinds.MTH &&
jjg@1755 148 !s.isConstructor())
jjg@1755 149 isDecl = true;
jjg@1755 150 } else if (e.value.name == names.PARAMETER) {
jjg@1755 151 if (s.kind == Kinds.VAR &&
jjg@1755 152 s.owner.kind == Kinds.MTH &&
jjg@1755 153 (s.flags() & Flags.PARAMETER) != 0)
jjg@1755 154 isDecl = true;
jjg@1755 155 } else if (e.value.name == names.CONSTRUCTOR) {
jjg@1755 156 if (s.kind == Kinds.MTH &&
jjg@1755 157 s.isConstructor())
jjg@1755 158 isDecl = true;
jjg@1755 159 } else if (e.value.name == names.LOCAL_VARIABLE) {
jjg@1755 160 if (s.kind == Kinds.VAR &&
jjg@1755 161 s.owner.kind == Kinds.MTH &&
jjg@1755 162 (s.flags() & Flags.PARAMETER) == 0)
jjg@1755 163 isDecl = true;
jjg@1755 164 } else if (e.value.name == names.ANNOTATION_TYPE) {
jjg@1755 165 if (s.kind == Kinds.TYP &&
jjg@1755 166 (s.flags() & Flags.ANNOTATION) != 0)
jjg@1755 167 isDecl = true;
jjg@1755 168 } else if (e.value.name == names.PACKAGE) {
jjg@1755 169 if (s.kind == Kinds.PCK)
jjg@1755 170 isDecl = true;
jjg@1755 171 } else if (e.value.name == names.TYPE_USE) {
jjg@1755 172 if (s.kind == Kinds.TYP ||
jjg@1755 173 s.kind == Kinds.VAR ||
jjg@1755 174 (s.kind == Kinds.MTH && !s.isConstructor() &&
jjg@1755 175 !s.type.getReturnType().hasTag(TypeTag.VOID)) ||
jjg@1755 176 (s.kind == Kinds.MTH && s.isConstructor()))
jjg@1755 177 isType = true;
jjg@1755 178 } else if (e.value.name == names.TYPE_PARAMETER) {
jjg@1755 179 /* Irrelevant in this case */
jjg@1755 180 // TYPE_PARAMETER doesn't aid in distinguishing between
jjg@1755 181 // Type annotations and declaration annotations on an
jjg@1755 182 // Element
jjg@1755 183 } else {
jjg@1755 184 Assert.error("annotationType(): unrecognized Attribute name " + e.value.name +
jjg@1755 185 " (" + e.value.name.getClass() + ")");
jjg@1755 186 isDecl = true;
jjg@1755 187 }
jjg@1755 188 }
jjg@1755 189 if (isDecl && isType) {
jjg@1755 190 return AnnotationType.BOTH;
jjg@1755 191 } else if (isType) {
jjg@1755 192 return AnnotationType.TYPE;
jjg@1755 193 } else {
jjg@1755 194 return AnnotationType.DECLARATION;
jjg@1755 195 }
jjg@1755 196 }
jjg@1755 197
jjg@1755 198 /** Infer the target annotation kind, if none is give.
jjg@1755 199 * We only infer declaration annotations.
jjg@1755 200 */
jjg@1755 201 private static AnnotationType inferTargetMetaInfo(Attribute.Compound a, Symbol s) {
jjg@1755 202 return AnnotationType.DECLARATION;
jjg@1755 203 }
jjg@1755 204
jjg@1755 205
jjg@1521 206 private static class TypeAnnotationPositions extends TreeScanner {
jjg@1521 207
jjg@1521 208 private final Symtab syms;
jjg@1521 209 private final Names names;
jjg@1521 210 private final Log log;
jjg@1521 211 private final boolean sigOnly;
jjg@1521 212
jjg@1521 213 private TypeAnnotationPositions(Symtab syms, Names names, Log log, boolean sigOnly) {
jjg@1521 214 this.syms = syms;
jjg@1521 215 this.names = names;
jjg@1521 216 this.log = log;
jjg@1521 217 this.sigOnly = sigOnly;
jjg@1521 218 }
jjg@1521 219
jjg@1521 220 /*
jjg@1521 221 * When traversing the AST we keep the "frames" of visited
jjg@1521 222 * trees in order to determine the position of annotations.
jjg@1521 223 */
jjg@1521 224 private ListBuffer<JCTree> frames = ListBuffer.lb();
jjg@1521 225
jjg@1521 226 protected void push(JCTree t) { frames = frames.prepend(t); }
jjg@1521 227 protected JCTree pop() { return frames.next(); }
jjg@1521 228 // could this be frames.elems.tail.head?
jjg@1521 229 private JCTree peek2() { return frames.toList().tail.head; }
jjg@1521 230
jjg@1521 231 @Override
jjg@1521 232 public void scan(JCTree tree) {
jjg@1521 233 push(tree);
jjg@1521 234 super.scan(tree);
jjg@1521 235 pop();
jjg@1521 236 }
jjg@1521 237
jjg@1521 238 /**
jjg@1521 239 * Separates type annotations from declaration annotations.
jjg@1521 240 * This step is needed because in certain locations (where declaration
jjg@1521 241 * and type annotations can be mixed, e.g. the type of a field)
jjg@1521 242 * we never build an JCAnnotatedType. This step finds these
jjg@1521 243 * annotations and marks them as if they were part of the type.
jjg@1521 244 */
jjg@1521 245 private void separateAnnotationsKinds(JCTree typetree, Type type, Symbol sym,
jjg@1521 246 TypeAnnotationPosition pos) {
jjg@1521 247 /*
jjg@1521 248 System.out.printf("separateAnnotationsKinds(typetree: %s, type: %s, symbol: %s, pos: %s%n",
jjg@1521 249 typetree, type, sym, pos);
jjg@1521 250 */
jjg@1521 251 List<Attribute.Compound> annotations = sym.getRawAttributes();
jjg@1521 252 ListBuffer<Attribute.Compound> declAnnos = new ListBuffer<Attribute.Compound>();
jjg@1521 253 ListBuffer<Attribute.TypeCompound> typeAnnos = new ListBuffer<Attribute.TypeCompound>();
jjg@1521 254
jjg@1521 255 for (Attribute.Compound a : annotations) {
jjg@1755 256 switch (annotationType(syms, names, a, sym)) {
jjg@1521 257 case DECLARATION:
jjg@1521 258 declAnnos.append(a);
jjg@1521 259 break;
jjg@1521 260 case BOTH: {
jjg@1521 261 declAnnos.append(a);
jjg@1521 262 Attribute.TypeCompound ta = toTypeCompound(a, pos);
jjg@1521 263 typeAnnos.append(ta);
jjg@1521 264 break;
jjg@1521 265 }
jjg@1521 266 case TYPE: {
jjg@1521 267 Attribute.TypeCompound ta = toTypeCompound(a, pos);
jjg@1521 268 typeAnnos.append(ta);
jjg@1521 269 break;
jjg@1521 270 }
jjg@1521 271 }
jjg@1521 272 }
jjg@1521 273
jjg@1521 274 sym.annotations.reset();
jjg@1521 275 sym.annotations.setDeclarationAttributes(declAnnos.toList());
jjg@1521 276
jjg@1755 277 if (typeAnnos.isEmpty()) {
jjg@1755 278 return;
jjg@1755 279 }
jjg@1755 280
jjg@1521 281 List<Attribute.TypeCompound> typeAnnotations = typeAnnos.toList();
jjg@1521 282
jjg@1521 283 if (type == null) {
jjg@1521 284 // When type is null, put the type annotations to the symbol.
jjg@1521 285 // This is used for constructor return annotations, for which
jjg@1521 286 // no appropriate type exists.
jjg@1521 287 sym.annotations.appendUniqueTypes(typeAnnotations);
jjg@1521 288 return;
jjg@1521 289 }
jjg@1521 290
jjg@1521 291 // type is non-null and annotations are added to that type
jjg@1521 292 type = typeWithAnnotations(typetree, type, typeAnnotations, log);
jjg@1521 293
jjg@1521 294 if (sym.getKind() == ElementKind.METHOD) {
jjg@1521 295 sym.type.asMethodType().restype = type;
jjg@1755 296 } else if (sym.getKind() == ElementKind.PARAMETER) {
jjg@1755 297 sym.type = type;
jjg@1755 298 if (sym.getQualifiedName().equals(names._this)) {
jjg@1755 299 sym.owner.type.asMethodType().recvtype = type;
jjg@1755 300 // note that the typeAnnotations will also be added to the owner below.
jjg@1755 301 } else {
jjg@1755 302 MethodType methType = sym.owner.type.asMethodType();
jjg@1755 303 List<VarSymbol> params = ((MethodSymbol)sym.owner).params;
jjg@1755 304 List<Type> oldArgs = methType.argtypes;
jjg@1755 305 ListBuffer<Type> newArgs = new ListBuffer<Type>();
jjg@1755 306 while (params.nonEmpty()) {
jjg@1755 307 if (params.head == sym) {
jjg@1755 308 newArgs.add(type);
jjg@1755 309 } else {
jjg@1755 310 newArgs.add(oldArgs.head);
jjg@1755 311 }
jjg@1755 312 oldArgs = oldArgs.tail;
jjg@1755 313 params = params.tail;
jjg@1755 314 }
jjg@1755 315 methType.argtypes = newArgs.toList();
jjg@1755 316 }
jjg@1521 317 } else {
jjg@1521 318 sym.type = type;
jjg@1521 319 }
jjg@1521 320
jjg@1521 321 sym.annotations.appendUniqueTypes(typeAnnotations);
jjg@1755 322
jjg@1521 323 if (sym.getKind() == ElementKind.PARAMETER ||
jjg@1521 324 sym.getKind() == ElementKind.LOCAL_VARIABLE ||
jjg@1521 325 sym.getKind() == ElementKind.RESOURCE_VARIABLE ||
jjg@1521 326 sym.getKind() == ElementKind.EXCEPTION_PARAMETER) {
jjg@1521 327 // Make sure all type annotations from the symbol are also
jjg@1521 328 // on the owner.
jfranck@1709 329 sym.owner.annotations.appendUniqueTypes(sym.getRawTypeAttributes());
jjg@1521 330 }
jjg@1521 331 }
jjg@1521 332
jjg@1521 333 // This method has a similar purpose as
jjg@1521 334 // {@link com.sun.tools.javac.parser.JavacParser.insertAnnotationsToMostInner(JCExpression, List<JCTypeAnnotation>, boolean)}
jjg@1521 335 // We found a type annotation in a declaration annotation position,
jjg@1521 336 // for example, on the return type.
jjg@1521 337 // Such an annotation is _not_ part of an JCAnnotatedType tree and we therefore
jjg@1521 338 // need to set its position explicitly.
jjg@1521 339 // The method returns a copy of type that contains these annotations.
jjg@1563 340 //
jjg@1563 341 // As a side effect the method sets the type annotation position of "annotations".
jjg@1563 342 // Note that it is assumed that all annotations share the same position.
jjg@1521 343 private static Type typeWithAnnotations(final JCTree typetree, final Type type,
jjg@1521 344 final List<Attribute.TypeCompound> annotations, Log log) {
jjg@1521 345 // System.out.printf("typeWithAnnotations(typetree: %s, type: %s, annotations: %s)%n",
jjg@1521 346 // typetree, type, annotations);
jjg@1521 347 if (annotations.isEmpty()) {
jjg@1521 348 return type;
jjg@1521 349 }
jjg@1521 350 if (type.hasTag(TypeTag.ARRAY)) {
jjg@1521 351 Type toreturn;
jjg@1521 352 Type.ArrayType tomodify;
jjg@1521 353 Type.ArrayType arType;
jjg@1521 354 {
jjg@1521 355 Type touse = type;
jjg@1644 356 if (type.isAnnotated()) {
jjg@1521 357 Type.AnnotatedType atype = (Type.AnnotatedType)type;
jjg@1521 358 toreturn = new Type.AnnotatedType(atype.underlyingType);
jjg@1521 359 ((Type.AnnotatedType)toreturn).typeAnnotations = atype.typeAnnotations;
jjg@1521 360 touse = atype.underlyingType;
jjg@1521 361 arType = (Type.ArrayType) touse;
jjg@1521 362 tomodify = new Type.ArrayType(null, arType.tsym);
jjg@1521 363 ((Type.AnnotatedType)toreturn).underlyingType = tomodify;
jjg@1521 364 } else {
jjg@1521 365 arType = (Type.ArrayType) touse;
jjg@1521 366 tomodify = new Type.ArrayType(null, arType.tsym);
jjg@1521 367 toreturn = tomodify;
jjg@1521 368 }
jjg@1521 369 }
jjg@1521 370 JCArrayTypeTree arTree = arrayTypeTree(typetree);
jjg@1521 371
jjg@1521 372 ListBuffer<TypePathEntry> depth = ListBuffer.lb();
jjg@1521 373 depth = depth.append(TypePathEntry.ARRAY);
jjg@1521 374 while (arType.elemtype.hasTag(TypeTag.ARRAY)) {
jjg@1644 375 if (arType.elemtype.isAnnotated()) {
jjg@1521 376 Type.AnnotatedType aelemtype = (Type.AnnotatedType) arType.elemtype;
jjg@1521 377 Type.AnnotatedType newAT = new Type.AnnotatedType(aelemtype.underlyingType);
jjg@1521 378 tomodify.elemtype = newAT;
jjg@1521 379 newAT.typeAnnotations = aelemtype.typeAnnotations;
jjg@1521 380 arType = (Type.ArrayType) aelemtype.underlyingType;
jjg@1521 381 tomodify = new Type.ArrayType(null, arType.tsym);
jjg@1521 382 newAT.underlyingType = tomodify;
jjg@1521 383 } else {
jjg@1521 384 arType = (Type.ArrayType) arType.elemtype;
jjg@1521 385 tomodify.elemtype = new Type.ArrayType(null, arType.tsym);
jjg@1521 386 tomodify = (Type.ArrayType) tomodify.elemtype;
jjg@1521 387 }
jjg@1521 388 arTree = arrayTypeTree(arTree.elemtype);
jjg@1521 389 depth = depth.append(TypePathEntry.ARRAY);
jjg@1521 390 }
jjg@1521 391 Type arelemType = typeWithAnnotations(arTree.elemtype, arType.elemtype, annotations, log);
jjg@1521 392 tomodify.elemtype = arelemType;
jjg@1563 393 {
jjg@1563 394 // All annotations share the same position; modify the first one.
jjg@1563 395 Attribute.TypeCompound a = annotations.get(0);
jjg@1521 396 TypeAnnotationPosition p = a.position;
jjg@1521 397 p.location = p.location.prependList(depth.toList());
jjg@1521 398 }
jjg@1755 399 typetree.type = toreturn;
jjg@1521 400 return toreturn;
jjg@1521 401 } else if (type.hasTag(TypeTag.TYPEVAR)) {
jjg@1521 402 // Nothing to do for type variables.
jjg@1521 403 return type;
jjg@1755 404 } else if (type.getKind() == TypeKind.UNION) {
jjg@1755 405 // There is a TypeKind, but no TypeTag.
jjg@1755 406 JCTypeUnion tutree = (JCTypeUnion) typetree;
jjg@1755 407 JCExpression fst = tutree.alternatives.get(0);
jjg@1755 408 Type res = typeWithAnnotations(fst, fst.type, annotations, log);
jjg@1755 409 fst.type = res;
jjg@1755 410 // TODO: do we want to set res as first element in uct.alternatives?
jjg@1755 411 // UnionClassType uct = (com.sun.tools.javac.code.Type.UnionClassType)type;
jjg@1755 412 // Return the un-annotated union-type.
jjg@1755 413 return type;
jjg@1521 414 } else {
jjg@1521 415 Type enclTy = type;
jjg@1521 416 Element enclEl = type.asElement();
jjg@1521 417 JCTree enclTr = typetree;
jjg@1521 418
jjg@1521 419 while (enclEl != null &&
jjg@1521 420 enclEl.getKind() != ElementKind.PACKAGE &&
jjg@1521 421 enclTy != null &&
jjg@1521 422 enclTy.getKind() != TypeKind.NONE &&
jjg@1521 423 enclTy.getKind() != TypeKind.ERROR &&
jjg@1521 424 (enclTr.getKind() == JCTree.Kind.MEMBER_SELECT ||
jjg@1521 425 enclTr.getKind() == JCTree.Kind.PARAMETERIZED_TYPE ||
jjg@1521 426 enclTr.getKind() == JCTree.Kind.ANNOTATED_TYPE)) {
jjg@1521 427 // Iterate also over the type tree, not just the type: the type is already
jjg@1521 428 // completely resolved and we cannot distinguish where the annotation
jjg@1521 429 // belongs for a nested type.
jjg@1521 430 if (enclTr.getKind() == JCTree.Kind.MEMBER_SELECT) {
jjg@1521 431 // only change encl in this case.
jjg@1521 432 enclTy = enclTy.getEnclosingType();
jjg@1521 433 enclEl = enclEl.getEnclosingElement();
jjg@1521 434 enclTr = ((JCFieldAccess)enclTr).getExpression();
jjg@1521 435 } else if (enclTr.getKind() == JCTree.Kind.PARAMETERIZED_TYPE) {
jjg@1521 436 enclTr = ((JCTypeApply)enclTr).getType();
jjg@1521 437 } else {
jjg@1521 438 // only other option because of while condition
jjg@1521 439 enclTr = ((JCAnnotatedType)enclTr).getUnderlyingType();
jjg@1521 440 }
jjg@1521 441 }
jjg@1521 442
jjg@1521 443 /** We are trying to annotate some enclosing type,
jjg@1521 444 * but nothing more exists.
jjg@1521 445 */
jjg@1521 446 if (enclTy != null &&
jjg@1521 447 enclTy.getKind() == TypeKind.NONE &&
jjg@1521 448 (enclTr.getKind() == JCTree.Kind.IDENTIFIER ||
jjg@1521 449 enclTr.getKind() == JCTree.Kind.MEMBER_SELECT ||
jjg@1521 450 enclTr.getKind() == JCTree.Kind.PARAMETERIZED_TYPE ||
jjg@1521 451 enclTr.getKind() == JCTree.Kind.ANNOTATED_TYPE)) {
jjg@1521 452 // TODO: also if it's "java. @A lang.Object", that is,
jjg@1521 453 // if it's on a package?
jjg@1521 454 log.error(enclTr.pos(), "cant.annotate.nested.type", enclTr.toString());
jjg@1521 455 return type;
jjg@1521 456 }
jjg@1521 457
jjg@1521 458 // At this point we have visited the part of the nested
jjg@1521 459 // type that is written in the source code.
jjg@1521 460 // Now count from here to the actual top-level class to determine
jjg@1521 461 // the correct nesting.
jjg@1521 462
jjg@1521 463 // The genericLocation for the annotation.
jjg@1521 464 ListBuffer<TypePathEntry> depth = ListBuffer.lb();
jjg@1521 465
jjg@1521 466 Type topTy = enclTy;
jjg@1521 467 while (enclEl != null &&
jjg@1521 468 enclEl.getKind() != ElementKind.PACKAGE &&
jjg@1521 469 topTy != null &&
jjg@1521 470 topTy.getKind() != TypeKind.NONE &&
jjg@1521 471 topTy.getKind() != TypeKind.ERROR) {
jjg@1521 472 topTy = topTy.getEnclosingType();
jjg@1521 473 enclEl = enclEl.getEnclosingElement();
jjg@1521 474
jjg@1521 475 if (topTy != null && topTy.getKind() != TypeKind.NONE) {
jjg@1521 476 // Only count enclosing types.
jjg@1521 477 depth = depth.append(TypePathEntry.INNER_TYPE);
jjg@1521 478 }
jjg@1521 479 }
jjg@1521 480
jjg@1521 481 if (depth.nonEmpty()) {
jjg@1521 482 // Only need to change the annotation positions
jjg@1521 483 // if they are on an enclosed type.
jjg@1563 484 // All annotations share the same position; modify the first one.
jjg@1563 485 Attribute.TypeCompound a = annotations.get(0);
jjg@1563 486 TypeAnnotationPosition p = a.position;
jjg@1563 487 p.location = p.location.appendList(depth.toList());
jjg@1521 488 }
jjg@1521 489
jjg@1521 490 Type ret = typeWithAnnotations(type, enclTy, annotations);
jjg@1755 491 typetree.type = ret;
jjg@1521 492 return ret;
jjg@1521 493 }
jjg@1521 494 }
jjg@1521 495
jjg@1521 496 private static JCArrayTypeTree arrayTypeTree(JCTree typetree) {
jjg@1521 497 if (typetree.getKind() == JCTree.Kind.ARRAY_TYPE) {
jjg@1521 498 return (JCArrayTypeTree) typetree;
jjg@1521 499 } else if (typetree.getKind() == JCTree.Kind.ANNOTATED_TYPE) {
jjg@1521 500 return (JCArrayTypeTree) ((JCAnnotatedType)typetree).underlyingType;
jjg@1521 501 } else {
jjg@1521 502 Assert.error("Could not determine array type from type tree: " + typetree);
jjg@1521 503 return null;
jjg@1521 504 }
jjg@1521 505 }
jjg@1521 506
jjg@1521 507 /** Return a copy of the first type that only differs by
jjg@1521 508 * inserting the annotations to the left-most/inner-most type
jjg@1521 509 * or the type given by stopAt.
jjg@1521 510 *
jjg@1521 511 * We need the stopAt parameter to know where on a type to
jjg@1521 512 * put the annotations.
jjg@1521 513 * If we have nested classes Outer > Middle > Inner, and we
jjg@1521 514 * have the source type "@A Middle.Inner", we will invoke
jjg@1521 515 * this method with type = Outer.Middle.Inner,
jjg@1521 516 * stopAt = Middle.Inner, and annotations = @A.
jjg@1521 517 *
jjg@1521 518 * @param type The type to copy.
jjg@1521 519 * @param stopAt The type to stop at.
jjg@1521 520 * @param annotations The annotations to insert.
jjg@1521 521 * @return A copy of type that contains the annotations.
jjg@1521 522 */
jjg@1521 523 private static Type typeWithAnnotations(final Type type,
jjg@1521 524 final Type stopAt,
jjg@1521 525 final List<Attribute.TypeCompound> annotations) {
jjg@1521 526 Visitor<Type, List<TypeCompound>> visitor =
jjg@1521 527 new Type.Visitor<Type, List<Attribute.TypeCompound>>() {
jjg@1521 528 @Override
jjg@1521 529 public Type visitClassType(ClassType t, List<TypeCompound> s) {
jjg@1521 530 // assert that t.constValue() == null?
jjg@1521 531 if (t == stopAt ||
jjg@1521 532 t.getEnclosingType() == Type.noType) {
jjg@1521 533 return new AnnotatedType(s, t);
jjg@1521 534 } else {
jjg@1521 535 ClassType ret = new ClassType(t.getEnclosingType().accept(this, s),
jjg@1521 536 t.typarams_field, t.tsym);
jjg@1521 537 ret.all_interfaces_field = t.all_interfaces_field;
jjg@1521 538 ret.allparams_field = t.allparams_field;
jjg@1521 539 ret.interfaces_field = t.interfaces_field;
jjg@1521 540 ret.rank_field = t.rank_field;
jjg@1521 541 ret.supertype_field = t.supertype_field;
jjg@1521 542 return ret;
jjg@1521 543 }
jjg@1521 544 }
jjg@1521 545
jjg@1521 546 @Override
jjg@1521 547 public Type visitAnnotatedType(AnnotatedType t, List<TypeCompound> s) {
jjg@1521 548 return new AnnotatedType(t.typeAnnotations, t.underlyingType.accept(this, s));
jjg@1521 549 }
jjg@1521 550
jjg@1521 551 @Override
jjg@1521 552 public Type visitWildcardType(WildcardType t, List<TypeCompound> s) {
jjg@1521 553 return new AnnotatedType(s, t);
jjg@1521 554 }
jjg@1521 555
jjg@1521 556 @Override
jjg@1521 557 public Type visitArrayType(ArrayType t, List<TypeCompound> s) {
jjg@1521 558 ArrayType ret = new ArrayType(t.elemtype.accept(this, s), t.tsym);
jjg@1521 559 return ret;
jjg@1521 560 }
jjg@1521 561
jjg@1521 562 @Override
jjg@1521 563 public Type visitMethodType(MethodType t, List<TypeCompound> s) {
jjg@1521 564 // Impossible?
jjg@1521 565 return t;
jjg@1521 566 }
jjg@1521 567
jjg@1521 568 @Override
jjg@1521 569 public Type visitPackageType(PackageType t, List<TypeCompound> s) {
jjg@1521 570 // Impossible?
jjg@1521 571 return t;
jjg@1521 572 }
jjg@1521 573
jjg@1521 574 @Override
jjg@1521 575 public Type visitTypeVar(TypeVar t, List<TypeCompound> s) {
jjg@1521 576 return new AnnotatedType(s, t);
jjg@1521 577 }
jjg@1521 578
jjg@1521 579 @Override
jjg@1521 580 public Type visitCapturedType(CapturedType t, List<TypeCompound> s) {
jjg@1521 581 return new AnnotatedType(s, t);
jjg@1521 582 }
jjg@1521 583
jjg@1521 584 @Override
jjg@1521 585 public Type visitForAll(ForAll t, List<TypeCompound> s) {
jjg@1521 586 // Impossible?
jjg@1521 587 return t;
jjg@1521 588 }
jjg@1521 589
jjg@1521 590 @Override
jjg@1521 591 public Type visitUndetVar(UndetVar t, List<TypeCompound> s) {
jjg@1521 592 // Impossible?
jjg@1521 593 return t;
jjg@1521 594 }
jjg@1521 595
jjg@1521 596 @Override
jjg@1521 597 public Type visitErrorType(ErrorType t, List<TypeCompound> s) {
jjg@1521 598 return new AnnotatedType(s, t);
jjg@1521 599 }
jjg@1521 600
jjg@1521 601 @Override
jjg@1521 602 public Type visitType(Type t, List<TypeCompound> s) {
jjg@1563 603 return new AnnotatedType(s, t);
jjg@1521 604 }
jjg@1521 605 };
jjg@1521 606
jjg@1521 607 return type.accept(visitor, annotations);
jjg@1521 608 }
jjg@1521 609
jjg@1521 610 private static Attribute.TypeCompound toTypeCompound(Attribute.Compound a, TypeAnnotationPosition p) {
jjg@1521 611 // It is safe to alias the position.
jjg@1521 612 return new Attribute.TypeCompound(a, p);
jjg@1521 613 }
jjg@1521 614
jjg@1521 615
jjg@1521 616 /* This is the beginning of the second part of organizing
jjg@1521 617 * type annotations: determine the type annotation positions.
jjg@1521 618 */
jjg@1521 619
jjg@1521 620 private void resolveFrame(JCTree tree, JCTree frame,
jjg@1521 621 List<JCTree> path, TypeAnnotationPosition p) {
jjg@1521 622 /*
jjg@1521 623 System.out.println("Resolving tree: " + tree + " kind: " + tree.getKind());
jjg@1521 624 System.out.println(" Framing tree: " + frame + " kind: " + frame.getKind());
jjg@1521 625 */
jjg@1563 626
jjg@1563 627 // Note that p.offset is set in
jjg@1563 628 // com.sun.tools.javac.jvm.Gen.setTypeAnnotationPositions(int)
jjg@1563 629
jjg@1521 630 switch (frame.getKind()) {
jjg@1521 631 case TYPE_CAST:
jjg@1755 632 JCTypeCast frameTC = (JCTypeCast) frame;
jjg@1521 633 p.type = TargetType.CAST;
jjg@1755 634 if (frameTC.clazz.hasTag(Tag.TYPEINTERSECTION)) {
jjg@1755 635 // This case was already handled by INTERSECTION_TYPE
jjg@1755 636 } else {
jjg@1755 637 p.type_index = 0;
jjg@1755 638 }
jjg@1521 639 p.pos = frame.pos;
jjg@1521 640 return;
jjg@1521 641
jjg@1521 642 case INSTANCE_OF:
jjg@1521 643 p.type = TargetType.INSTANCEOF;
jjg@1521 644 p.pos = frame.pos;
jjg@1521 645 return;
jjg@1521 646
jjg@1521 647 case NEW_CLASS:
jjg@1755 648 JCNewClass frameNewClass = (JCNewClass) frame;
jjg@1755 649 if (frameNewClass.def != null) {
jjg@1755 650 // Special handling for anonymous class instantiations
jjg@1755 651 JCClassDecl frameClassDecl = frameNewClass.def;
jjg@1755 652 if (frameClassDecl.extending == tree) {
jjg@1755 653 p.type = TargetType.CLASS_EXTENDS;
jjg@1755 654 p.type_index = -1;
jjg@1755 655 } else if (frameClassDecl.implementing.contains(tree)) {
jjg@1755 656 p.type = TargetType.CLASS_EXTENDS;
jjg@1755 657 p.type_index = frameClassDecl.implementing.indexOf(tree);
jjg@1755 658 } else {
jjg@1755 659 // In contrast to CLASS below, typarams cannot occur here.
jjg@1755 660 Assert.error("Could not determine position of tree " + tree +
jjg@1755 661 " within frame " + frame);
jjg@1755 662 }
jjg@1755 663 } else if (frameNewClass.typeargs.contains(tree)) {
jjg@1521 664 p.type = TargetType.CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT;
jjg@1521 665 p.type_index = frameNewClass.typeargs.indexOf(tree);
jjg@1521 666 } else {
jjg@1521 667 p.type = TargetType.NEW;
jjg@1521 668 }
jjg@1521 669 p.pos = frame.pos;
jjg@1521 670 return;
jjg@1521 671
jjg@1521 672 case NEW_ARRAY:
jjg@1521 673 p.type = TargetType.NEW;
jjg@1521 674 p.pos = frame.pos;
jjg@1521 675 return;
jjg@1521 676
jjg@1521 677 case ANNOTATION_TYPE:
jjg@1521 678 case CLASS:
jjg@1521 679 case ENUM:
jjg@1521 680 case INTERFACE:
jjg@1521 681 p.pos = frame.pos;
jjg@1521 682 if (((JCClassDecl)frame).extending == tree) {
jjg@1521 683 p.type = TargetType.CLASS_EXTENDS;
jjg@1521 684 p.type_index = -1;
jjg@1521 685 } else if (((JCClassDecl)frame).implementing.contains(tree)) {
jjg@1521 686 p.type = TargetType.CLASS_EXTENDS;
jjg@1521 687 p.type_index = ((JCClassDecl)frame).implementing.indexOf(tree);
jjg@1521 688 } else if (((JCClassDecl)frame).typarams.contains(tree)) {
jjg@1521 689 p.type = TargetType.CLASS_TYPE_PARAMETER;
jjg@1521 690 p.parameter_index = ((JCClassDecl)frame).typarams.indexOf(tree);
jjg@1521 691 } else {
jjg@1521 692 Assert.error("Could not determine position of tree " + tree +
jjg@1521 693 " within frame " + frame);
jjg@1521 694 }
jjg@1521 695 return;
jjg@1521 696
jjg@1521 697 case METHOD: {
jjg@1521 698 JCMethodDecl frameMethod = (JCMethodDecl) frame;
jjg@1521 699 p.pos = frame.pos;
jjg@1521 700 if (frameMethod.thrown.contains(tree)) {
jjg@1521 701 p.type = TargetType.THROWS;
jjg@1521 702 p.type_index = frameMethod.thrown.indexOf(tree);
jjg@1521 703 } else if (frameMethod.restype == tree) {
jjg@1521 704 p.type = TargetType.METHOD_RETURN;
jjg@1521 705 } else if (frameMethod.typarams.contains(tree)) {
jjg@1521 706 p.type = TargetType.METHOD_TYPE_PARAMETER;
jjg@1521 707 p.parameter_index = frameMethod.typarams.indexOf(tree);
jjg@1521 708 } else {
jjg@1521 709 Assert.error("Could not determine position of tree " + tree +
jjg@1521 710 " within frame " + frame);
jjg@1521 711 }
jjg@1521 712 return;
jjg@1521 713 }
jjg@1521 714
jjg@1521 715 case PARAMETERIZED_TYPE: {
jjg@1755 716 List<JCTree> newPath = path.tail;
jjg@1755 717
jjg@1521 718 if (((JCTypeApply)frame).clazz == tree) {
jjg@1521 719 // generic: RAW; noop
jjg@1521 720 } else if (((JCTypeApply)frame).arguments.contains(tree)) {
jjg@1521 721 JCTypeApply taframe = (JCTypeApply) frame;
jjg@1521 722 int arg = taframe.arguments.indexOf(tree);
jjg@1521 723 p.location = p.location.prepend(new TypePathEntry(TypePathEntryKind.TYPE_ARGUMENT, arg));
jjg@1521 724
jjg@1755 725 Type typeToUse;
jjg@1755 726 if (newPath.tail != null && newPath.tail.head.hasTag(Tag.NEWCLASS)) {
jjg@1755 727 // If we are within an anonymous class instantiation, use its type,
jjg@1755 728 // because it contains a correctly nested type.
jjg@1755 729 typeToUse = newPath.tail.head.type;
jjg@1755 730 } else {
jjg@1755 731 typeToUse = taframe.type;
jjg@1755 732 }
jjg@1755 733
jjg@1755 734 locateNestedTypes(typeToUse, p);
jjg@1521 735 } else {
jjg@1521 736 Assert.error("Could not determine type argument position of tree " + tree +
jjg@1521 737 " within frame " + frame);
jjg@1521 738 }
jjg@1521 739
jjg@1521 740 resolveFrame(newPath.head, newPath.tail.head, newPath, p);
jjg@1521 741 return;
jjg@1521 742 }
jjg@1521 743
jjg@1563 744 case MEMBER_REFERENCE: {
jjg@1563 745 JCMemberReference mrframe = (JCMemberReference) frame;
jjg@1563 746
jjg@1563 747 if (mrframe.expr == tree) {
jjg@1563 748 switch (mrframe.mode) {
jjg@1563 749 case INVOKE:
jjg@1563 750 p.type = TargetType.METHOD_REFERENCE;
jjg@1563 751 break;
jjg@1563 752 case NEW:
jjg@1563 753 p.type = TargetType.CONSTRUCTOR_REFERENCE;
jjg@1563 754 break;
jjg@1563 755 default:
jjg@1563 756 Assert.error("Unknown method reference mode " + mrframe.mode +
jjg@1563 757 " for tree " + tree + " within frame " + frame);
jjg@1563 758 }
jjg@1563 759 p.pos = frame.pos;
jjg@1563 760 } else if (mrframe.typeargs != null &&
jjg@1563 761 mrframe.typeargs.contains(tree)) {
jjg@1563 762 int arg = mrframe.typeargs.indexOf(tree);
jjg@1563 763 p.type_index = arg;
jjg@1563 764 switch (mrframe.mode) {
jjg@1563 765 case INVOKE:
jjg@1563 766 p.type = TargetType.METHOD_REFERENCE_TYPE_ARGUMENT;
jjg@1563 767 break;
jjg@1563 768 case NEW:
jjg@1563 769 p.type = TargetType.CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT;
jjg@1563 770 break;
jjg@1563 771 default:
jjg@1563 772 Assert.error("Unknown method reference mode " + mrframe.mode +
jjg@1563 773 " for tree " + tree + " within frame " + frame);
jjg@1563 774 }
jjg@1563 775 p.pos = frame.pos;
jjg@1563 776 } else {
jjg@1563 777 Assert.error("Could not determine type argument position of tree " + tree +
jjg@1563 778 " within frame " + frame);
jjg@1563 779 }
jjg@1563 780 return;
jjg@1563 781 }
jjg@1563 782
jjg@1521 783 case ARRAY_TYPE: {
jjg@1521 784 ListBuffer<TypePathEntry> index = ListBuffer.lb();
jjg@1521 785 index = index.append(TypePathEntry.ARRAY);
jjg@1521 786 List<JCTree> newPath = path.tail;
jjg@1521 787 while (true) {
jjg@1521 788 JCTree npHead = newPath.tail.head;
jjg@1521 789 if (npHead.hasTag(JCTree.Tag.TYPEARRAY)) {
jjg@1521 790 newPath = newPath.tail;
jjg@1521 791 index = index.append(TypePathEntry.ARRAY);
jjg@1521 792 } else if (npHead.hasTag(JCTree.Tag.ANNOTATED_TYPE)) {
jjg@1521 793 newPath = newPath.tail;
jjg@1521 794 } else {
jjg@1521 795 break;
jjg@1521 796 }
jjg@1521 797 }
jjg@1521 798 p.location = p.location.prependList(index.toList());
jjg@1521 799 resolveFrame(newPath.head, newPath.tail.head, newPath, p);
jjg@1521 800 return;
jjg@1521 801 }
jjg@1521 802
jjg@1521 803 case TYPE_PARAMETER:
jjg@1521 804 if (path.tail.tail.head.hasTag(JCTree.Tag.CLASSDEF)) {
jjg@1521 805 JCClassDecl clazz = (JCClassDecl)path.tail.tail.head;
jjg@1521 806 p.type = TargetType.CLASS_TYPE_PARAMETER_BOUND;
jjg@1521 807 p.parameter_index = clazz.typarams.indexOf(path.tail.head);
jjg@1521 808 p.bound_index = ((JCTypeParameter)frame).bounds.indexOf(tree);
jjg@1521 809 if (((JCTypeParameter)frame).bounds.get(0).type.isInterface()) {
jjg@1521 810 // Account for an implicit Object as bound 0
jjg@1521 811 p.bound_index += 1;
jjg@1521 812 }
jjg@1521 813 } else if (path.tail.tail.head.hasTag(JCTree.Tag.METHODDEF)) {
jjg@1521 814 JCMethodDecl method = (JCMethodDecl)path.tail.tail.head;
jjg@1521 815 p.type = TargetType.METHOD_TYPE_PARAMETER_BOUND;
jjg@1521 816 p.parameter_index = method.typarams.indexOf(path.tail.head);
jjg@1521 817 p.bound_index = ((JCTypeParameter)frame).bounds.indexOf(tree);
jjg@1521 818 if (((JCTypeParameter)frame).bounds.get(0).type.isInterface()) {
jjg@1521 819 // Account for an implicit Object as bound 0
jjg@1521 820 p.bound_index += 1;
jjg@1521 821 }
jjg@1521 822 } else {
jjg@1521 823 Assert.error("Could not determine position of tree " + tree +
jjg@1521 824 " within frame " + frame);
jjg@1521 825 }
jjg@1521 826 p.pos = frame.pos;
jjg@1521 827 return;
jjg@1521 828
jjg@1521 829 case VARIABLE:
jjg@1521 830 VarSymbol v = ((JCVariableDecl)frame).sym;
jjg@1521 831 p.pos = frame.pos;
jjg@1521 832 switch (v.getKind()) {
jjg@1521 833 case LOCAL_VARIABLE:
jjg@1521 834 p.type = TargetType.LOCAL_VARIABLE;
jjg@1521 835 break;
jjg@1521 836 case FIELD:
jjg@1521 837 p.type = TargetType.FIELD;
jjg@1521 838 break;
jjg@1521 839 case PARAMETER:
jjg@1521 840 if (v.getQualifiedName().equals(names._this)) {
jjg@1521 841 // TODO: Intro a separate ElementKind?
jjg@1521 842 p.type = TargetType.METHOD_RECEIVER;
jjg@1521 843 } else {
jjg@1521 844 p.type = TargetType.METHOD_FORMAL_PARAMETER;
jjg@1521 845 p.parameter_index = methodParamIndex(path, frame);
jjg@1521 846 }
jjg@1521 847 break;
jjg@1521 848 case EXCEPTION_PARAMETER:
jjg@1521 849 p.type = TargetType.EXCEPTION_PARAMETER;
jjg@1521 850 break;
jjg@1521 851 case RESOURCE_VARIABLE:
jjg@1521 852 p.type = TargetType.RESOURCE_VARIABLE;
jjg@1521 853 break;
jjg@1521 854 default:
jjg@1521 855 Assert.error("Found unexpected type annotation for variable: " + v + " with kind: " + v.getKind());
jjg@1521 856 }
jjg@1755 857 if (v.getKind() != ElementKind.FIELD) {
jjg@1755 858 v.owner.annotations.appendUniqueTypes(v.getRawTypeAttributes());
jjg@1755 859 }
jjg@1521 860 return;
jjg@1521 861
jjg@1521 862 case ANNOTATED_TYPE: {
jjg@1521 863 if (frame == tree) {
jjg@1521 864 // This is only true for the first annotated type we see.
jjg@1521 865 // For any other annotated types along the path, we do
jjg@1521 866 // not care about inner types.
jjg@1521 867 JCAnnotatedType atypetree = (JCAnnotatedType) frame;
jjg@1521 868 final Type utype = atypetree.underlyingType.type;
jjg@1755 869 if (utype == null) {
jjg@1755 870 // This might happen during DeferredAttr;
jjg@1755 871 // we will be back later.
jjg@1755 872 return;
jjg@1755 873 }
jjg@1521 874 Symbol tsym = utype.tsym;
jjg@1521 875 if (tsym.getKind().equals(ElementKind.TYPE_PARAMETER) ||
jjg@1521 876 utype.getKind().equals(TypeKind.WILDCARD) ||
jjg@1521 877 utype.getKind().equals(TypeKind.ARRAY)) {
jjg@1521 878 // Type parameters, wildcards, and arrays have the declaring
jjg@1521 879 // class/method as enclosing elements.
jjg@1521 880 // There is actually nothing to do for them.
jjg@1521 881 } else {
jjg@1521 882 locateNestedTypes(utype, p);
jjg@1521 883 }
jjg@1521 884 }
jjg@1521 885 List<JCTree> newPath = path.tail;
jjg@1521 886 resolveFrame(newPath.head, newPath.tail.head, newPath, p);
jjg@1521 887 return;
jjg@1521 888 }
jjg@1521 889
jjg@1521 890 case UNION_TYPE: {
jjg@1521 891 List<JCTree> newPath = path.tail;
jjg@1521 892 resolveFrame(newPath.head, newPath.tail.head, newPath, p);
jjg@1521 893 return;
jjg@1521 894 }
jjg@1521 895
jjg@1563 896 case INTERSECTION_TYPE: {
jjg@1563 897 JCTypeIntersection isect = (JCTypeIntersection)frame;
jjg@1563 898 p.type_index = isect.bounds.indexOf(tree);
jjg@1563 899 List<JCTree> newPath = path.tail;
jjg@1563 900 resolveFrame(newPath.head, newPath.tail.head, newPath, p);
jjg@1563 901 return;
jjg@1563 902 }
jjg@1563 903
jjg@1521 904 case METHOD_INVOCATION: {
jjg@1521 905 JCMethodInvocation invocation = (JCMethodInvocation)frame;
jjg@1521 906 if (!invocation.typeargs.contains(tree)) {
jjg@1521 907 Assert.error("{" + tree + "} is not an argument in the invocation: " + invocation);
jjg@1521 908 }
jjg@1521 909 p.type = TargetType.METHOD_INVOCATION_TYPE_ARGUMENT;
jjg@1521 910 p.pos = invocation.pos;
jjg@1521 911 p.type_index = invocation.typeargs.indexOf(tree);
jjg@1521 912 return;
jjg@1521 913 }
jjg@1521 914
jjg@1521 915 case EXTENDS_WILDCARD:
jjg@1521 916 case SUPER_WILDCARD: {
jjg@1521 917 // Annotations in wildcard bounds
jjg@1521 918 p.location = p.location.prepend(TypePathEntry.WILDCARD);
jjg@1521 919 List<JCTree> newPath = path.tail;
jjg@1521 920 resolveFrame(newPath.head, newPath.tail.head, newPath, p);
jjg@1521 921 return;
jjg@1521 922 }
jjg@1521 923
jjg@1521 924 case MEMBER_SELECT: {
jjg@1521 925 List<JCTree> newPath = path.tail;
jjg@1521 926 resolveFrame(newPath.head, newPath.tail.head, newPath, p);
jjg@1521 927 return;
jjg@1521 928 }
jjg@1521 929
jjg@1521 930 default:
jjg@1521 931 Assert.error("Unresolved frame: " + frame + " of kind: " + frame.getKind() +
jjg@1521 932 "\n Looking for tree: " + tree);
jjg@1521 933 return;
jjg@1521 934 }
jjg@1521 935 }
jjg@1521 936
jjg@1521 937 private static void locateNestedTypes(Type type, TypeAnnotationPosition p) {
jjg@1521 938 // The number of "steps" to get from the full type to the
jjg@1521 939 // left-most outer type.
jjg@1521 940 ListBuffer<TypePathEntry> depth = ListBuffer.lb();
jjg@1521 941
jjg@1521 942 Type encl = type.getEnclosingType();
jjg@1521 943 while (encl != null &&
jjg@1521 944 encl.getKind() != TypeKind.NONE &&
jjg@1521 945 encl.getKind() != TypeKind.ERROR) {
jjg@1521 946 depth = depth.append(TypePathEntry.INNER_TYPE);
jjg@1521 947 encl = encl.getEnclosingType();
jjg@1521 948 }
jjg@1521 949 if (depth.nonEmpty()) {
jjg@1521 950 p.location = p.location.prependList(depth.toList());
jjg@1521 951 }
jjg@1521 952 }
jjg@1521 953
jjg@1521 954 private static int methodParamIndex(List<JCTree> path, JCTree param) {
jjg@1521 955 List<JCTree> curr = path;
jjg@1755 956 while (curr.head.getTag() != Tag.METHODDEF &&
jjg@1755 957 curr.head.getTag() != Tag.LAMBDA) {
jjg@1521 958 curr = curr.tail;
jjg@1521 959 }
jjg@1755 960 if (curr.head.getTag() == Tag.METHODDEF) {
jjg@1755 961 JCMethodDecl method = (JCMethodDecl)curr.head;
jjg@1755 962 return method.params.indexOf(param);
jjg@1755 963 } else if (curr.head.getTag() == Tag.LAMBDA) {
jjg@1755 964 JCLambda lambda = (JCLambda)curr.head;
jjg@1755 965 return lambda.params.indexOf(param);
jjg@1755 966 } else {
jjg@1755 967 Assert.error("methodParamIndex expected to find method or lambda for param: " + param);
jjg@1755 968 return -1;
jjg@1755 969 }
jjg@1521 970 }
jjg@1521 971
jjg@1521 972 // Each class (including enclosed inner classes) is visited separately.
jjg@1521 973 // This flag is used to prevent from visiting inner classes.
jjg@1521 974 private boolean isInClass = false;
jjg@1521 975
jjg@1521 976 @Override
jjg@1521 977 public void visitClassDef(JCClassDecl tree) {
jjg@1521 978 if (isInClass)
jjg@1521 979 return;
jjg@1521 980 isInClass = true;
jjg@1755 981
jjg@1521 982 if (sigOnly) {
jjg@1521 983 scan(tree.mods);
jjg@1521 984 scan(tree.typarams);
jjg@1521 985 scan(tree.extending);
jjg@1521 986 scan(tree.implementing);
jjg@1521 987 }
jjg@1521 988 scan(tree.defs);
jjg@1521 989 }
jjg@1521 990
jjg@1521 991 /**
jjg@1521 992 * Resolve declaration vs. type annotations in methods and
jjg@1521 993 * then determine the positions.
jjg@1521 994 */
jjg@1521 995 @Override
jjg@1521 996 public void visitMethodDef(final JCMethodDecl tree) {
jjg@1521 997 if (tree.sym == null) {
jjg@1521 998 // Something most be wrong, e.g. a class not found.
jjg@1521 999 // Quietly ignore. (See test FailOver15.java)
jjg@1521 1000 return;
jjg@1521 1001 }
jjg@1521 1002 if (sigOnly) {
jjg@1755 1003 if (!tree.mods.annotations.isEmpty()) {
jjg@1755 1004 // Nothing to do for separateAnnotationsKinds if
jjg@1755 1005 // there are no annotations of either kind.
jjg@1521 1006 TypeAnnotationPosition pos = new TypeAnnotationPosition();
jjg@1521 1007 pos.type = TargetType.METHOD_RETURN;
jjg@1521 1008 if (tree.sym.isConstructor()) {
jjg@1521 1009 pos.pos = tree.pos;
jjg@1521 1010 // Use null to mark that the annotations go with the symbol.
jjg@1521 1011 separateAnnotationsKinds(tree, null, tree.sym, pos);
jjg@1521 1012 } else {
jjg@1521 1013 pos.pos = tree.restype.pos;
jjg@1521 1014 separateAnnotationsKinds(tree.restype, tree.sym.type.getReturnType(),
jjg@1521 1015 tree.sym, pos);
jjg@1521 1016 }
jjg@1521 1017 }
jjg@1755 1018 if (tree.recvparam != null && tree.recvparam.sym != null &&
jjg@1755 1019 !tree.recvparam.mods.annotations.isEmpty()) {
jjg@1755 1020 // Nothing to do for separateAnnotationsKinds if
jjg@1755 1021 // there are no annotations of either kind.
jjg@1521 1022 // TODO: make sure there are no declaration annotations.
jjg@1521 1023 TypeAnnotationPosition pos = new TypeAnnotationPosition();
jjg@1521 1024 pos.type = TargetType.METHOD_RECEIVER;
jjg@1521 1025 pos.pos = tree.recvparam.vartype.pos;
jjg@1521 1026 separateAnnotationsKinds(tree.recvparam.vartype, tree.recvparam.sym.type,
jjg@1521 1027 tree.recvparam.sym, pos);
jjg@1521 1028 }
jjg@1521 1029 int i = 0;
jjg@1521 1030 for (JCVariableDecl param : tree.params) {
jjg@1755 1031 if (!param.mods.annotations.isEmpty()) {
jjg@1755 1032 // Nothing to do for separateAnnotationsKinds if
jjg@1755 1033 // there are no annotations of either kind.
jjg@1755 1034 TypeAnnotationPosition pos = new TypeAnnotationPosition();
jjg@1755 1035 pos.type = TargetType.METHOD_FORMAL_PARAMETER;
jjg@1755 1036 pos.parameter_index = i;
jjg@1755 1037 pos.pos = param.vartype.pos;
jjg@1755 1038 separateAnnotationsKinds(param.vartype, param.sym.type, param.sym, pos);
jjg@1755 1039 }
jjg@1521 1040 ++i;
jjg@1521 1041 }
jjg@1521 1042 }
jjg@1521 1043
jjg@1521 1044 push(tree);
jjg@1521 1045 // super.visitMethodDef(tree);
jjg@1521 1046 if (sigOnly) {
jjg@1521 1047 scan(tree.mods);
jjg@1521 1048 scan(tree.restype);
jjg@1521 1049 scan(tree.typarams);
jjg@1521 1050 scan(tree.recvparam);
jjg@1521 1051 scan(tree.params);
jjg@1521 1052 scan(tree.thrown);
jjg@1521 1053 } else {
jjg@1521 1054 scan(tree.defaultValue);
jjg@1521 1055 scan(tree.body);
jjg@1521 1056 }
jjg@1521 1057 pop();
jjg@1521 1058 }
jjg@1521 1059
jjg@1755 1060 /* Store a reference to the current lambda expression, to
jjg@1755 1061 * be used by all type annotations within this expression.
jjg@1755 1062 */
jjg@1755 1063 private JCLambda currentLambda = null;
jjg@1755 1064
jjg@1755 1065 public void visitLambda(JCLambda tree) {
jjg@1755 1066 JCLambda prevLambda = currentLambda;
jjg@1755 1067 try {
jjg@1755 1068 currentLambda = tree;
jjg@1755 1069
jjg@1755 1070 int i = 0;
jjg@1755 1071 for (JCVariableDecl param : tree.params) {
jjg@1755 1072 if (!param.mods.annotations.isEmpty()) {
jjg@1755 1073 // Nothing to do for separateAnnotationsKinds if
jjg@1755 1074 // there are no annotations of either kind.
jjg@1755 1075 TypeAnnotationPosition pos = new TypeAnnotationPosition();
jjg@1755 1076 pos.type = TargetType.METHOD_FORMAL_PARAMETER;
jjg@1755 1077 pos.parameter_index = i;
jjg@1755 1078 pos.pos = param.vartype.pos;
jjg@1755 1079 pos.onLambda = tree;
jjg@1755 1080 separateAnnotationsKinds(param.vartype, param.sym.type, param.sym, pos);
jjg@1755 1081 }
jjg@1755 1082 ++i;
jjg@1755 1083 }
jjg@1755 1084
jjg@1755 1085 push(tree);
jjg@1755 1086 scan(tree.body);
jjg@1755 1087 scan(tree.params);
jjg@1755 1088 pop();
jjg@1755 1089 } finally {
jjg@1755 1090 currentLambda = prevLambda;
jjg@1755 1091 }
jjg@1755 1092 }
jjg@1755 1093
jjg@1521 1094 /**
jjg@1521 1095 * Resolve declaration vs. type annotations in variable declarations and
jjg@1521 1096 * then determine the positions.
jjg@1521 1097 */
jjg@1521 1098 @Override
jjg@1521 1099 public void visitVarDef(final JCVariableDecl tree) {
jjg@1755 1100 if (tree.mods.annotations.isEmpty()) {
jjg@1755 1101 // Nothing to do for separateAnnotationsKinds if
jjg@1755 1102 // there are no annotations of either kind.
jjg@1755 1103 } else if (tree.sym == null) {
jjg@1521 1104 // Something is wrong already. Quietly ignore.
jjg@1563 1105 } else if (tree.sym.getKind() == ElementKind.PARAMETER) {
jjg@1755 1106 // Parameters are handled in visitMethodDef or visitLambda.
jjg@1521 1107 } else if (tree.sym.getKind() == ElementKind.FIELD) {
jjg@1521 1108 if (sigOnly) {
jjg@1521 1109 TypeAnnotationPosition pos = new TypeAnnotationPosition();
jjg@1521 1110 pos.type = TargetType.FIELD;
jjg@1521 1111 pos.pos = tree.pos;
jjg@1521 1112 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos);
jjg@1521 1113 }
jjg@1521 1114 } else if (tree.sym.getKind() == ElementKind.LOCAL_VARIABLE) {
jjg@1521 1115 TypeAnnotationPosition pos = new TypeAnnotationPosition();
jjg@1521 1116 pos.type = TargetType.LOCAL_VARIABLE;
jjg@1521 1117 pos.pos = tree.pos;
jjg@1755 1118 pos.onLambda = currentLambda;
jjg@1521 1119 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos);
jjg@1521 1120 } else if (tree.sym.getKind() == ElementKind.EXCEPTION_PARAMETER) {
jjg@1521 1121 TypeAnnotationPosition pos = new TypeAnnotationPosition();
jjg@1521 1122 pos.type = TargetType.EXCEPTION_PARAMETER;
jjg@1521 1123 pos.pos = tree.pos;
jjg@1755 1124 pos.onLambda = currentLambda;
jjg@1521 1125 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos);
jjg@1521 1126 } else if (tree.sym.getKind() == ElementKind.RESOURCE_VARIABLE) {
jjg@1521 1127 TypeAnnotationPosition pos = new TypeAnnotationPosition();
jjg@1521 1128 pos.type = TargetType.RESOURCE_VARIABLE;
jjg@1521 1129 pos.pos = tree.pos;
jjg@1755 1130 pos.onLambda = currentLambda;
jjg@1521 1131 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos);
jjg@1563 1132 } else if (tree.sym.getKind() == ElementKind.ENUM_CONSTANT) {
jjg@1563 1133 // No type annotations can occur here.
jjg@1521 1134 } else {
jjg@1521 1135 // There is nothing else in a variable declaration that needs separation.
jjg@1563 1136 Assert.error("Unhandled variable kind: " + tree + " of kind: " + tree.sym.getKind());
jjg@1521 1137 }
jjg@1521 1138
jjg@1521 1139 push(tree);
jjg@1521 1140 // super.visitVarDef(tree);
jjg@1521 1141 scan(tree.mods);
jjg@1521 1142 scan(tree.vartype);
jjg@1521 1143 if (!sigOnly) {
jjg@1521 1144 scan(tree.init);
jjg@1521 1145 }
jjg@1521 1146 pop();
jjg@1521 1147 }
jjg@1521 1148
jjg@1521 1149 @Override
jjg@1521 1150 public void visitBlock(JCBlock tree) {
jjg@1521 1151 // Do not descend into top-level blocks when only interested
jjg@1521 1152 // in the signature.
jjg@1521 1153 if (!sigOnly) {
jjg@1521 1154 scan(tree.stats);
jjg@1521 1155 }
jjg@1521 1156 }
jjg@1521 1157
jjg@1521 1158 @Override
jjg@1521 1159 public void visitAnnotatedType(JCAnnotatedType tree) {
jjg@1521 1160 push(tree);
jjg@1521 1161 findPosition(tree, tree, tree.annotations);
jjg@1521 1162 pop();
jjg@1521 1163 super.visitAnnotatedType(tree);
jjg@1521 1164 }
jjg@1521 1165
jjg@1521 1166 @Override
jjg@1521 1167 public void visitTypeParameter(JCTypeParameter tree) {
jjg@1521 1168 findPosition(tree, peek2(), tree.annotations);
jjg@1521 1169 super.visitTypeParameter(tree);
jjg@1521 1170 }
jjg@1521 1171
jjg@1521 1172 @Override
jjg@1755 1173 public void visitNewClass(JCNewClass tree) {
jjg@1755 1174 if (tree.def != null &&
jjg@1755 1175 !tree.def.mods.annotations.isEmpty()) {
jjg@1755 1176 JCClassDecl classdecl = tree.def;
jjg@1755 1177 TypeAnnotationPosition pos = new TypeAnnotationPosition();
jjg@1755 1178 pos.type = TargetType.CLASS_EXTENDS;
jjg@1755 1179 pos.pos = tree.pos;
jjg@1755 1180 if (classdecl.extending == tree.clazz) {
jjg@1755 1181 pos.type_index = -1;
jjg@1755 1182 } else if (classdecl.implementing.contains(tree.clazz)) {
jjg@1755 1183 pos.type_index = classdecl.implementing.indexOf(tree.clazz);
jjg@1755 1184 } else {
jjg@1755 1185 // In contrast to CLASS elsewhere, typarams cannot occur here.
jjg@1755 1186 Assert.error("Could not determine position of tree " + tree);
jjg@1755 1187 }
jjg@1755 1188 Type before = classdecl.sym.type;
jjg@1755 1189 separateAnnotationsKinds(classdecl, tree.clazz.type, classdecl.sym, pos);
jjg@1755 1190
jjg@1755 1191 // classdecl.sym.type now contains an annotated type, which
jjg@1755 1192 // is not what we want there.
jjg@1755 1193 // TODO: should we put this type somewhere in the superclass/interface?
jjg@1755 1194 classdecl.sym.type = before;
jjg@1755 1195 }
jjg@1755 1196
jjg@1755 1197 scan(tree.encl);
jjg@1755 1198 scan(tree.typeargs);
jjg@1755 1199 scan(tree.clazz);
jjg@1755 1200 scan(tree.args);
jjg@1755 1201
jjg@1755 1202 // The class body will already be scanned.
jjg@1755 1203 // scan(tree.def);
jjg@1755 1204 }
jjg@1755 1205
jjg@1755 1206 @Override
jjg@1521 1207 public void visitNewArray(JCNewArray tree) {
jjg@1521 1208 findPosition(tree, tree, tree.annotations);
jjg@1521 1209 int dimAnnosCount = tree.dimAnnotations.size();
jjg@1521 1210 ListBuffer<TypePathEntry> depth = ListBuffer.lb();
jjg@1521 1211
jjg@1521 1212 // handle annotations associated with dimensions
jjg@1521 1213 for (int i = 0; i < dimAnnosCount; ++i) {
jjg@1521 1214 TypeAnnotationPosition p = new TypeAnnotationPosition();
jjg@1521 1215 p.pos = tree.pos;
jjg@1755 1216 p.onLambda = currentLambda;
jjg@1521 1217 p.type = TargetType.NEW;
jjg@1521 1218 if (i != 0) {
jjg@1521 1219 depth = depth.append(TypePathEntry.ARRAY);
jjg@1521 1220 p.location = p.location.appendList(depth.toList());
jjg@1521 1221 }
jjg@1521 1222
jjg@1521 1223 setTypeAnnotationPos(tree.dimAnnotations.get(i), p);
jjg@1521 1224 }
jjg@1521 1225
jjg@1521 1226 // handle "free" annotations
jjg@1521 1227 // int i = dimAnnosCount == 0 ? 0 : dimAnnosCount - 1;
jjg@1521 1228 // TODO: is depth.size == i here?
jjg@1521 1229 JCExpression elemType = tree.elemtype;
jjg@1755 1230 depth = depth.append(TypePathEntry.ARRAY);
jjg@1521 1231 while (elemType != null) {
jjg@1521 1232 if (elemType.hasTag(JCTree.Tag.ANNOTATED_TYPE)) {
jjg@1521 1233 JCAnnotatedType at = (JCAnnotatedType)elemType;
jjg@1521 1234 TypeAnnotationPosition p = new TypeAnnotationPosition();
jjg@1521 1235 p.type = TargetType.NEW;
jjg@1521 1236 p.pos = tree.pos;
jjg@1755 1237 p.onLambda = currentLambda;
jjg@1755 1238 locateNestedTypes(elemType.type, p);
jjg@1755 1239 p.location = p.location.prependList(depth.toList());
jjg@1521 1240 setTypeAnnotationPos(at.annotations, p);
jjg@1521 1241 elemType = at.underlyingType;
jjg@1521 1242 } else if (elemType.hasTag(JCTree.Tag.TYPEARRAY)) {
jjg@1521 1243 depth = depth.append(TypePathEntry.ARRAY);
jjg@1521 1244 elemType = ((JCArrayTypeTree)elemType).elemtype;
jjg@1755 1245 } else if (elemType.hasTag(JCTree.Tag.SELECT)) {
jjg@1755 1246 elemType = ((JCFieldAccess)elemType).selected;
jjg@1521 1247 } else {
jjg@1521 1248 break;
jjg@1521 1249 }
jjg@1521 1250 }
jjg@1521 1251 scan(tree.elems);
jjg@1521 1252 }
jjg@1521 1253
jjg@1521 1254 private void findPosition(JCTree tree, JCTree frame, List<JCAnnotation> annotations) {
jjg@1521 1255 if (!annotations.isEmpty()) {
jjg@1521 1256 /*
jjg@1521 1257 System.out.println("Finding pos for: " + annotations);
jjg@1755 1258 System.out.println(" tree: " + tree + " kind: " + tree.getKind());
jjg@1755 1259 System.out.println(" frame: " + frame + " kind: " + frame.getKind());
jjg@1521 1260 */
jjg@1521 1261 TypeAnnotationPosition p = new TypeAnnotationPosition();
jjg@1755 1262 p.onLambda = currentLambda;
jjg@1521 1263 resolveFrame(tree, frame, frames.toList(), p);
jjg@1521 1264 setTypeAnnotationPos(annotations, p);
jjg@1521 1265 }
jjg@1521 1266 }
jjg@1521 1267
jjg@1521 1268 private static void setTypeAnnotationPos(List<JCAnnotation> annotations,
jjg@1521 1269 TypeAnnotationPosition position) {
jjg@1521 1270 for (JCAnnotation anno : annotations) {
jjg@1755 1271 // attribute might be null during DeferredAttr;
jjg@1755 1272 // we will be back later.
jjg@1755 1273 if (anno.attribute != null) {
jjg@1755 1274 ((Attribute.TypeCompound) anno.attribute).position = position;
jjg@1755 1275 }
jjg@1521 1276 }
jjg@1521 1277 }
jjg@1755 1278
jjg@1755 1279 @Override
jjg@1755 1280 public String toString() {
jjg@1755 1281 return super.toString() + ": sigOnly: " + sigOnly;
jjg@1755 1282 }
jjg@1521 1283 }
jjg@1521 1284 }

mercurial