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