author | mcimadamore |
Mon, 04 Dec 2017 17:54:49 +0000 | |
changeset 48054 | 702043a4cdeb |
parent 47216 | 71c04702a3d5 |
child 48085 | 8e96f85f2feb |
permissions | -rw-r--r-- |
10 | 1 |
/* |
43142
e72c3d9b0a0b
8172213: Remove unused and partially implemented JavacElements#getSourcePosition methods
vromero
parents:
42826
diff
changeset
|
2 |
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.model; |
|
27 |
||
43857
37614a3b7193
8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents:
43591
diff
changeset
|
28 |
import java.util.Collections; |
43149 | 29 |
import java.util.HashSet; |
42824 | 30 |
import java.util.LinkedHashSet; |
10 | 31 |
import java.util.Map; |
42824 | 32 |
import java.util.Set; |
43149 | 33 |
import java.util.stream.Collectors; |
13077 | 34 |
|
42826 | 35 |
import javax.lang.model.AnnotatedConstruct; |
10 | 36 |
import javax.lang.model.SourceVersion; |
37 |
import javax.lang.model.element.*; |
|
38 |
import javax.lang.model.type.DeclaredType; |
|
39 |
import javax.lang.model.util.Elements; |
|
40 |
import javax.tools.JavaFileObject; |
|
13077 | 41 |
import static javax.lang.model.util.ElementFilter.methodsIn; |
42 |
||
36526 | 43 |
import com.sun.source.util.JavacTask; |
44 |
import com.sun.tools.javac.api.JavacTaskImpl; |
|
10 | 45 |
import com.sun.tools.javac.code.*; |
42826 | 46 |
import com.sun.tools.javac.code.Attribute.Compound; |
47 |
import com.sun.tools.javac.code.Directive.ExportsDirective; |
|
48 |
import com.sun.tools.javac.code.Directive.ExportsFlag; |
|
49 |
import com.sun.tools.javac.code.Directive.OpensDirective; |
|
50 |
import com.sun.tools.javac.code.Directive.OpensFlag; |
|
51 |
import com.sun.tools.javac.code.Directive.RequiresDirective; |
|
52 |
import com.sun.tools.javac.code.Directive.RequiresFlag; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
24069
diff
changeset
|
53 |
import com.sun.tools.javac.code.Scope.WriteableScope; |
48054
702043a4cdeb
8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents:
47216
diff
changeset
|
54 |
import com.sun.tools.javac.code.Source.Feature; |
10 | 55 |
import com.sun.tools.javac.code.Symbol.*; |
56 |
import com.sun.tools.javac.comp.AttrContext; |
|
57 |
import com.sun.tools.javac.comp.Enter; |
|
58 |
import com.sun.tools.javac.comp.Env; |
|
59 |
import com.sun.tools.javac.main.JavaCompiler; |
|
60 |
import com.sun.tools.javac.processing.PrintingProcessor; |
|
61 |
import com.sun.tools.javac.tree.JCTree; |
|
62 |
import com.sun.tools.javac.tree.JCTree.*; |
|
63 |
import com.sun.tools.javac.tree.TreeInfo; |
|
64 |
import com.sun.tools.javac.tree.TreeScanner; |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
10
diff
changeset
|
65 |
import com.sun.tools.javac.util.*; |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
66 |
import com.sun.tools.javac.util.DefinedBy.Api; |
10 | 67 |
import com.sun.tools.javac.util.Name; |
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
26266
diff
changeset
|
68 |
import static com.sun.tools.javac.code.Kinds.Kind.*; |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
24069
diff
changeset
|
69 |
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE; |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
13077
diff
changeset
|
70 |
import static com.sun.tools.javac.code.TypeTag.CLASS; |
36526 | 71 |
import com.sun.tools.javac.comp.Modules; |
42824 | 72 |
import com.sun.tools.javac.comp.Resolve; |
73 |
import com.sun.tools.javac.comp.Resolve.RecoveryLoadClass; |
|
43149 | 74 |
import com.sun.tools.javac.resources.CompilerProperties.Notes; |
10950 | 75 |
import static com.sun.tools.javac.tree.JCTree.Tag.*; |
10 | 76 |
|
77 |
/** |
|
78 |
* Utility methods for operating on program elements. |
|
79 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
80 |
* <p><b>This is NOT part of any supported API. |
10 | 81 |
* If you write code that depends on this, you do so at your own |
82 |
* risk. This code and its internal interfaces are subject to change |
|
83 |
* or deletion without notice.</b></p> |
|
84 |
*/ |
|
85 |
public class JavacElements implements Elements { |
|
86 |
||
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
23122
diff
changeset
|
87 |
private final JavaCompiler javaCompiler; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
23122
diff
changeset
|
88 |
private final Symtab syms; |
36526 | 89 |
private final Modules modules; |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
23122
diff
changeset
|
90 |
private final Names names; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
23122
diff
changeset
|
91 |
private final Types types; |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
23122
diff
changeset
|
92 |
private final Enter enter; |
42824 | 93 |
private final Resolve resolve; |
36526 | 94 |
private final JavacTaskImpl javacTaskImpl; |
43149 | 95 |
private final Log log; |
96 |
private final boolean allowModules; |
|
10 | 97 |
|
98 |
public static JavacElements instance(Context context) { |
|
6924
ef7a9281ad2f
6988836: A new JavacElements is created for each round of annotation processing
jjg
parents:
5847
diff
changeset
|
99 |
JavacElements instance = context.get(JavacElements.class); |
ef7a9281ad2f
6988836: A new JavacElements is created for each round of annotation processing
jjg
parents:
5847
diff
changeset
|
100 |
if (instance == null) |
10 | 101 |
instance = new JavacElements(context); |
102 |
return instance; |
|
103 |
} |
|
104 |
||
6924
ef7a9281ad2f
6988836: A new JavacElements is created for each round of annotation processing
jjg
parents:
5847
diff
changeset
|
105 |
protected JavacElements(Context context) { |
ef7a9281ad2f
6988836: A new JavacElements is created for each round of annotation processing
jjg
parents:
5847
diff
changeset
|
106 |
context.put(JavacElements.class, this); |
10 | 107 |
javaCompiler = JavaCompiler.instance(context); |
108 |
syms = Symtab.instance(context); |
|
36526 | 109 |
modules = Modules.instance(context); |
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
10
diff
changeset
|
110 |
names = Names.instance(context); |
10 | 111 |
types = Types.instance(context); |
112 |
enter = Enter.instance(context); |
|
42824 | 113 |
resolve = Resolve.instance(context); |
36526 | 114 |
JavacTask t = context.get(JavacTask.class); |
115 |
javacTaskImpl = t instanceof JavacTaskImpl ? (JavacTaskImpl) t : null; |
|
43149 | 116 |
log = Log.instance(context); |
117 |
Source source = Source.instance(context); |
|
48054
702043a4cdeb
8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents:
47216
diff
changeset
|
118 |
allowModules = Feature.MODULES.allowedInSource(source); |
36526 | 119 |
} |
120 |
||
121 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
|
43857
37614a3b7193
8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents:
43591
diff
changeset
|
122 |
public Set<? extends ModuleElement> getAllModuleElements() { |
37614a3b7193
8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents:
43591
diff
changeset
|
123 |
if (allowModules) |
37614a3b7193
8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents:
43591
diff
changeset
|
124 |
return Collections.unmodifiableSet(modules.allModules()); |
37614a3b7193
8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents:
43591
diff
changeset
|
125 |
else |
37614a3b7193
8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents:
43591
diff
changeset
|
126 |
return Collections.emptySet(); |
37614a3b7193
8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents:
43591
diff
changeset
|
127 |
} |
37614a3b7193
8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents:
43591
diff
changeset
|
128 |
|
37614a3b7193
8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents:
43591
diff
changeset
|
129 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
36526 | 130 |
public ModuleSymbol getModuleElement(CharSequence name) { |
131 |
ensureEntered("getModuleElement"); |
|
42824 | 132 |
if (modules.getDefaultModule() == syms.noModule) |
133 |
return null; |
|
36526 | 134 |
String strName = name.toString(); |
135 |
if (strName.equals("")) |
|
136 |
return syms.unnamedModule; |
|
137 |
return modules.getObservableModule(names.fromString(strName)); |
|
10 | 138 |
} |
139 |
||
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36779
diff
changeset
|
140 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
10 | 141 |
public PackageSymbol getPackageElement(CharSequence name) { |
42824 | 142 |
return doGetPackageElement(null, name); |
36526 | 143 |
} |
144 |
||
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36779
diff
changeset
|
145 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
36526 | 146 |
public PackageSymbol getPackageElement(ModuleElement module, CharSequence name) { |
42824 | 147 |
module.getClass(); |
148 |
return doGetPackageElement(module, name); |
|
149 |
} |
|
150 |
||
151 |
private PackageSymbol doGetPackageElement(ModuleElement module, CharSequence name) { |
|
152 |
ensureEntered("getPackageElement"); |
|
43149 | 153 |
return doGetElement(module, "getPackageElement", name, PackageSymbol.class); |
10 | 154 |
} |
155 |
||
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36779
diff
changeset
|
156 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
10 | 157 |
public ClassSymbol getTypeElement(CharSequence name) { |
42824 | 158 |
return doGetTypeElement(null, name); |
36526 | 159 |
} |
160 |
||
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36779
diff
changeset
|
161 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
36526 | 162 |
public ClassSymbol getTypeElement(ModuleElement module, CharSequence name) { |
42824 | 163 |
module.getClass(); |
164 |
||
165 |
return doGetTypeElement(module, name); |
|
166 |
} |
|
167 |
||
168 |
private ClassSymbol doGetTypeElement(ModuleElement module, CharSequence name) { |
|
169 |
ensureEntered("getTypeElement"); |
|
43149 | 170 |
return doGetElement(module, "getTypeElement", name, ClassSymbol.class); |
42824 | 171 |
} |
172 |
||
43149 | 173 |
private <S extends Symbol> S doGetElement(ModuleElement module, String methodName, |
174 |
CharSequence name, Class<S> clazz) { |
|
10 | 175 |
String strName = name.toString(); |
43369
aafd33c96bac
8173068: ElementUtils getPackageElement does not allow for an unnamed package
jlahoda
parents:
43272
diff
changeset
|
176 |
if (!SourceVersion.isName(strName) && (!strName.isEmpty() || clazz == ClassSymbol.class)) { |
42824 | 177 |
return null; |
178 |
} |
|
179 |
if (module == null) { |
|
43149 | 180 |
return unboundNameToSymbol(methodName, strName, clazz); |
42824 | 181 |
} else { |
182 |
return nameToSymbol((ModuleSymbol) module, strName, clazz); |
|
183 |
} |
|
184 |
} |
|
185 |
||
43149 | 186 |
private final Set<String> alreadyWarnedDuplicates = new HashSet<>(); |
187 |
||
188 |
private <S extends Symbol> S unboundNameToSymbol(String methodName, |
|
189 |
String nameStr, |
|
190 |
Class<S> clazz) { |
|
42824 | 191 |
if (modules.getDefaultModule() == syms.noModule) { //not a modular mode: |
192 |
return nameToSymbol(syms.noModule, nameStr, clazz); |
|
193 |
} |
|
194 |
||
43272
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
195 |
Set<S> found = new LinkedHashSet<>(); |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
196 |
|
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
197 |
for (ModuleSymbol msym : modules.allModules()) { |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
198 |
S sym = nameToSymbol(msym, nameStr, clazz); |
42824 | 199 |
|
43272
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
200 |
if (sym != null) { |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
201 |
if (!allowModules || clazz == ClassSymbol.class || !sym.members().isEmpty()) { |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
202 |
//do not add packages without members: |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
203 |
found.add(sym); |
42824 | 204 |
} |
205 |
} |
|
43272
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
206 |
} |
42824 | 207 |
|
43272
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
208 |
if (found.size() == 1) { |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
209 |
return found.iterator().next(); |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
210 |
} else if (found.size() > 1) { |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
211 |
//more than one element found, produce a note: |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
212 |
if (alreadyWarnedDuplicates.add(methodName + ":" + nameStr)) { |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
213 |
String moduleNames = found.stream() |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
214 |
.map(s -> s.packge().modle) |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
215 |
.map(m -> m.toString()) |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
216 |
.collect(Collectors.joining(", ")); |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
217 |
log.note(Notes.MultipleElements(methodName, nameStr, moduleNames)); |
42824 | 218 |
} |
43272
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
219 |
return null; |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
220 |
} else { |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
221 |
//not found, or more than one element found: |
421ae1e38d2d
8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents:
43149
diff
changeset
|
222 |
return null; |
42824 | 223 |
} |
10 | 224 |
} |
225 |
||
226 |
/** |
|
36526 | 227 |
* Returns a symbol given the type's or package's canonical name, |
10 | 228 |
* or null if the name isn't found. |
229 |
*/ |
|
36526 | 230 |
private <S extends Symbol> S nameToSymbol(ModuleSymbol module, String nameStr, Class<S> clazz) { |
10 | 231 |
Name name = names.fromString(nameStr); |
232 |
// First check cache. |
|
233 |
Symbol sym = (clazz == ClassSymbol.class) |
|
36526 | 234 |
? syms.getClass(module, name) |
235 |
: syms.lookupPackage(module, name); |
|
10 | 236 |
|
237 |
try { |
|
238 |
if (sym == null) |
|
36526 | 239 |
sym = javaCompiler.resolveIdent(module, nameStr); |
10 | 240 |
|
241 |
sym.complete(); |
|
242 |
||
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
26266
diff
changeset
|
243 |
return (sym.kind != ERR && |
10 | 244 |
sym.exists() && |
245 |
clazz.isInstance(sym) && |
|
246 |
name.equals(sym.getQualifiedName())) |
|
247 |
? clazz.cast(sym) |
|
248 |
: null; |
|
249 |
} catch (CompletionFailure e) { |
|
250 |
return null; |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Returns the tree for an annotation given the annotated element |
|
256 |
* and the element's own tree. Returns null if the tree cannot be found. |
|
257 |
*/ |
|
258 |
private JCTree matchAnnoToTree(AnnotationMirror findme, |
|
259 |
Element e, JCTree tree) { |
|
260 |
Symbol sym = cast(Symbol.class, e); |
|
261 |
class Vis extends JCTree.Visitor { |
|
262 |
List<JCAnnotation> result = null; |
|
24069 | 263 |
public void visitPackageDef(JCPackageDecl tree) { |
264 |
result = tree.annotations; |
|
10 | 265 |
} |
266 |
public void visitClassDef(JCClassDecl tree) { |
|
267 |
result = tree.mods.annotations; |
|
268 |
} |
|
269 |
public void visitMethodDef(JCMethodDecl tree) { |
|
270 |
result = tree.mods.annotations; |
|
271 |
} |
|
272 |
public void visitVarDef(JCVariableDecl tree) { |
|
273 |
result = tree.mods.annotations; |
|
274 |
} |
|
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
22163
diff
changeset
|
275 |
@Override |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
22163
diff
changeset
|
276 |
public void visitTypeParameter(JCTypeParameter tree) { |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
22163
diff
changeset
|
277 |
result = tree.annotations; |
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
22163
diff
changeset
|
278 |
} |
10 | 279 |
} |
280 |
Vis vis = new Vis(); |
|
281 |
tree.accept(vis); |
|
282 |
if (vis.result == null) |
|
283 |
return null; |
|
15355
a4757c33cae9
7193719: Support repeating annotations in javax.lang.model
jfranck
parents:
14359
diff
changeset
|
284 |
|
23122
02c931d49ad2
6411385: Trees.getPath does not work for constructors
jlahoda
parents:
22163
diff
changeset
|
285 |
List<Attribute.Compound> annos = sym.getAnnotationMirrors(); |
10 | 286 |
return matchAnnoToTree(cast(Attribute.Compound.class, findme), |
15355
a4757c33cae9
7193719: Support repeating annotations in javax.lang.model
jfranck
parents:
14359
diff
changeset
|
287 |
annos, |
10 | 288 |
vis.result); |
289 |
} |
|
290 |
||
291 |
/** |
|
292 |
* Returns the tree for an annotation given a list of annotations |
|
293 |
* in which to search (recursively) and their corresponding trees. |
|
294 |
* Returns null if the tree cannot be found. |
|
295 |
*/ |
|
296 |
private JCTree matchAnnoToTree(Attribute.Compound findme, |
|
297 |
List<Attribute.Compound> annos, |
|
298 |
List<JCAnnotation> trees) { |
|
299 |
for (Attribute.Compound anno : annos) { |
|
300 |
for (JCAnnotation tree : trees) { |
|
43591
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
301 |
if (tree.type.tsym != anno.type.tsym) |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
302 |
continue; |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
303 |
JCTree match = matchAttributeToTree(findme, anno, tree); |
10 | 304 |
if (match != null) |
305 |
return match; |
|
306 |
} |
|
307 |
} |
|
308 |
return null; |
|
309 |
} |
|
310 |
||
311 |
/** |
|
43591
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
312 |
* Returns the tree for an attribute given an enclosing attribute to |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
313 |
* search (recursively) and the enclosing attribute's corresponding tree. |
10 | 314 |
* Returns null if the tree cannot be found. |
315 |
*/ |
|
43591
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
316 |
private JCTree matchAttributeToTree(final Attribute findme, |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
317 |
final Attribute attr, |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
318 |
final JCTree tree) { |
10 | 319 |
if (attr == findme) |
43591
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
320 |
return tree; |
10 | 321 |
|
322 |
class Vis implements Attribute.Visitor { |
|
323 |
JCTree result = null; |
|
324 |
public void visitConstant(Attribute.Constant value) { |
|
325 |
} |
|
326 |
public void visitClass(Attribute.Class clazz) { |
|
327 |
} |
|
328 |
public void visitCompound(Attribute.Compound anno) { |
|
329 |
for (Pair<MethodSymbol, Attribute> pair : anno.values) { |
|
330 |
JCExpression expr = scanForAssign(pair.fst, tree); |
|
331 |
if (expr != null) { |
|
43591
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
332 |
JCTree match = matchAttributeToTree(findme, pair.snd, expr); |
10 | 333 |
if (match != null) { |
334 |
result = match; |
|
335 |
return; |
|
336 |
} |
|
337 |
} |
|
338 |
} |
|
339 |
} |
|
340 |
public void visitArray(Attribute.Array array) { |
|
43591
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
341 |
if (tree.hasTag(NEWARRAY)) { |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
342 |
List<JCExpression> elems = ((JCNewArray)tree).elems; |
10 | 343 |
for (Attribute value : array.values) { |
43591
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
344 |
JCTree match = matchAttributeToTree(findme, value, elems.head); |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
345 |
if (match != null) { |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
346 |
result = match; |
10 | 347 |
return; |
348 |
} |
|
349 |
elems = elems.tail; |
|
350 |
} |
|
43591
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
351 |
} else if (array.values.length == 1) { |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
352 |
// the tree may not be a NEWARRAY for single-element array initializers |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
353 |
result = matchAttributeToTree(findme, array.values[0], tree); |
10 | 354 |
} |
355 |
} |
|
356 |
public void visitEnum(Attribute.Enum e) { |
|
357 |
} |
|
358 |
public void visitError(Attribute.Error e) { |
|
359 |
} |
|
360 |
} |
|
361 |
Vis vis = new Vis(); |
|
362 |
attr.accept(vis); |
|
363 |
return vis.result; |
|
364 |
} |
|
365 |
||
366 |
/** |
|
367 |
* Scans for a JCAssign node with a LHS matching a given |
|
368 |
* symbol, and returns its RHS. Does not scan nested JCAnnotations. |
|
369 |
*/ |
|
370 |
private JCExpression scanForAssign(final MethodSymbol sym, |
|
371 |
final JCTree tree) { |
|
372 |
class TS extends TreeScanner { |
|
373 |
JCExpression result = null; |
|
374 |
public void scan(JCTree t) { |
|
375 |
if (t != null && result == null) |
|
376 |
t.accept(this); |
|
377 |
} |
|
378 |
public void visitAnnotation(JCAnnotation t) { |
|
379 |
if (t == tree) |
|
380 |
scan(t.args); |
|
381 |
} |
|
382 |
public void visitAssign(JCAssign t) { |
|
10950 | 383 |
if (t.lhs.hasTag(IDENT)) { |
10 | 384 |
JCIdent ident = (JCIdent) t.lhs; |
385 |
if (ident.sym == sym) |
|
386 |
result = t.rhs; |
|
387 |
} |
|
388 |
} |
|
389 |
} |
|
390 |
TS scanner = new TS(); |
|
391 |
tree.accept(scanner); |
|
392 |
return scanner.result; |
|
393 |
} |
|
394 |
||
395 |
/** |
|
396 |
* Returns the tree node corresponding to this element, or null |
|
397 |
* if none can be found. |
|
398 |
*/ |
|
399 |
public JCTree getTree(Element e) { |
|
400 |
Pair<JCTree, ?> treeTop = getTreeAndTopLevel(e); |
|
401 |
return (treeTop != null) ? treeTop.fst : null; |
|
402 |
} |
|
403 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
404 |
@DefinedBy(Api.LANGUAGE_MODEL) |
10 | 405 |
public String getDocComment(Element e) { |
406 |
// Our doc comment is contained in a map in our toplevel, |
|
407 |
// indexed by our tree. Find our enter environment, which gives |
|
408 |
// us our toplevel. It also gives us a tree that contains our |
|
409 |
// tree: walk it to find our tree. This is painful. |
|
410 |
Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e); |
|
411 |
if (treeTop == null) |
|
412 |
return null; |
|
413 |
JCTree tree = treeTop.fst; |
|
414 |
JCCompilationUnit toplevel = treeTop.snd; |
|
415 |
if (toplevel.docComments == null) |
|
416 |
return null; |
|
13077 | 417 |
return toplevel.docComments.getCommentText(tree); |
10 | 418 |
} |
419 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
420 |
@DefinedBy(Api.LANGUAGE_MODEL) |
10 | 421 |
public PackageElement getPackageOf(Element e) { |
422 |
return cast(Symbol.class, e).packge(); |
|
423 |
} |
|
424 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
425 |
@DefinedBy(Api.LANGUAGE_MODEL) |
36526 | 426 |
public ModuleElement getModuleOf(Element e) { |
427 |
Symbol sym = cast(Symbol.class, e); |
|
42824 | 428 |
if (modules.getDefaultModule() == syms.noModule) |
429 |
return null; |
|
36526 | 430 |
return (sym.kind == MDL) ? ((ModuleElement) e) : sym.packge().modle; |
431 |
} |
|
432 |
||
433 |
@DefinedBy(Api.LANGUAGE_MODEL) |
|
10 | 434 |
public boolean isDeprecated(Element e) { |
435 |
Symbol sym = cast(Symbol.class, e); |
|
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37848
diff
changeset
|
436 |
sym.complete(); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
37848
diff
changeset
|
437 |
return sym.isDeprecated(); |
10 | 438 |
} |
439 |
||
42826 | 440 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
441 |
public Origin getOrigin(Element e) { |
|
442 |
Symbol sym = cast(Symbol.class, e); |
|
443 |
if ((sym.flags() & Flags.GENERATEDCONSTR) != 0) |
|
444 |
return Origin.MANDATED; |
|
445 |
//TypeElement.getEnclosedElements does not return synthetic elements, |
|
446 |
//and most synthetic elements are not read from the classfile anyway: |
|
447 |
return Origin.EXPLICIT; |
|
448 |
} |
|
449 |
||
450 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
|
451 |
public Origin getOrigin(AnnotatedConstruct c, AnnotationMirror a) { |
|
452 |
Compound ac = cast(Compound.class, a); |
|
453 |
if (ac.isSynthesized()) |
|
454 |
return Origin.MANDATED; |
|
455 |
return Origin.EXPLICIT; |
|
456 |
} |
|
457 |
||
458 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
|
459 |
public Origin getOrigin(ModuleElement m, ModuleElement.Directive directive) { |
|
460 |
switch (directive.getKind()) { |
|
461 |
case REQUIRES: |
|
462 |
RequiresDirective rd = cast(RequiresDirective.class, directive); |
|
463 |
if (rd.flags.contains(RequiresFlag.MANDATED)) |
|
464 |
return Origin.MANDATED; |
|
465 |
if (rd.flags.contains(RequiresFlag.SYNTHETIC)) |
|
466 |
return Origin.SYNTHETIC; |
|
467 |
return Origin.EXPLICIT; |
|
468 |
case EXPORTS: |
|
469 |
ExportsDirective ed = cast(ExportsDirective.class, directive); |
|
470 |
if (ed.flags.contains(ExportsFlag.MANDATED)) |
|
471 |
return Origin.MANDATED; |
|
472 |
if (ed.flags.contains(ExportsFlag.SYNTHETIC)) |
|
473 |
return Origin.SYNTHETIC; |
|
474 |
return Origin.EXPLICIT; |
|
475 |
case OPENS: |
|
476 |
OpensDirective od = cast(OpensDirective.class, directive); |
|
477 |
if (od.flags.contains(OpensFlag.MANDATED)) |
|
478 |
return Origin.MANDATED; |
|
479 |
if (od.flags.contains(OpensFlag.SYNTHETIC)) |
|
480 |
return Origin.SYNTHETIC; |
|
481 |
return Origin.EXPLICIT; |
|
482 |
} |
|
483 |
return Origin.EXPLICIT; |
|
484 |
} |
|
485 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
486 |
@DefinedBy(Api.LANGUAGE_MODEL) |
10 | 487 |
public Name getBinaryName(TypeElement type) { |
488 |
return cast(TypeSymbol.class, type).flatName(); |
|
489 |
} |
|
490 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
491 |
@DefinedBy(Api.LANGUAGE_MODEL) |
10 | 492 |
public Map<MethodSymbol, Attribute> getElementValuesWithDefaults( |
493 |
AnnotationMirror a) { |
|
494 |
Attribute.Compound anno = cast(Attribute.Compound.class, a); |
|
495 |
DeclaredType annotype = a.getAnnotationType(); |
|
496 |
Map<MethodSymbol, Attribute> valmap = anno.getElementValues(); |
|
497 |
||
498 |
for (ExecutableElement ex : |
|
499 |
methodsIn(annotype.asElement().getEnclosedElements())) { |
|
500 |
MethodSymbol meth = (MethodSymbol) ex; |
|
501 |
Attribute defaultValue = meth.getDefaultValue(); |
|
502 |
if (defaultValue != null && !valmap.containsKey(meth)) { |
|
503 |
valmap.put(meth, defaultValue); |
|
504 |
} |
|
505 |
} |
|
506 |
return valmap; |
|
507 |
} |
|
508 |
||
509 |
/** |
|
510 |
* {@inheritDoc} |
|
511 |
*/ |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
512 |
@DefinedBy(Api.LANGUAGE_MODEL) |
10 | 513 |
public FilteredMemberList getAllMembers(TypeElement element) { |
514 |
Symbol sym = cast(Symbol.class, element); |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
24069
diff
changeset
|
515 |
WriteableScope scope = sym.members().dupUnshared(); |
10 | 516 |
List<Type> closure = types.closure(sym.asType()); |
517 |
for (Type t : closure) |
|
518 |
addMembers(scope, t); |
|
519 |
return new FilteredMemberList(scope); |
|
520 |
} |
|
521 |
// where |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
24069
diff
changeset
|
522 |
private void addMembers(WriteableScope scope, Type type) { |
10 | 523 |
members: |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
24069
diff
changeset
|
524 |
for (Symbol e : type.asElement().members().getSymbols(NON_RECURSIVE)) { |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
24069
diff
changeset
|
525 |
for (Symbol overrider : scope.getSymbolsByName(e.getSimpleName())) { |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
24069
diff
changeset
|
526 |
if (overrider.kind == e.kind && (overrider.flags() & Flags.SYNTHETIC) == 0) { |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
24069
diff
changeset
|
527 |
if (overrider.getKind() == ElementKind.METHOD && |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
24069
diff
changeset
|
528 |
overrides((ExecutableElement)overrider, (ExecutableElement)e, (TypeElement)type.asElement())) { |
10 | 529 |
continue members; |
530 |
} |
|
531 |
} |
|
532 |
} |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
24069
diff
changeset
|
533 |
boolean derived = e.getEnclosingElement() != scope.owner; |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
24069
diff
changeset
|
534 |
ElementKind kind = e.getKind(); |
10 | 535 |
boolean initializer = kind == ElementKind.CONSTRUCTOR |
536 |
|| kind == ElementKind.INSTANCE_INIT |
|
537 |
|| kind == ElementKind.STATIC_INIT; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
24069
diff
changeset
|
538 |
if (!derived || (!initializer && e.isInheritedIn(scope.owner, types))) |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
24069
diff
changeset
|
539 |
scope.enter(e); |
10 | 540 |
} |
541 |
} |
|
542 |
||
543 |
/** |
|
544 |
* Returns all annotations of an element, whether |
|
545 |
* inherited or directly present. |
|
546 |
* |
|
547 |
* @param e the element being examined |
|
548 |
* @return all annotations of the element |
|
549 |
*/ |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
550 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
10 | 551 |
public List<Attribute.Compound> getAllAnnotationMirrors(Element e) { |
552 |
Symbol sym = cast(Symbol.class, e); |
|
17278
a48ec76f26e9
8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents:
16557
diff
changeset
|
553 |
List<Attribute.Compound> annos = sym.getAnnotationMirrors(); |
10 | 554 |
while (sym.getKind() == ElementKind.CLASS) { |
555 |
Type sup = ((ClassSymbol) sym).getSuperclass(); |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
13077
diff
changeset
|
556 |
if (!sup.hasTag(CLASS) || sup.isErroneous() || |
10 | 557 |
sup.tsym == syms.objectType.tsym) { |
558 |
break; |
|
559 |
} |
|
560 |
sym = sup.tsym; |
|
561 |
List<Attribute.Compound> oldAnnos = annos; |
|
17278
a48ec76f26e9
8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents:
16557
diff
changeset
|
562 |
List<Attribute.Compound> newAnnos = sym.getAnnotationMirrors(); |
15355
a4757c33cae9
7193719: Support repeating annotations in javax.lang.model
jfranck
parents:
14359
diff
changeset
|
563 |
for (Attribute.Compound anno : newAnnos) { |
10 | 564 |
if (isInherited(anno.type) && |
565 |
!containsAnnoOfType(oldAnnos, anno.type)) { |
|
566 |
annos = annos.prepend(anno); |
|
567 |
} |
|
568 |
} |
|
569 |
} |
|
570 |
return annos; |
|
571 |
} |
|
572 |
||
573 |
/** |
|
574 |
* Tests whether an annotation type is @Inherited. |
|
575 |
*/ |
|
576 |
private boolean isInherited(Type annotype) { |
|
15355
a4757c33cae9
7193719: Support repeating annotations in javax.lang.model
jfranck
parents:
14359
diff
changeset
|
577 |
return annotype.tsym.attribute(syms.inheritedType.tsym) != null; |
10 | 578 |
} |
579 |
||
580 |
/** |
|
581 |
* Tests whether a list of annotations contains an annotation |
|
582 |
* of a given type. |
|
583 |
*/ |
|
584 |
private static boolean containsAnnoOfType(List<Attribute.Compound> annos, |
|
585 |
Type type) { |
|
586 |
for (Attribute.Compound anno : annos) { |
|
587 |
if (anno.type.tsym == type.tsym) |
|
588 |
return true; |
|
589 |
} |
|
590 |
return false; |
|
591 |
} |
|
592 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
593 |
@DefinedBy(Api.LANGUAGE_MODEL) |
10 | 594 |
public boolean hides(Element hiderEl, Element hideeEl) { |
595 |
Symbol hider = cast(Symbol.class, hiderEl); |
|
596 |
Symbol hidee = cast(Symbol.class, hideeEl); |
|
597 |
||
598 |
// Fields only hide fields; methods only methods; types only types. |
|
599 |
// Names must match. Nothing hides itself (just try it). |
|
600 |
if (hider == hidee || |
|
601 |
hider.kind != hidee.kind || |
|
602 |
hider.name != hidee.name) { |
|
603 |
return false; |
|
604 |
} |
|
605 |
||
606 |
// Only static methods can hide other methods. |
|
607 |
// Methods only hide methods with matching signatures. |
|
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
26266
diff
changeset
|
608 |
if (hider.kind == MTH) { |
10 | 609 |
if (!hider.isStatic() || |
610 |
!types.isSubSignature(hider.type, hidee.type)) { |
|
611 |
return false; |
|
612 |
} |
|
613 |
} |
|
614 |
||
615 |
// Hider must be in a subclass of hidee's class. |
|
616 |
// Note that if M1 hides M2, and M2 hides M3, and M3 is accessible |
|
617 |
// in M1's class, then M1 and M2 both hide M3. |
|
618 |
ClassSymbol hiderClass = hider.owner.enclClass(); |
|
619 |
ClassSymbol hideeClass = hidee.owner.enclClass(); |
|
620 |
if (hiderClass == null || hideeClass == null || |
|
621 |
!hiderClass.isSubClass(hideeClass, types)) { |
|
622 |
return false; |
|
623 |
} |
|
624 |
||
625 |
// Hidee must be accessible in hider's class. |
|
626 |
// The method isInheritedIn is poorly named: it checks only access. |
|
627 |
return hidee.isInheritedIn(hiderClass, types); |
|
628 |
} |
|
629 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
630 |
@DefinedBy(Api.LANGUAGE_MODEL) |
10 | 631 |
public boolean overrides(ExecutableElement riderEl, |
632 |
ExecutableElement rideeEl, TypeElement typeEl) { |
|
633 |
MethodSymbol rider = cast(MethodSymbol.class, riderEl); |
|
634 |
MethodSymbol ridee = cast(MethodSymbol.class, rideeEl); |
|
635 |
ClassSymbol origin = cast(ClassSymbol.class, typeEl); |
|
636 |
||
637 |
return rider.name == ridee.name && |
|
638 |
||
639 |
// not reflexive as per JLS |
|
640 |
rider != ridee && |
|
641 |
||
642 |
// we don't care if ridee is static, though that wouldn't |
|
643 |
// compile |
|
644 |
!rider.isStatic() && |
|
645 |
||
646 |
// Symbol.overrides assumes the following |
|
647 |
ridee.isMemberOf(origin, types) && |
|
648 |
||
649 |
// check access and signatures; don't check return types |
|
650 |
rider.overrides(ridee, origin, types, false); |
|
651 |
} |
|
652 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
653 |
@DefinedBy(Api.LANGUAGE_MODEL) |
10 | 654 |
public String getConstantExpression(Object value) { |
655 |
return Constants.format(value); |
|
656 |
} |
|
657 |
||
658 |
/** |
|
659 |
* Print a representation of the elements to the given writer in |
|
660 |
* the specified order. The main purpose of this method is for |
|
661 |
* diagnostics. The exact format of the output is <em>not</em> |
|
662 |
* specified and is subject to change. |
|
663 |
* |
|
664 |
* @param w the writer to print the output to |
|
665 |
* @param elements the elements to print |
|
666 |
*/ |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
667 |
@DefinedBy(Api.LANGUAGE_MODEL) |
10 | 668 |
public void printElements(java.io.Writer w, Element... elements) { |
669 |
for (Element element : elements) |
|
670 |
(new PrintingProcessor.PrintingElementVisitor(w, this)).visit(element).flush(); |
|
671 |
} |
|
672 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
673 |
@DefinedBy(Api.LANGUAGE_MODEL) |
10 | 674 |
public Name getName(CharSequence cs) { |
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
10
diff
changeset
|
675 |
return names.fromString(cs.toString()); |
10 | 676 |
} |
677 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
678 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
15714
d9a83ab62853
8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents:
15365
diff
changeset
|
679 |
public boolean isFunctionalInterface(TypeElement element) { |
d9a83ab62853
8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents:
15365
diff
changeset
|
680 |
if (element.getKind() != ElementKind.INTERFACE) |
d9a83ab62853
8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents:
15365
diff
changeset
|
681 |
return false; |
d9a83ab62853
8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents:
15365
diff
changeset
|
682 |
else { |
d9a83ab62853
8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents:
15365
diff
changeset
|
683 |
TypeSymbol tsym = cast(TypeSymbol.class, element); |
d9a83ab62853
8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents:
15365
diff
changeset
|
684 |
return types.isFunctionalInterface(tsym); |
d9a83ab62853
8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents:
15365
diff
changeset
|
685 |
} |
d9a83ab62853
8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents:
15365
diff
changeset
|
686 |
} |
d9a83ab62853
8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents:
15365
diff
changeset
|
687 |
|
10 | 688 |
/** |
689 |
* Returns the tree node and compilation unit corresponding to this |
|
690 |
* element, or null if they can't be found. |
|
691 |
*/ |
|
692 |
private Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(Element e) { |
|
693 |
Symbol sym = cast(Symbol.class, e); |
|
694 |
Env<AttrContext> enterEnv = getEnterEnv(sym); |
|
695 |
if (enterEnv == null) |
|
696 |
return null; |
|
697 |
JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree); |
|
698 |
if (tree == null || enterEnv.toplevel == null) |
|
699 |
return null; |
|
22163 | 700 |
return new Pair<>(tree, enterEnv.toplevel); |
10 | 701 |
} |
702 |
||
703 |
/** |
|
704 |
* Returns the best approximation for the tree node and compilation unit |
|
705 |
* corresponding to the given element, annotation and value. |
|
706 |
* If the element is null, null is returned. |
|
707 |
* If the annotation is null or cannot be found, the tree node and |
|
708 |
* compilation unit for the element is returned. |
|
709 |
* If the annotation value is null or cannot be found, the tree node and |
|
710 |
* compilation unit for the annotation is returned. |
|
711 |
*/ |
|
712 |
public Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel( |
|
713 |
Element e, AnnotationMirror a, AnnotationValue v) { |
|
714 |
if (e == null) |
|
715 |
return null; |
|
716 |
||
717 |
Pair<JCTree, JCCompilationUnit> elemTreeTop = getTreeAndTopLevel(e); |
|
718 |
if (elemTreeTop == null) |
|
719 |
return null; |
|
720 |
||
721 |
if (a == null) |
|
722 |
return elemTreeTop; |
|
723 |
||
724 |
JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst); |
|
725 |
if (annoTree == null) |
|
726 |
return elemTreeTop; |
|
727 |
||
43591
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
728 |
if (v == null) |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
729 |
return new Pair<>(annoTree, elemTreeTop.snd); |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
730 |
|
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
731 |
JCTree valueTree = matchAttributeToTree( |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
732 |
cast(Attribute.class, v), cast(Attribute.class, a), annoTree); |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
733 |
if (valueTree == null) |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
734 |
return new Pair<>(annoTree, elemTreeTop.snd); |
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
735 |
|
62824732af55
6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents:
43369
diff
changeset
|
736 |
return new Pair<>(valueTree, elemTreeTop.snd); |
10 | 737 |
} |
738 |
||
739 |
/** |
|
740 |
* Returns a symbol's enter environment, or null if it has none. |
|
741 |
*/ |
|
742 |
private Env<AttrContext> getEnterEnv(Symbol sym) { |
|
743 |
// Get enclosing class of sym, or sym itself if it is a class |
|
36779
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36526
diff
changeset
|
744 |
// package, or module. |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36526
diff
changeset
|
745 |
TypeSymbol ts = null; |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36526
diff
changeset
|
746 |
switch (sym.kind) { |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36526
diff
changeset
|
747 |
case PCK: |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36526
diff
changeset
|
748 |
ts = (PackageSymbol)sym; |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36526
diff
changeset
|
749 |
break; |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36526
diff
changeset
|
750 |
case MDL: |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36526
diff
changeset
|
751 |
ts = (ModuleSymbol)sym; |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36526
diff
changeset
|
752 |
break; |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36526
diff
changeset
|
753 |
default: |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36526
diff
changeset
|
754 |
ts = sym.enclClass(); |
9a030c4d2591
8152771: NPE accessing comments on module declarations
ksrini
parents:
36526
diff
changeset
|
755 |
} |
10 | 756 |
return (ts != null) |
757 |
? enter.getEnv(ts) |
|
758 |
: null; |
|
759 |
} |
|
760 |
||
36526 | 761 |
private void ensureEntered(String methodName) { |
762 |
if (javacTaskImpl != null) { |
|
763 |
javacTaskImpl.ensureEntered(); |
|
764 |
} |
|
765 |
if (!javaCompiler.isEnterDone()) { |
|
766 |
throw new IllegalStateException("Cannot use Elements." + methodName + " before the TaskEvent.Kind.ENTER finished event."); |
|
767 |
} |
|
768 |
} |
|
769 |
||
10 | 770 |
/** |
771 |
* Returns an object cast to the specified type. |
|
772 |
* @throws NullPointerException if the object is {@code null} |
|
773 |
* @throws IllegalArgumentException if the object is of the wrong type |
|
774 |
*/ |
|
775 |
private static <T> T cast(Class<T> clazz, Object o) { |
|
776 |
if (! clazz.isInstance(o)) |
|
777 |
throw new IllegalArgumentException(o.toString()); |
|
778 |
return clazz.cast(o); |
|
779 |
} |
|
780 |
} |