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