public class ElementUtils
extends java.lang.Object
Element
s.Modifier and Type | Method and Description |
---|---|
static javax.lang.model.element.TypeElement |
enclosingClass(javax.lang.model.element.Element elem)
Returns the innermost type element enclosing the given element
|
static javax.lang.model.element.PackageElement |
enclosingPackage(javax.lang.model.element.Element elem)
Returns the innermost package element enclosing the given element.
|
static javax.lang.model.element.VariableElement |
findFieldInType(javax.lang.model.element.TypeElement type,
java.lang.String name)
Returns the field of the class
|
static java.util.Set<javax.lang.model.element.VariableElement> |
findFieldsInType(javax.lang.model.element.TypeElement type,
java.util.Collection<java.lang.String> names)
Returns the elements of the fields whose simple names are
names and are declared in
type . |
static java.util.Set<javax.lang.model.element.VariableElement> |
findFieldsInTypeOrSuperType(javax.lang.model.type.TypeMirror type,
java.util.Collection<java.lang.String> names)
Returns non-private field elements, and side-effects
names to remove them. |
static java.util.List<javax.lang.model.element.VariableElement> |
getAllFieldsIn(javax.lang.model.util.Elements elements,
javax.lang.model.element.TypeElement type)
Return all fields declared in the given type or any superclass/interface.
|
static java.util.List<javax.lang.model.element.ExecutableElement> |
getAllMethodsIn(javax.lang.model.util.Elements elements,
javax.lang.model.element.TypeElement type)
Return all methods declared in the given type or any superclass/interface.
|
static java.util.List<javax.lang.model.element.TypeElement> |
getAllTypeElementsIn(javax.lang.model.element.TypeElement type)
Return all nested/inner classes/interfaces declared in the given type.
|
static @Nullable javax.lang.model.element.Name |
getQualifiedClassName(javax.lang.model.element.Element element)
Returns the qualified name of the inner most class enclosing the provided
Element |
static java.util.List<javax.lang.model.element.TypeElement> |
getSuperTypes(javax.lang.model.util.Elements elements,
javax.lang.model.element.TypeElement type)
Determine all type elements for the classes and interfaces referenced (directly or
indirectly) in the extends/implements clauses of the given type element.
|
static javax.lang.model.type.TypeMirror |
getType(javax.lang.model.element.Element element)
Returns the
TypeMirror for usage of Element as a value. |
static java.lang.String |
getVerboseName(javax.lang.model.element.Element elt)
Returns a verbose name that identifies the element.
|
static boolean |
hasReceiver(javax.lang.model.element.Element element)
Does the given element need a receiver for accesses? For example, an access to a local
variable does not require a receiver.
|
static boolean |
isCompileTimeConstant(javax.lang.model.element.Element elt)
Returns true if the element is a constant time reference
|
static boolean |
isEffectivelyFinal(javax.lang.model.element.Element element)
Returns true if the element is a effectively final element.
|
static boolean |
isElementFromByteCode(javax.lang.model.element.Element elt)
Returns true if the element is declared in ByteCode.
|
static boolean |
isError(javax.lang.model.element.Element element) |
static boolean |
isFinal(javax.lang.model.element.Element element)
Returns true if the element is a final element: a final field, final method, or final class
|
static boolean |
isMethod(javax.lang.model.element.ExecutableElement questioned,
javax.lang.model.element.ExecutableElement method,
javax.annotation.processing.ProcessingEnvironment env)
Returns true if the given element is, or overrides, method.
|
static boolean |
isObject(javax.lang.model.element.TypeElement element)
Check if the element is an element for 'java.lang.Object'
|
static boolean |
isStatic(javax.lang.model.element.Element element)
Returns true if the element is a static element: whether it is a static field, static method,
or static class
|
static boolean |
isTypeDeclaration(javax.lang.model.element.Element elt) |
static boolean |
matchesElement(javax.lang.model.element.ExecutableElement method,
java.lang.String methodName,
java.lang.Class<?>... parameters)
Check that a method Element matches a signature.
|
static javax.lang.model.element.PackageElement |
parentPackage(javax.lang.model.util.Elements e,
javax.lang.model.element.PackageElement elem)
Returns the "parent" package element for the given package element.
|
public static javax.lang.model.element.TypeElement enclosingClass(javax.lang.model.element.Element elem)
elem
- the enclosed element of a classpublic static javax.lang.model.element.PackageElement enclosingPackage(javax.lang.model.element.Element elem)
Elements.getPackageOf(Element)
. Returns the element itself if it is a
package.elem
- the enclosed element of a packagepublic static javax.lang.model.element.PackageElement parentPackage(javax.lang.model.util.Elements e, javax.lang.model.element.PackageElement elem)
Note that packages are not enclosed within each other, we have to manually climb the namespaces. Calling "enclosingPackage" on a package element returns the package element itself again.
elem
- the package to start frompublic static boolean isStatic(javax.lang.model.element.Element element)
public static boolean isFinal(javax.lang.model.element.Element element)
public static boolean isEffectivelyFinal(javax.lang.model.element.Element element)
public static javax.lang.model.type.TypeMirror getType(javax.lang.model.element.Element element)
TypeMirror
for usage of Element as a value. It returns the return type of
a method element, the class type of a constructor, or simply the type mirror of the element
itself.public static @Nullable javax.lang.model.element.Name getQualifiedClassName(javax.lang.model.element.Element element)
Element
element
- an element enclosed by a class, or a TypeElement
Name
of the innermost class enclosing the elementpublic static java.lang.String getVerboseName(javax.lang.model.element.Element elt)
public static boolean isObject(javax.lang.model.element.TypeElement element)
element
- the type elementpublic static boolean isCompileTimeConstant(javax.lang.model.element.Element elt)
public static boolean isElementFromByteCode(javax.lang.model.element.Element elt)
public static javax.lang.model.element.VariableElement findFieldInType(javax.lang.model.element.TypeElement type, java.lang.String name)
public static java.util.Set<javax.lang.model.element.VariableElement> findFieldsInType(javax.lang.model.element.TypeElement type, java.util.Collection<java.lang.String> names)
names
and are declared in
type
.
If a field isn't declared in type
, its element isn't included in the returned set.
If none of the fields is declared in type
, the empty set is returned.
type
- where to look for fieldsnames
- simple names of fields that might be declared in type
names
and are declared in
type
public static java.util.Set<javax.lang.model.element.VariableElement> findFieldsInTypeOrSuperType(javax.lang.model.type.TypeMirror type, java.util.Collection<java.lang.String> names)
names
to remove them. For every
field name in names
that is declared in type
or a supertype, add its element
to the returned set and remove it from names
.
When this routine returns, the combination of the return value and names
has the
same cardinality, and represents the same fields, as names
did when the method was
called.
type
- where to look for fieldsnames
- simple names of fields that might be declared in type
or a supertype.
(Names that are found are removed from this list.)VariableElement
s for non-private fields that are declared in type
whose simple names were in names
when the method was called.public static boolean isError(javax.lang.model.element.Element element)
public static boolean hasReceiver(javax.lang.model.element.Element element)
element
- the element to testpublic static java.util.List<javax.lang.model.element.TypeElement> getSuperTypes(javax.lang.model.util.Elements elements, javax.lang.model.element.TypeElement type)
TODO: can we learn from the implementation of com.sun.tools.javac.model.JavacElements.getAllMembers(TypeElement)?
public static java.util.List<javax.lang.model.element.VariableElement> getAllFieldsIn(javax.lang.model.util.Elements elements, javax.lang.model.element.TypeElement type)
public static java.util.List<javax.lang.model.element.ExecutableElement> getAllMethodsIn(javax.lang.model.util.Elements elements, javax.lang.model.element.TypeElement type)
public static java.util.List<javax.lang.model.element.TypeElement> getAllTypeElementsIn(javax.lang.model.element.TypeElement type)
public static boolean isTypeDeclaration(javax.lang.model.element.Element elt)
public static boolean matchesElement(javax.lang.model.element.ExecutableElement method, java.lang.String methodName, java.lang.Class<?>... parameters)
Note: Matching the receiver type must be done elsewhere as the Element receiver type is only populated when annotated.
method
- the method ElementmethodName
- the name of the methodparameters
- the formal parameters' Classespublic static boolean isMethod(javax.lang.model.element.ExecutableElement questioned, javax.lang.model.element.ExecutableElement method, javax.annotation.processing.ProcessingEnvironment env)