Modifier and Type | Method and Description |
---|---|
static Set<ElementKind> |
classElementKinds()
Deprecated.
|
static @Nullable TypeElement |
enclosingClass(Element elem)
Deprecated.
|
static PackageElement |
enclosingPackage(Element elem)
Returns the innermost package element enclosing the given element.
|
static @Nullable TypeElement |
enclosingTypeElement(Element elem)
Returns the innermost type element that is, or encloses, the given element.
|
static @Nullable VariableElement |
findFieldInType(TypeElement type,
String name)
Returns the field of the class or
null if not found. |
static Set<VariableElement> |
findFieldsInType(TypeElement type,
Collection<String> names)
Returns the elements of the fields whose simple names are
names and are declared in
type . |
static Set<VariableElement> |
findFieldsInTypeOrSuperType(TypeMirror type,
Collection<String> names)
Returns non-private field elements, and side-effects
names to remove them. |
static List<VariableElement> |
getAllFieldsIn(TypeElement type,
Elements elements)
Return all fields declared in the given type or any superclass/interface.
|
static List<ExecutableElement> |
getAllMethodsIn(TypeElement type,
Elements elements)
Return all methods declared in the given type or any superclass/interface.
|
static List<TypeElement> |
getAllSupertypes(TypeElement type,
ProcessingEnvironment env)
Get all the supertypes of a given type, including the type itself.
|
static List<TypeElement> |
getAllTypeElementsIn(TypeElement type)
Return all nested/inner classes/interfaces declared in the given type.
|
static @BinaryName String |
getBinaryName(TypeElement te)
Returns the binary name of the given type.
|
static List<TypeElement> |
getDirectSuperTypeElements(TypeElement type,
Elements elements)
Determine all type elements for the direct supertypes of the given type element.
|
static @BinaryName String |
getEnclosingClassName(ExecutableElement executableElement)
Returns the binary name of the class enclosing
executableElement . |
static @BinaryName String |
getEnclosingClassName(VariableElement variableElement)
Returns the binary name of the class enclosing
variableElement . |
static List<VariableElement> |
getEnumConstants(TypeElement type)
Returns all enum constants declared in the given enumeration.
|
static ElementKind |
getKindRecordAsClass(Element elt)
Calls getKind() on the given Element, but returns CLASS if the ElementKind is RECORD.
|
static Set<? extends ExecutableElement> |
getOverriddenMethods(ExecutableElement m,
Types types)
Returns the methods that are overridden or implemented by a given method.
|
static @Nullable Name |
getQualifiedClassName(Element element)
Returns the qualified name of the innermost class enclosing the provided
Element . |
static String |
getQualifiedName(Element elt)
Returns a verbose name that identifies the element.
|
static List<? extends Element> |
getRecordComponents(TypeElement element)
Calls getRecordComponents on the given TypeElement.
|
static CharSequence |
getSimpleNameOrDescription(ExecutableElement element)
Returns a user-friendly name for the given method.
|
static String |
getSimpleSignature(ExecutableElement element)
Returns the canonical representation of the method declaration, which contains simple names of
the types only.
|
static String |
getSourceFilePath(TypeElement element)
Returns the path to the source file containing
element , which must be from source code. |
static @Nullable TypeElement |
getSuperClass(TypeElement typeElt)
Returns a type's superclass, or null if it does not have a superclass (it is object or an
interface, or the superclass is not on the classpath).
|
static List<TypeElement> |
getSuperTypes(TypeElement type,
Elements elements)
Determine all type elements for the supertypes of the given type element.
|
static TypeMirror |
getType(Element element)
Returns the
TypeMirror for usage of Element as a value. |
static TypeElement |
getTypeElement(ProcessingEnvironment processingEnv,
Class<?> clazz)
Returns the TypeElement for the given class.
|
static boolean |
hasAnnotation(Element element,
String annotName)
Given an annotation name, return true if the element has the annotation of that name.
|
static boolean |
hasReceiver(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 |
inSameClass(Element e1,
Element e2)
Returns true if the two elements are in the same class.
|
static boolean |
isBindingVariable(Element element)
Return true if the element is a binding variable.
|
static boolean |
isClassElement(Element element)
Deprecated.
|
static boolean |
isCompactCanonicalRecordConstructor(Element elt)
Check if the given element is a compact canonical record constructor.
|
static boolean |
isCompileTimeConstant(@Nullable Element elt)
Returns true if the element is a reference to a compile-time constant.
|
static boolean |
isEffectivelyFinal(Element element)
Returns true if the element is a effectively final element.
|
static boolean |
isElementFromByteCode(@Nullable Element elt)
Returns true if the element is declared in ByteCode.
|
static boolean |
isElementFromSourceCode(@Nullable Element element)
Checks whether a given element came from a source file.
|
static boolean |
isError(Element element)
Returns true if
element is "com.sun.tools.javac.comp.Resolve$SymbolNotFoundError". |
static boolean |
isFinal(Element element)
Returns true if the element is a final element: a final field, final method, or final class.
|
static boolean |
isLocalVariable(Element elt)
Return true if the element is a local variable.
|
static boolean |
isMethod(ExecutableElement questioned,
ExecutableElement method,
ProcessingEnvironment env)
Returns true if the given element is, or overrides, method.
|
static boolean |
isObject(TypeElement element)
Check if the element is an element for 'java.lang.Object'
|
static boolean |
isRecordAccessor(ExecutableElement methodElement)
Returns true if the element is a record accessor method.
|
static boolean |
isStatic(Element element)
Returns true if the element is a static element: whether it is a static field, static method,
or static class.
|
static boolean |
isString(TypeElement element)
Check if the element is an element for 'java.lang.String'
|
static boolean |
isTypeDeclaration(Element elt)
Return true if the element is a type declaration.
|
static boolean |
isTypeElement(Element element)
Is the given element kind a type, i.e., a class, enum, interface, or annotation type.
|
static boolean |
matchesElement(ExecutableElement method,
String methodName,
Class<?>... parameters)
Check that a method Element matches a signature.
|
static @Nullable PackageElement |
parentPackage(PackageElement elem,
Elements elements)
Returns the "parent" package element for the given package element.
|
static @Nullable TypeElement |
strictEnclosingTypeElement(Element elem)
Returns the innermost type element enclosing the given element, that is different from the
element itself.
|
static TypeElement |
toplevelEnclosingTypeElement(Element element)
Returns the top-level type element that contains
element . |
static Set<ElementKind> |
typeElementKinds()
Return the set of kinds that represent classes.
|
@Deprecated public static @Nullable TypeElement enclosingClass(Element elem)
enclosingTypeElement(javax.lang.model.element.Element)
elem
- the enclosed element of a classelem
public static @Nullable TypeElement enclosingTypeElement(Element elem)
Note that in this code:
class Outer {
static class Inner { }
}
Inner
has no enclosing type, but this method returns Outer
.elem
- the enclosed element of a classelem
is
not, and is not enclosed by, a type elementpublic static @Nullable TypeElement strictEnclosingTypeElement(Element elem)
enclosingTypeElement(javax.lang.model.element.Element)
returns its argument if the argument
is a type element.elem
- the enclosed element of a classelem
public static TypeElement toplevelEnclosingTypeElement(Element element)
element
.element
- the element whose enclosing tye element to findelement
that isn't contained in another classpublic static @BinaryName String getEnclosingClassName(ExecutableElement executableElement)
executableElement
.executableElement
- the ExecutableElementexecutableElement
public static @BinaryName String getEnclosingClassName(VariableElement variableElement)
variableElement
.variableElement
- the VariableElementvariableElement
public static PackageElement enclosingPackage(Element elem)
Elements.getPackageOf(Element)
. Returns the element itself if it is a
package.elem
- the enclosed element of a packagepublic static @Nullable PackageElement parentPackage(PackageElement elem, Elements elements)
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 fromelements
- the elementnull
public static boolean isStatic(Element element)
public static boolean isFinal(Element element)
public static boolean isEffectivelyFinal(Element element)
public static TypeMirror getType(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.element
- the element whose type to obtainpublic static @Nullable Name getQualifiedClassName(Element element)
Element
.element
- an element enclosed by a class, or a TypeElement
Name
of the innermost class enclosing the elementpublic static String getQualifiedName(Element elt)
elt
- the element whose name to obtainpublic static @BinaryName String getBinaryName(TypeElement te)
te
- a typepublic static String getSimpleSignature(ExecutableElement element)
element
- a method declarationpublic static CharSequence getSimpleNameOrDescription(ExecutableElement element)
"<init>"
or "<clinit>"
as ExecutableElement.getSimpleName() does.element
- a method declarationpublic static boolean isObject(TypeElement element)
element
- the type elementpublic static boolean isString(TypeElement element)
element
- the type elementpublic static boolean isCompileTimeConstant(@Nullable Element elt)
elt
- an elementpublic static boolean isElementFromSourceCode(@Nullable Element element)
By contrast, isElementFromByteCode(Element)
returns true if there is a
classfile for the given element, even if there is also a source file.
element
- the element to check, or nullpublic static boolean isElementFromByteCode(@Nullable Element elt)
elt
- some elementpublic static String getSourceFilePath(TypeElement element)
element
, which must be from source code.element
- the type element to look atelement
public static @Nullable VariableElement findFieldInType(TypeElement type, String name)
null
if not found.type
- TypeElement to searchname
- name of a fieldpublic static Set<VariableElement> findFieldsInType(TypeElement type, Collection<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 Set<VariableElement> findFieldsInTypeOrSuperType(TypeMirror type, Collection<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(Element element)
element
is "com.sun.tools.javac.comp.Resolve$SymbolNotFoundError".element
- the element to testelement
is "com.sun.tools.javac.comp.Resolve$SymbolNotFoundError"public static boolean hasReceiver(Element element)
element
- the element to testpublic static @Nullable TypeElement getSuperClass(TypeElement typeElt)
typeElt
- a type elementtypeElt
public static List<TypeElement> getSuperTypes(TypeElement type, Elements elements)
TODO: can we learn from the implementation of com.sun.tools.javac.model.JavacElements.getAllMembers(TypeElement)?
type
- the type whose supertypes to returnelements
- the Element utilitiestype
public static List<TypeElement> getDirectSuperTypeElements(TypeElement type, Elements elements)
type
- the type whose supertypes to returnelements
- the Element utilitiestype
public static List<VariableElement> getAllFieldsIn(TypeElement type, Elements elements)
TODO: should this use javax.lang.model.util.Elements.getAllMembers(TypeElement) instead of our own getSuperTypes?
type
- the type whose fields to returnelements
- the Element utilitiestype
public static List<VariableElement> getEnumConstants(TypeElement type)
type
- an Enum typepublic static List<ExecutableElement> getAllMethodsIn(TypeElement type, Elements elements)
TODO: should this use javax.lang.model.util.Elements.getAllMembers(TypeElement) instead of our own getSuperTypes?
type
- the type whose methods to returnelements
- the Element utilitiestype
public static List<TypeElement> getAllTypeElementsIn(TypeElement type)
type
- a typetype
@Deprecated public static Set<ElementKind> classElementKinds()
typeElementKinds()
public static Set<ElementKind> typeElementKinds()
@Deprecated public static boolean isClassElement(Element element)
isTypeElement(javax.lang.model.element.Element)
element
- the element to testpublic static boolean isTypeElement(Element element)
element
- the element to testpublic static boolean isTypeDeclaration(Element elt)
elt
- the element to testpublic static boolean isLocalVariable(Element elt)
elt
- the element to testpublic static boolean isBindingVariable(Element element)
This implementation compiles under JDK 8 and 11 as well as versions that contain ElementKind.BINDING_VARIABLE
.
element
- the element to testpublic static boolean isRecordAccessor(ExecutableElement methodElement)
methodElement
- a method elementpublic static boolean matchesElement(ExecutableElement method, String methodName, Class<?>... parameters)
Note: Matching the receiver type must be done elsewhere as the Element receiver type is only populated when annotated.
method
- the method Element to be testedmethodName
- the goal method nameparameters
- the goal formal parameter Classespublic static boolean isMethod(ExecutableElement questioned, ExecutableElement method, ProcessingEnvironment env)
public static boolean hasAnnotation(Element element, String annotName)
It is more efficient to use Element#getAnnotation(Class)
, but note that both methods
ignore types from annotation files, such as stub or ajava files.
To include types from annotation files, use AnnotatedTypeFactory#fromElement
or
AnnotatedTypeFactory#getDeclAnnotations
.
element
- the elementannotName
- name of the annotationpublic static TypeElement getTypeElement(ProcessingEnvironment processingEnv, Class<?> clazz)
processingEnv
- the processing environmentclazz
- a classpublic static List<TypeElement> getAllSupertypes(TypeElement type, ProcessingEnvironment env)
type
- a typeenv
- the processing environmentpublic static Set<? extends ExecutableElement> getOverriddenMethods(ExecutableElement m, Types types)
m
- a methodtypes
- the type utilitiesm
overrides or implementspublic static boolean inSameClass(Element e1, Element e2)
e1
- an elemente2
- an elementpublic static ElementKind getKindRecordAsClass(Element elt)
elt
- the element to get the kind forpublic static List<? extends Element> getRecordComponents(TypeElement element)
element
- the type element to call getRecordComponents onpublic static boolean isCompactCanonicalRecordConstructor(Element elt)
elt
- the element to check