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

Tue, 15 Oct 2013 15:57:13 -0700

author
jjg
date
Tue, 15 Oct 2013 15:57:13 -0700
changeset 2134
b0c086cd4520
parent 2133
19e8eebfbe52
child 2136
7f6481e5fe3a
permissions
-rw-r--r--

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

mercurial