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

Mon, 04 Feb 2013 18:08:53 -0500

author
dholmes
date
Mon, 04 Feb 2013 18:08:53 -0500
changeset 1570
f91144b7da75
parent 1521
71f35e4b93a5
child 1563
bc456436c613
permissions
-rw-r--r--

Merge

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

mercurial