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

Wed, 28 Aug 2013 15:40:33 -0700

author
jjg
date
Wed, 28 Aug 2013 15:40:33 -0700
changeset 1984
189942cdf585
parent 1969
7de231613e4a
child 2047
5f915a0c9615
permissions
-rw-r--r--

8010310: [javadoc] Error processing sources with -private
Reviewed-by: vromero, mcimadamore

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

mercurial