public final class ElementHandle<T extends Element> extends Object
Element
which can be kept and later resolved
by another javac. The javac Element
s are valid only in a single
JavaCompiler.CompilationTask
or a single run of a
CancellableTask
. A client needing to
keep a reference to an Element
and use it in another CancellableTask
must serialize it into an ElementHandle
.
Currently not all Element
s can be serialized. See ElementHandle.create(T)
for details.
Typical usage of ElementHandle
is as follows:
final ElementHandle[] elementHandle = new ElementHandle[1]; javaSource.runUserActionTask(new Task<CompilationController>() { public void run(CompilationController compilationController) { compilationController.toPhase(Phase.RESOLVED); CompilationUnitTree cu = compilationController.getTree(); List<? extends Tree> types = getTypeDecls(cu); Tree tree = getInterestingElementTree(types); Element element = compilationController.getElement(tree); elementHandle[0] = ElementHandle.create(element); } }, true); otherJavaSource.runUserActionTask(new Task<CompilationController>() { public void run(CompilationController compilationController) { compilationController.toPhase(Phase.RESOLVED); Element element = elementHandle[0].resolve(compilationController); // .... } }, true);
Modifier and Type | Method and Description |
---|---|
static <T extends Element> |
create(T element)
Factory method for creating
ElementHandle . |
static ElementHandle<ModuleElement> |
createModuleElementHandle(String moduleName)
Creates an
ElementHandle representing a ModuleElement . |
static ElementHandle<PackageElement> |
createPackageElementHandle(String packageName)
Creates an
ElementHandle representing a PackageElement . |
static ElementHandle<TypeElement> |
createTypeElementHandle(ElementKind kind,
String binaryName)
Creates an
ElementHandle representing a TypeElement . |
boolean |
equals(Object other) |
static ElementHandle<? extends TypeElement> |
from(TypeMirrorHandle<? extends DeclaredType> typeMirrorHandle)
|
String |
getBinaryName()
Returns a binary name of the
TypeElement represented by this
ElementHandle . |
ElementKind |
getKind()
Returns the
ElementKind of this element handle,
it is the kind of the Element from which the handle
was created. |
String |
getQualifiedName()
Returns a qualified name of the
TypeElement represented by this
ElementHandle . |
int |
hashCode() |
T |
resolve(CompilationInfo compilationInfo)
Resolves an
Element from the ElementHandle . |
boolean |
signatureEquals(ElementHandle<? extends Element> handle)
Tests if the handle has the same signature as the parameter.
|
boolean |
signatureEquals(T element)
Tests if the handle has this same signature as the parameter.
|
String |
toString() |
@CheckForNull public T resolve(@NonNull CompilationInfo compilationInfo)
Element
from the ElementHandle
.compilationInfo
- representing the JavaCompiler.CompilationTask
in which the Element
should be resolved.Element
or null if the elment does not exist on
the classpath/sourcepath of JavaCompiler.CompilationTask
.public boolean signatureEquals(@NonNull ElementHandle<? extends Element> handle)
JavaCompiler
task, but may be resolved into
the different Element
s in the different JavaCompiler
tasks.handle
- to be checkedElement
s
in the same JavaCompiler
task.@NonNull public String getBinaryName() throws IllegalStateException
TypeElement
represented by this
ElementHandle
. When the ElementHandle
doesn't represent
a TypeElement
it throws a IllegalStateException
IllegalStateException
- when this ElementHandle
isn't created for the TypeElement
.@NonNull public String getQualifiedName() throws IllegalStateException
TypeElement
represented by this
ElementHandle
. When the ElementHandle
doesn't represent
a TypeElement
it throws a IllegalStateException
IllegalStateException
- when this ElementHandle
isn't creatred for the TypeElement
.public boolean signatureEquals(@NonNull T element)
JavaCompiler
task, but may be resolved into
the different Element
in the different JavaCompiler
task.element
- to be checkedElement
in the same JavaCompiler
task.@NonNull public ElementKind getKind()
ElementKind
of this element handle,
it is the kind of the Element
from which the handle
was created.ElementKind
@NonNull public static <T extends Element> ElementHandle<T> create(@NonNull T element) throws IllegalArgumentException
ElementHandle
.element
- for which the ElementHandle
should be created. Permitted
ElementKind
s
are: ElementKind.PACKAGE
, ElementKind.CLASS
,
ElementKind.INTERFACE
, ElementKind.ENUM
, ElementKind.ANNOTATION_TYPE
, ElementKind.METHOD
,
ElementKind.CONSTRUCTOR
, ElementKind.INSTANCE_INIT
, ElementKind.STATIC_INIT
,
ElementKind.FIELD
, and ElementKind.ENUM_CONSTANT
.ElementHandle
IllegalArgumentException
- if the element is of an unsupported ElementKind
@NonNull public static ElementHandle<PackageElement> createPackageElementHandle(@NonNull String packageName)
ElementHandle
representing a PackageElement
.packageName
- the name of the packageElementHandle
@NonNull public static ElementHandle<TypeElement> createTypeElementHandle(@NonNull ElementKind kind, @NonNull String binaryName) throws IllegalArgumentException
ElementHandle
representing a TypeElement
.kind
- the ElementKind
of the TypeElement
,
allowed values are ElementKind.CLASS
, ElementKind.INTERFACE
,
ElementKind.ENUM
and ElementKind.ANNOTATION_TYPE
.binaryName
- the class binary name as specified by JLS ยง13.1ElementHandle
IllegalArgumentException
- if kind is neither class nor interface@NonNull public static ElementHandle<ModuleElement> createModuleElementHandle(@NonNull String moduleName)
ElementHandle
representing a ModuleElement
.moduleName
- the name of the moduleElementHandle
@NonNull public static ElementHandle<? extends TypeElement> from(@NonNull TypeMirrorHandle<? extends DeclaredType> typeMirrorHandle)
typeMirrorHandle
- from which the ElementHandle
should be retrieved. Permitted
TypeKind
is TypeKind.DECLARED
.ElementHandle