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

Sun, 20 Oct 2013 12:01:43 -0700

author
jjg
date
Sun, 20 Oct 2013 12:01:43 -0700
changeset 2149
e5d3cd43c85e
parent 2136
7f6481e5fe3a
child 2150
ae4f5cb78ebd
permissions
-rw-r--r--

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

mercurial