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