Lines 93-129
Link Here
|
93 |
|
93 |
|
94 |
private SourceUtils() {} |
94 |
private SourceUtils() {} |
95 |
|
95 |
|
96 |
/** |
|
|
97 |
* @deprecated Use {@link com.sun.source.util.Trees#getTree(Element)} instead. |
98 |
*/ |
99 |
public static Tree treeFor(CompilationInfo info, Element element) { |
100 |
Context ctx = getSourceContextFor(info.getClasspathInfo(), Phase.ELEMENTS_RESOLVED, element); |
101 |
if (ctx != null) { |
102 |
Element e = getSourceElementFor(element, ctx); |
103 |
if (e != null) |
104 |
return JavacElements.instance(ctx).getTree((Symbol)e); |
105 |
} |
106 |
return null; |
107 |
} |
108 |
|
109 |
/** |
110 |
* @deprecated Use {@link com.sun.source.util.Trees#getPath(Element)} instead. |
111 |
*/ |
112 |
public static TreePath pathFor(CompilationInfo info, Element element) { |
113 |
Context ctx = getSourceContextFor(info.getClasspathInfo(), Phase.ELEMENTS_RESOLVED, element); |
114 |
if (ctx != null) { |
115 |
Element e = getSourceElementFor(element, ctx); |
116 |
if (e != null) |
117 |
return JavacTrees.instance(ctx).getPath(e); |
118 |
} |
119 |
return null; |
120 |
} |
121 |
|
122 |
public static Element getImplementationOf(CompilationInfo info, ExecutableElement method, TypeElement origin) { |
123 |
Context c = ((JavacTaskImpl) info.getJavacTask()).getContext(); |
124 |
return ((MethodSymbol)method).implementation((TypeSymbol)origin, com.sun.tools.javac.code.Types.instance(c), true); |
125 |
} |
126 |
|
127 |
public static boolean checkTypesAssignable(CompilationInfo info, TypeMirror from, TypeMirror to) { |
96 |
public static boolean checkTypesAssignable(CompilationInfo info, TypeMirror from, TypeMirror to) { |
128 |
Context c = ((JavacTaskImpl) info.getJavacTask()).getContext(); |
97 |
Context c = ((JavacTaskImpl) info.getJavacTask()).getContext(); |
129 |
if (from.getKind() == TypeKind.DECLARED) { |
98 |
if (from.getKind() == TypeKind.DECLARED) { |
Lines 195-203
Link Here
|
195 |
return (TypeElement)ec; |
164 |
return (TypeElement)ec; |
196 |
} |
165 |
} |
197 |
|
166 |
|
198 |
private static EnumSet JAVA_JFO_KIND = EnumSet.of(Kind.CLASS, Kind.SOURCE); |
|
|
199 |
|
200 |
|
201 |
/**Resolve full qualified name in the given context. Adds import statement as necessary. |
167 |
/**Resolve full qualified name in the given context. Adds import statement as necessary. |
202 |
* Returns name that resolved to a given FQN in given context (either simple name |
168 |
* Returns name that resolved to a given FQN in given context (either simple name |
203 |
* or full qualified name). Handles import conflicts. |
169 |
* or full qualified name). Handles import conflicts. |
Lines 308-314
Link Here
|
308 |
* |
274 |
* |
309 |
* |
275 |
* |
310 |
*/ |
276 |
*/ |
311 |
public static CompilationUnitTree addImports(CompilationUnitTree cut, List<String> toImport, TreeMaker make) |
277 |
private static CompilationUnitTree addImports(CompilationUnitTree cut, List<String> toImport, TreeMaker make) |
312 |
throws IOException { |
278 |
throws IOException { |
313 |
// do not modify the list given by the caller (may be reused or immutable). |
279 |
// do not modify the list given by the caller (may be reused or immutable). |
314 |
toImport = new ArrayList<String>(toImport); |
280 |
toImport = new ArrayList<String>(toImport); |
Lines 342-383
Link Here
|
342 |
|
308 |
|
343 |
/** |
309 |
/** |
344 |
* Returns a {@link FileObject} in which the Element is defined. |
310 |
* Returns a {@link FileObject} in which the Element is defined. |
345 |
* Element must be defined in source file |
|
|
346 |
* @param element for which the {@link FileObject} should be located |
347 |
* @return the defining {@link FileObject} or null if it cannot be |
348 |
* found in any source file. |
349 |
*/ |
350 |
public static FileObject getFile(Element element) { |
351 |
if (element == null) { |
352 |
throw new IllegalArgumentException ("Cannot pass null as an argument of the SourceUtils.getFile"); //NOI18N |
353 |
} |
354 |
Element prev = null; |
355 |
while (element.getKind() != ElementKind.PACKAGE) { |
356 |
prev = element; |
357 |
element = element.getEnclosingElement(); |
358 |
} |
359 |
if (prev == null || (!prev.getKind().isClass() && !prev.getKind().isInterface())) |
360 |
return null; |
361 |
ClassSymbol clsSym = (ClassSymbol)prev; |
362 |
URI uri; |
363 |
if (clsSym.completer != null) |
364 |
clsSym.complete(); |
365 |
if (clsSym.sourcefile != null && (uri=clsSym.sourcefile.toUri())!= null && uri.isAbsolute()) { |
366 |
try { |
367 |
return URLMapper.findFileObject(uri.toURL()); |
368 |
} catch (MalformedURLException ex) { |
369 |
ex.printStackTrace(); |
370 |
} |
371 |
} |
372 |
return null; |
373 |
} |
374 |
|
375 |
/** |
376 |
* Returns a {@link FileObject} in which the Element is defined. |
377 |
* @param element for which the {@link FileObject} should be located |
311 |
* @param element for which the {@link FileObject} should be located |
378 |
* @param cpInfo the classpaths context |
312 |
* @param cpInfo the classpaths context |
379 |
* @return the defining {@link FileObject} or null if it cannot be |
313 |
* @return the defining {@link FileObject} or null if it cannot be |
380 |
* found |
314 |
* found |
|
|
315 |
* |
316 |
* @deprecated use {@link getFile(ElementHandle, ClasspathInfo)} |
381 |
*/ |
317 |
*/ |
382 |
public static FileObject getFile (Element element, ClasspathInfo cpInfo) { |
318 |
public static FileObject getFile (Element element, ClasspathInfo cpInfo) { |
383 |
try { |
319 |
try { |
Lines 714-719
Link Here
|
714 |
* Waits for the end of the initial scan, this helper method |
650 |
* Waits for the end of the initial scan, this helper method |
715 |
* is designed for tests which require to wait for end of initial scan. |
651 |
* is designed for tests which require to wait for end of initial scan. |
716 |
* @throws InterruptedException is thrown when the waiting thread is interrupted. |
652 |
* @throws InterruptedException is thrown when the waiting thread is interrupted. |
|
|
653 |
* @deprecated use {@link JavaSource#runWhenScanFinished} |
717 |
*/ |
654 |
*/ |
718 |
public static void waitScanFinished () throws InterruptedException { |
655 |
public static void waitScanFinished () throws InterruptedException { |
719 |
RepositoryUpdater.getDefault().waitScanFinished(); |
656 |
RepositoryUpdater.getDefault().waitScanFinished(); |
Lines 969-1040
Link Here
|
969 |
|
906 |
|
970 |
// --------------- End of getFile () helper methods ------------------------------ |
907 |
// --------------- End of getFile () helper methods ------------------------------ |
971 |
|
908 |
|
972 |
private static Context getSourceContextFor(ClasspathInfo cpInfo, final JavaSource.Phase phase, Element element) { |
|
|
973 |
try { |
974 |
FileObject fo = getFile(element, cpInfo); |
975 |
if (fo != null) { |
976 |
JavaSource js = JavaSource.forFileObject(fo); |
977 |
if (js != null) { |
978 |
final Context[] ret = new Context[1]; |
979 |
js.runUserActionTask(new CancellableTask<CompilationController>() { |
980 |
public void cancel() { |
981 |
} |
982 |
public void run(CompilationController controller) throws Exception { |
983 |
controller.toPhase(phase); |
984 |
ret[0] = controller.getJavacTask().getContext(); |
985 |
} |
986 |
},true); |
987 |
return ret[0]; |
988 |
} |
989 |
} |
990 |
} catch (IOException ex) { |
991 |
Exceptions.printStackTrace(ex); |
992 |
} |
993 |
return null; |
994 |
} |
995 |
|
996 |
private static Element getSourceElementFor(Element element, Context ctx) { |
997 |
Symbol sym = (Symbol)element; |
998 |
Symtab symbolTable = Symtab.instance(ctx); |
999 |
Name.Table nameTable = Name.Table.instance(ctx); |
1000 |
Symbol owner = sym.owner; |
1001 |
ClassSymbol enclCls = sym.enclClass(); |
1002 |
Name name = nameTable.fromString(enclCls.flatname.toString()); |
1003 |
ClassSymbol cls = symbolTable.classes.get(name); |
1004 |
if (enclCls == sym) |
1005 |
return cls; |
1006 |
if (cls != null && owner == enclCls) { |
1007 |
com.sun.tools.javac.code.Scope.Entry e = cls.members().lookup(nameTable.fromString(sym.name.toString())); |
1008 |
while (e.scope != null) { |
1009 |
if (e.sym.kind == sym.kind && (e.sym.flags_field & Flags.SYNTHETIC) == 0 && |
1010 |
e.sym.type.toString().equals(sym.type.toString())) |
1011 |
return e.sym; |
1012 |
e = e.next(); |
1013 |
} |
1014 |
} else if (cls != null && owner.kind == Kinds.MTH && sym.kind == Kinds.VAR) { |
1015 |
com.sun.tools.javac.code.Scope.Entry e = cls.members().lookup(nameTable.fromString(owner.name.toString())); |
1016 |
Symbol newOwner = null; |
1017 |
while (e.scope != null) { |
1018 |
if (e.sym.kind == owner.kind && (e.sym.flags_field & Flags.SYNTHETIC) == 0 && |
1019 |
e.sym.type.toString().equals(owner.type.toString())) { |
1020 |
newOwner = e.sym; |
1021 |
break; |
1022 |
} |
1023 |
e = e.next(); |
1024 |
} |
1025 |
if (newOwner != null && newOwner.kind == Kinds.MTH) { |
1026 |
int i = 0; |
1027 |
for (com.sun.tools.javac.util.List<VarSymbol> l = ((MethodSymbol)owner).params; l.nonEmpty(); l = l.tail) { |
1028 |
i++; |
1029 |
if (sym == l.head) |
1030 |
break; |
1031 |
} |
1032 |
for (com.sun.tools.javac.util.List<VarSymbol> l = ((MethodSymbol)newOwner).params; l.nonEmpty(); l = l.tail) { |
1033 |
if (--i == 0) |
1034 |
return l.head; |
1035 |
} |
1036 |
} |
1037 |
} |
1038 |
return null; |
1039 |
} |
1040 |
} |
909 |
} |