Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.api.debugger.jpda; |
43 |
|
44 |
import java.io.IOException; |
45 |
import java.util.ArrayList; |
46 |
import java.util.Collection; |
47 |
import java.util.Collections; |
48 |
import java.util.LinkedHashSet; |
49 |
import java.util.List; |
50 |
import java.util.Set; |
51 |
import javax.lang.model.element.Element; |
52 |
import javax.lang.model.element.ElementKind; |
53 |
import javax.lang.model.element.ExecutableElement; |
54 |
import javax.lang.model.element.Modifier; |
55 |
import javax.lang.model.element.TypeElement; |
56 |
import javax.lang.model.element.VariableElement; |
57 |
import javax.lang.model.type.ArrayType; |
58 |
import javax.lang.model.type.DeclaredType; |
59 |
import javax.lang.model.type.TypeMirror; |
60 |
import javax.lang.model.util.ElementScanner6; |
61 |
import javax.lang.model.util.Types; |
62 |
import org.netbeans.api.java.source.ClassIndex; |
63 |
import org.netbeans.api.java.source.ClassIndexListener; |
64 |
import org.netbeans.api.java.source.CompilationController; |
65 |
import org.netbeans.api.java.source.ElementHandle; |
66 |
import org.netbeans.api.java.source.ElementUtilities; |
67 |
import org.netbeans.api.java.source.JavaSource; |
68 |
import org.netbeans.api.java.source.RootsEvent; |
69 |
import org.netbeans.api.java.source.Task; |
70 |
import org.netbeans.api.java.source.TypesEvent; |
71 |
import org.openide.filesystems.FileObject; |
72 |
import org.openide.filesystems.FileUtil; |
73 |
import org.openide.util.Exceptions; |
74 |
import org.openide.util.Parameters; |
75 |
|
76 |
/** |
77 |
* Java source utilities. |
78 |
* The main purpose is to track changes in Java elements. |
79 |
* |
80 |
* @author Martin Entlicher |
81 |
*/ |
82 |
final class JavaSourceUtils { |
83 |
|
84 |
private JavaSourceUtils() {} |
85 |
|
86 |
static Collection<ElementHandle<TypeElement>> getClasses (final FileObject fo) { |
87 |
Parameters.notNull("fo", fo); //NOI18N |
88 |
if (!fo.isValid()) { |
89 |
throw new IllegalArgumentException ("FileObject : " + FileUtil.getFileDisplayName(fo) + " is not valid."); //NOI18N |
90 |
} |
91 |
if (fo.isVirtual()) { |
92 |
throw new IllegalArgumentException ("FileObject : " + FileUtil.getFileDisplayName(fo) + " is virtual."); //NOI18N |
93 |
} |
94 |
final JavaSource js = JavaSource.forFileObject(fo); |
95 |
if (js == null) { |
96 |
throw new IllegalArgumentException (); |
97 |
} |
98 |
try { |
99 |
final LinkedHashSet<ElementHandle<TypeElement>> result = new LinkedHashSet<ElementHandle<TypeElement>>(); |
100 |
js.runUserActionTask(new Task<CompilationController>() { |
101 |
@Override |
102 |
public void run(final CompilationController control) throws Exception { |
103 |
if (control.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED).compareTo (JavaSource.Phase.ELEMENTS_RESOLVED)>=0) { |
104 |
final List<TypeElement> types = new ArrayList<TypeElement>(); |
105 |
final ElementScanner6<Void,Void> visitor = new ElementScanner6<Void, Void>() { |
106 |
@Override |
107 |
public Void visitType(TypeElement e, Void p) { |
108 |
if (e.getEnclosingElement().getKind() == ElementKind.PACKAGE |
109 |
|| e.getModifiers().contains(Modifier.STATIC)) { |
110 |
types.add(e); |
111 |
return super.visitType(e, p); |
112 |
} else { |
113 |
return null; |
114 |
} |
115 |
} |
116 |
}; |
117 |
visitor.scan(control.getTopLevelElements(), null); |
118 |
for (TypeElement type : types) { |
119 |
result.add (ElementHandle.create(type)); |
120 |
} |
121 |
} |
122 |
} |
123 |
|
124 |
}, true); |
125 |
return result; |
126 |
} catch (IOException ioe) { |
127 |
Exceptions.printStackTrace(ioe); |
128 |
return Collections.<ElementHandle<TypeElement>>emptySet(); |
129 |
} |
130 |
} |
131 |
|
132 |
static ElementChangeTracker createElementChangeTracker(JavaSource js, String clazz, String name, String signature) { |
133 |
ClassIndex classIndex = js.getClasspathInfo().getClassIndex(); |
134 |
String packageName = ""; |
135 |
int pkgIndex = clazz.lastIndexOf('.'); |
136 |
if (pkgIndex > 0) { |
137 |
packageName = clazz.substring(0, pkgIndex); |
138 |
} |
139 |
ClassIndex.SearchScopeType scopeType = new JavaSourceUtils.PackageSearchType(packageName); |
140 |
String prefix = (pkgIndex > 0) ? clazz.substring(pkgIndex+1) : clazz; |
141 |
int i = prefix.lastIndexOf('$'); |
142 |
if (i > 0) { |
143 |
prefix = prefix.substring(i+1); |
144 |
} |
145 |
ElementHandle<TypeElement> classElement = null; |
146 |
Set<ElementHandle<TypeElement>> declaredTypes = classIndex.getDeclaredTypes(prefix, ClassIndex.NameKind.PREFIX, Collections.singleton(scopeType)); |
147 |
//System.err.println("Declared types of "+clazz+" = "+declaredTypes); |
148 |
for (ElementHandle<TypeElement> dt : declaredTypes) { |
149 |
if (clazz.equals(dt.getBinaryName())) { |
150 |
classElement = dt; |
151 |
} |
152 |
} |
153 |
//System.err.println("Class Element of "+clazz+" = "+classElement); |
154 |
if (classElement == null) { |
155 |
return null; |
156 |
} |
157 |
return new ElementChangeTracker(js, classIndex, classElement, name, signature); |
158 |
} |
159 |
|
160 |
static final class ElementChangeTracker { |
161 |
|
162 |
private final Tracker tracker = new Tracker(); |
163 |
private final JavaSource js; |
164 |
private final ClassIndex classIndex; |
165 |
private final ElementHandle<TypeElement> classElement; |
166 |
private String name; |
167 |
private String signature; |
168 |
private ElementChangeListener ecl; |
169 |
|
170 |
private ElementChangeTracker(JavaSource js, ClassIndex classIndex, ElementHandle<TypeElement> classElement, String name, String signature) { |
171 |
assert classElement != null; |
172 |
this.js = js; |
173 |
this.classIndex = classIndex; |
174 |
this.classElement = classElement; |
175 |
this.name = name; |
176 |
this.signature = signature; |
177 |
classIndex.addClassIndexListener(tracker); |
178 |
} |
179 |
|
180 |
void setName(String name) { |
181 |
this.name = name; |
182 |
} |
183 |
|
184 |
void setSignature(String signature) { |
185 |
this.signature = signature; |
186 |
} |
187 |
|
188 |
void setElementChangeListener(ElementChangeListener ecl) { |
189 |
this.ecl = ecl; |
190 |
} |
191 |
|
192 |
void destroy() { |
193 |
classIndex.removeClassIndexListener(tracker); |
194 |
} |
195 |
|
196 |
class Tracker implements ClassIndexListener { |
197 |
|
198 |
@Override |
199 |
public void typesAdded(TypesEvent event) { |
200 |
for (ElementHandle<TypeElement> eh : event.getTypes()) { |
201 |
String binaryName = eh.getBinaryName(); |
202 |
} |
203 |
} |
204 |
|
205 |
@Override |
206 |
public void typesRemoved(TypesEvent event) { |
207 |
for (ElementHandle<TypeElement> eh : event.getTypes()) { |
208 |
//String binaryName = eh.getBinaryName(); |
209 |
if (classElement.signatureEquals(eh)) { |
210 |
ecl.removed(); |
211 |
} |
212 |
} |
213 |
} |
214 |
|
215 |
@Override |
216 |
public void typesChanged(TypesEvent event) { |
217 |
for (ElementHandle<TypeElement> eh : event.getTypes()) { |
218 |
//String binaryName = eh.getBinaryName(); |
219 |
if (classElement.signatureEquals(eh)) { |
220 |
if (name != null && !name.isEmpty()) { |
221 |
if (!doesElementExist(js, eh, name, signature)) { |
222 |
ecl.removed(); |
223 |
} |
224 |
} |
225 |
//ecl.changed(name); |
226 |
} |
227 |
} |
228 |
} |
229 |
|
230 |
@Override |
231 |
public void rootsAdded(RootsEvent event) { |
232 |
event.toString(); |
233 |
} |
234 |
|
235 |
@Override |
236 |
public void rootsRemoved(RootsEvent event) { |
237 |
event.toString(); |
238 |
} |
239 |
} |
240 |
|
241 |
} |
242 |
|
243 |
static boolean doesElementExist(final JavaSource js, |
244 |
final ElementHandle<TypeElement> typeHandle, |
245 |
final String name, |
246 |
final String signature) { |
247 |
final boolean[] exists = new boolean[] { false }; |
248 |
try { |
249 |
js.runUserActionTask(new Task<CompilationController>() { |
250 |
@Override |
251 |
public void run(CompilationController cc) throws Exception { |
252 |
if (cc.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED).compareTo (JavaSource.Phase.ELEMENTS_RESOLVED) >= 0) { |
253 |
TypeElement type = typeHandle.resolve(cc); |
254 |
if (type == null) { |
255 |
return ; |
256 |
} |
257 |
//List<? extends Element> allMembers = cc.getElements().getAllMembers(type); |
258 |
List<? extends Element> allMembers = type.getEnclosedElements(); |
259 |
for (Element member : allMembers) { |
260 |
if (!member.getSimpleName().contentEquals(name)) { |
261 |
continue; |
262 |
} |
263 |
if (signature == null) { |
264 |
if (!ElementKind.FIELD.equals(member.getKind())) { |
265 |
continue; |
266 |
} |
267 |
exists[0] = true; |
268 |
break; |
269 |
} else { |
270 |
if (!ElementKind.METHOD.equals(member.getKind())) { |
271 |
continue; |
272 |
} |
273 |
ExecutableElement method = (ExecutableElement) member; |
274 |
if (!signature.isEmpty()) { |
275 |
if (!signature.equals(createSignature(method, cc.getTypes()))) { |
276 |
continue; |
277 |
} |
278 |
} |
279 |
exists[0] = true; |
280 |
break; |
281 |
} |
282 |
} |
283 |
} else { |
284 |
exists[0] = true; // Be optimistic |
285 |
} |
286 |
} |
287 |
}, true); |
288 |
} catch (IOException ex) { |
289 |
Exceptions.printStackTrace(ex); |
290 |
} |
291 |
return exists[0]; |
292 |
} |
293 |
|
294 |
private static String createSignature(ExecutableElement elm, Types types) { |
295 |
StringBuilder signature = new StringBuilder("("); |
296 |
for (VariableElement param : elm.getParameters()) { |
297 |
TypeMirror pt = param.asType(); |
298 |
pt = types.erasure(pt); |
299 |
String paramType = getTypeBinaryName(pt); |
300 |
signature.append(getSignature(paramType)); |
301 |
} |
302 |
signature.append(')'); |
303 |
String returnType = getTypeBinaryName(types.erasure(elm.getReturnType())); |
304 |
signature.append(getSignature(returnType)); |
305 |
return signature.toString(); |
306 |
} |
307 |
|
308 |
private static String getTypeBinaryName(TypeMirror t) { |
309 |
if (t instanceof ArrayType) { |
310 |
TypeMirror ct = ((ArrayType) t).getComponentType(); |
311 |
return getTypeBinaryName(ct)+"[]"; |
312 |
} |
313 |
if (t instanceof DeclaredType) { |
314 |
return ElementUtilities.getBinaryName((TypeElement) ((DeclaredType) t).asElement()); |
315 |
} |
316 |
return t.toString(); |
317 |
} |
318 |
|
319 |
private static String getSignature(String javaType) { |
320 |
if (javaType.equals("boolean")) { |
321 |
return "Z"; |
322 |
} else if (javaType.equals("byte")) { |
323 |
return "B"; |
324 |
} else if (javaType.equals("char")) { |
325 |
return "C"; |
326 |
} else if (javaType.equals("short")) { |
327 |
return "S"; |
328 |
} else if (javaType.equals("int")) { |
329 |
return "I"; |
330 |
} else if (javaType.equals("long")) { |
331 |
return "J"; |
332 |
} else if (javaType.equals("float")) { |
333 |
return "F"; |
334 |
} else if (javaType.equals("double")) { |
335 |
return "D"; |
336 |
} else if (javaType.equals("void")) { |
337 |
return "V"; |
338 |
} else if (javaType.endsWith("[]")) { |
339 |
return "["+getSignature(javaType.substring(0, javaType.length() - 2)); |
340 |
} else { |
341 |
return "L"+javaType.replace('.', '/')+";"; |
342 |
} |
343 |
} |
344 |
|
345 |
static interface ElementChangeListener { |
346 |
void removed(); |
347 |
void changed(String name); |
348 |
} |
349 |
|
350 |
static final class PackageSearchType implements ClassIndex.SearchScopeType { |
351 |
|
352 |
private final Set<? extends String> packages; |
353 |
|
354 |
PackageSearchType(String packageName) { |
355 |
packages = Collections.singleton(packageName); |
356 |
} |
357 |
|
358 |
@Override |
359 |
public Set<? extends String> getPackages() { |
360 |
return packages; |
361 |
} |
362 |
|
363 |
@Override |
364 |
public boolean isSources() { |
365 |
return true; |
366 |
} |
367 |
|
368 |
@Override |
369 |
public boolean isDependencies() { |
370 |
return false; |
371 |
} |
372 |
} |
373 |
|
374 |
} |