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