public class SourceUtils extends Object
Modifier and Type | Method and Description |
---|---|
static boolean |
checkTypesAssignable(CompilationInfo info,
TypeMirror from,
TypeMirror to) |
static Set<TreePath> |
computeDuplicates(CompilationInfo info,
TreePath searchingFor,
TreePath scope,
AtomicBoolean cancel)
Find duplicates for provided expression
|
static void |
forceSource(CompilationController cc,
FileObject file)
Ensure that the given file is parsed from source, in the context of the
provided parser instance.
|
static List<? extends Completion> |
getAttributeValueCompletions(CompilationInfo info,
Element element,
AnnotationMirror annotation,
ExecutableElement member,
String userText)
Returns a list of completions for an annotation attribute value suggested by
annotation processors.
|
static TypeMirror |
getBound(WildcardType wildcardType) |
static Set<URL> |
getDependentRoots(URL root)
Returns the dependent source path roots for given source root.
|
static Set<URL> |
getDependentRoots(URL root,
boolean filterNonOpenedProjects)
Returns the dependent source path roots for given source root.
|
static Object |
getDiagnosticParam(Diagnostic<?> d,
int index)
Extracts diagnostic params from a diagnostic.
|
static TypeElement |
getEnclosingTypeElement(Element element)
|
static FileObject |
getFile(Element element,
ClasspathInfo cpInfo)
Deprecated.
|
static FileObject |
getFile(ElementHandle<? extends Element> handle,
ClasspathInfo cpInfo)
Returns a
FileObject of the source file in which the handle is declared. |
static Collection<? extends Element> |
getForwardReferences(TreePath path,
int pos,
SourcePositions sourcePositions,
Trees trees)
Returns all elements of the given scope that are declared after given position in a source.
|
static Collection<? extends URL> |
getJavadoc(Element element)
Finds
URL s of a javadoc page for given element when available. |
static URL |
getJavadoc(Element element,
ClasspathInfo cpInfo)
|
static TokenSequence<org.netbeans.api.java.lexer.JavaTokenId> |
getJavaTokenSequence(TokenHierarchy hierarchy,
int offset) |
static String[] |
getJVMSignature(ElementHandle<?> handle)
Returns an array containing the JVM signature of the
ElementHandle . |
static Collection<ElementHandle<TypeElement>> |
getMainClasses(FileObject fo)
Returns classes declared in the given source file which have the main method.
|
static Collection<ElementHandle<TypeElement>> |
getMainClasses(FileObject[] sourceRoots)
Returns classes declared under the given source roots which have the main method.
|
static String |
getModuleName(URL rootUrl)
Returns the name of the module.
|
static String |
getModuleName(URL rootUrl,
boolean canUseSources)
Returns the name of the module.
|
static Set<String> |
getModuleNames(CompilationInfo info,
Set<? extends ClassIndex.SearchScopeType> scope)
Returns names of all modules within given scope.
|
static TypeElement |
getOutermostEnclosingTypeElement(Element element) |
static URL |
getPreferredJavadoc(Element element)
Returns preferred Javadoc
URL . |
static boolean |
isMainClass(String qualifiedName,
ClasspathInfo cpInfo)
Returns true when the class contains main method.
|
static boolean |
isMainClass(String qualifiedName,
ClasspathInfo cpInfo,
boolean optimistic)
Returns true when the class contains main method.
|
static boolean |
isMainMethod(ExecutableElement method)
Returns true if the method is a main method
|
static boolean |
isScanInProgress()
Tests whether the initial scan is in progress.
|
static String |
parseModuleName(FileObject moduleInfo)
Returns a module name parsed from the given module-info.java.
|
static TypeMirror |
resolveCapturedType(CompilationInfo info,
TypeMirror tm)
Resolves all captured type variables to their respective wildcards in the given type.
|
static WildcardType |
resolveCapturedType(TypeMirror type) |
static String |
resolveImport(CompilationInfo info,
TreePath context,
String fqn)
Resolve full qualified name in the given context.
|
static void |
waitScanFinished()
|
public static TokenSequence<org.netbeans.api.java.lexer.JavaTokenId> getJavaTokenSequence(TokenHierarchy hierarchy, int offset)
public static Set<TreePath> computeDuplicates(CompilationInfo info, TreePath searchingFor, TreePath scope, AtomicBoolean cancel)
info
- CompilationInfosearchingFor
- expression which is being searchedscope
- scope for searchcancel
- option to cancel find duplicatespublic static boolean checkTypesAssignable(CompilationInfo info, TypeMirror from, TypeMirror to)
public static TypeMirror getBound(WildcardType wildcardType)
public static List<? extends Completion> getAttributeValueCompletions(CompilationInfo info, Element element, AnnotationMirror annotation, ExecutableElement member, String userText)
info
- the CompilationInfo used to resolve annotation processorselement
- the element being annotatedannotation
- the (perhaps partial) annotation being applied to the elementmember
- the annotation member to return possible completions foruserText
- source code text to be completed@Deprecated public static TypeElement getEnclosingTypeElement(Element element) throws IllegalArgumentException
IllegalArgumentException
- if the provided element is a package elementpublic static TypeElement getOutermostEnclosingTypeElement(Element element)
@NonNull public static String[] getJVMSignature(@NonNull ElementHandle<?> handle)
ElementHandle
.handle
- to obtain the JVM signature for.ElementHandle
's ElementKind
. For class or package
it returns a single element array containing the class (package) binary
name (JLS section 13.1). For field (method) it returns three element array
containing owner class binary name (JLS section 13.1) in the first element,
field (method) name in the second element and JVM type (JVM method formal
parameters (JVMS section 2.10.1)) in the third element.public static String resolveImport(CompilationInfo info, TreePath context, String fqn) throws NullPointerException, IOException
info
passed to this method is not an instance of WorkingCopy
,
missing import statement is added from a separate modification task executed asynchronously.
info
- CompilationInfo over which the method should workcontext
- in which the fully qualified should be resolvedfqn
- the fully qualified name to resolveNullPointerException
IOException
public static FileObject getFile(Element element, ClasspathInfo cpInfo)
SourceUtils.getFile(ElementHandle, ClasspathInfo)
FileObject
in which the Element is defined.element
- for which the FileObject
should be locatedcpInfo
- the classpaths contextFileObject
or null if it cannot be
foundpublic static FileObject getFile(ElementHandle<? extends Element> handle, ClasspathInfo cpInfo)
FileObject
of the source file in which the handle is declared.handle
- to find the FileObject
forcpInfo
- classpaths for resolving handleFileObject
or null when the source file cannot be found@Deprecated public static URL getJavadoc(Element element, ClasspathInfo cpInfo)
SourceUtils.getJavadoc(javax.lang.model.element.Element)
or SourceUtils.getPreferredJavadoc(javax.lang.model.element.Element)
URL
of a javadoc page for given element when available. This method
uses JavadocForBinaryQuery
to find the javadoc page for the give element.
For PackageElement
it returns the package-summary.html for given package.element
- to find the Javadoc forcpInfo
- classpaths used to resolve (currently unused)@CheckForNull public static URL getPreferredJavadoc(@NonNull Element element)
URL
.
Threading: The method parses the javadoc to find out the used doclet,
so it should not be called from EDT.element
- to find the Javadoc for@NonNull public static Collection<? extends URL> getJavadoc(@NonNull Element element)
URL
s of a javadoc page for given element when available. This method
uses JavadocForBinaryQuery
to find the javadoc page for the give element.
For PackageElement
it returns the package-summary.html for given package.
Due to the https://bugs.openjdk.java.net/browse/JDK-8025633 there are more possible
URLs for ExecutableElement
s, this method returns all of them.element
- to find the Javadoc forpublic static boolean isScanInProgress()
public static void waitScanFinished() throws InterruptedException
JavaSource.runWhenScanFinished(org.netbeans.api.java.source.Task<org.netbeans.api.java.source.CompilationController>, boolean)
InterruptedException
- is thrown when the waiting thread is interrupted.@NonNull public static Set<URL> getDependentRoots(@NonNull URL root)
@NonNull public static Set<URL> getDependentRoots(@NonNull URL root, boolean filterNonOpenedProjects)
public static Collection<ElementHandle<TypeElement>> getMainClasses(@NonNull FileObject fo)
fo
- source fileIllegalArgumentException
- when file does not exist or is not a java source file.public static boolean isMainClass(String qualifiedName, ClasspathInfo cpInfo)
qualifiedName
- the fully qualified name of classcpInfo
- the classpath used to resolve the classpublic static boolean isMainClass(String qualifiedName, ClasspathInfo cpInfo, boolean optimistic)
qualifiedName
- the fully qualified name of classcpInfo
- the classpath used to resolve the classoptimistic
- when true does only index check without parsing the file.
The optimistic check is faster but it works only for source file not for binaries
for which index does not exist. It also does not handle inheritance of the main method.public static boolean isMainMethod(ExecutableElement method)
method
- to be checkedpublic static Collection<ElementHandle<TypeElement>> getMainClasses(FileObject[] sourceRoots)
sourceRoots
- the source rootspublic static TypeMirror resolveCapturedType(CompilationInfo info, TypeMirror tm)
info
- CompilationInfo over which the method should worktm
- type to resolvepublic static WildcardType resolveCapturedType(TypeMirror type)
public static Collection<? extends Element> getForwardReferences(TreePath path, int pos, SourcePositions sourcePositions, Trees trees)
path
- to the given search scopepos
- position in the sourcesourcePositions
- trees
- public static Set<String> getModuleNames(CompilationInfo info, @NonNull Set<? extends ClassIndex.SearchScopeType> scope)
info
- the CompilationInfo used to resolve modulesscope
- to search in SearchScope
@CheckForNull public static String getModuleName(@NonNull URL rootUrl)
rootUrl
- the binary root@CheckForNull public static String getModuleName(@NonNull URL rootUrl, @NonNull boolean canUseSources)
rootUrl
- the binary rootcanUseSources
- @CheckForNull public static String parseModuleName(@NonNull FileObject moduleInfo)
moduleInfo
- the module-info java file to parse the module name frompublic static Object getDiagnosticParam(Diagnostic<?> d, int index)
d
- diagnosticindex
- parameter index to extractpublic static void forceSource(CompilationController cc, FileObject file)
CompilationController.toPhase(org.netbeans.api.java.source.JavaSource.Phase)
.cc
- the parser instance that should be augmentedfile
- the input source file