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