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