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