public final class ElementUtilities extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
ElementUtilities.ElementAcceptor
Filter
Element s |
Modifier and Type | Method and Description |
---|---|
boolean |
alreadyDefinedIn(CharSequence name,
ExecutableType method,
TypeElement enclClass)
Returns true if a method specified by name and type is defined in a
class type.
|
TypeElement |
enclosingTypeElement(Element element)
Returns the type element within which this member or constructor
is declared.
|
Element |
findElement(String description)
Looks up the given Java element.
|
Map<? extends Element,Scope> |
findElementsAndOrigins(Scope scope,
ElementUtilities.ElementAcceptor acceptor)
Finds symbols which satisfy the acceptor visible in the passed scope.
|
List<? extends ExecutableElement> |
findOverridableMethods(TypeElement type)
Find all methods in given type and its supertypes, which are overridable.
|
List<? extends ExecutableElement> |
findUnimplementedMethods(TypeElement impl)
Find all methods in given type and its supertypes, which are not implemented.
|
List<? extends ExecutableElement> |
findUnimplementedMethods(TypeElement impl,
boolean includeDefaults)
Finds all unimplemented methods in the given type and supertypes, but possibly include
also interface default methods.
|
static String |
getBinaryName(TypeElement element)
Returns a binary name of a type.
|
ExecutableElement |
getDescriptorElement(TypeElement origin)
Find the element of the method descriptor associated to the functional interface.
|
CharSequence |
getElementName(Element el,
boolean fqn)
Returns name of the given element.
|
Iterable<? extends TypeElement> |
getGlobalTypes(ElementUtilities.ElementAcceptor acceptor)
Return
TypeElement s:
which are imported
which are in the same package as the current file
which are in the java.lang package
|
Element |
getImplementationOf(ExecutableElement method,
TypeElement origin)
Returns the implementation of a method in class origin; null if none exists.
|
Iterable<? extends Element> |
getLocalMembersAndVars(Scope scope,
ElementUtilities.ElementAcceptor acceptor)
Return members declared in the given scope.
|
Iterable<? extends Element> |
getLocalVars(Scope scope,
ElementUtilities.ElementAcceptor acceptor)
Return variables declared in the given scope.
|
Iterable<? extends Element> |
getMembers(TypeMirror type,
ElementUtilities.ElementAcceptor acceptor)
Returns all members of a type, whether inherited or
declared directly.
|
ExecutableElement |
getOverriddenMethod(ExecutableElement method)
Returns the parent method which the specified method overrides, or null
if the method does not override a parent class method.
|
boolean |
hasGetter(TypeElement type,
VariableElement field,
CodeStyle codeStyle)
Check whether given type has a getter method for the given field.
|
boolean |
hasSetter(TypeElement type,
VariableElement field,
CodeStyle codeStyle)
Check whether given type has a setter method for the given field.
|
boolean |
implementsMethod(ExecutableElement element)
Returns true if this element represents a method which
implements a method in an interface the parent class implements.
|
boolean |
isEffectivelyFinal(VariableElement e)
Check whether the given variable is effectively final or final.
|
boolean |
isErroneous(Element e)
Checks whether 'e' contains error or is missing.
|
boolean |
isLocal(Element element)
Returns true if the element is declared (directly or indirectly) local
to a method or variable initializer.
|
boolean |
isMemberOf(Element e,
TypeElement type)
Returns true if a type element has the specified element as a member.
|
boolean |
isOpen(ModuleElement element)
Returns true if the given module is open.
|
boolean |
isSynthetic(Element element)
Returns true if the given element is synthetic.
|
TypeElement |
outermostTypeElement(Element element)
The outermost TypeElement which indirectly encloses this element.
|
boolean |
overridesMethod(ExecutableElement element)
Returns true if this element represents a method which overrides a
method in one of its superclasses.
|
public TypeElement enclosingTypeElement(Element element) throws IllegalArgumentException
IllegalArgumentException
- if the provided element is a package elementpublic TypeElement outermostTypeElement(Element element)
public Element getImplementationOf(ExecutableElement method, TypeElement origin)
public boolean isSynthetic(Element element)
element
- to checkpublic boolean isOpen(ModuleElement element)
element
- to checkpublic boolean overridesMethod(ExecutableElement element)
public static String getBinaryName(TypeElement element) throws IllegalArgumentException
element
- for which the binary name should be returnedIllegalArgumentException
- when the element is not a javac elementpublic Iterable<? extends Element> getMembers(TypeMirror type, ElementUtilities.ElementAcceptor acceptor)
type
- the type being examinedacceptor
- to filter the membersElements.getAllMembers(javax.lang.model.element.TypeElement)
@NonNull public Map<? extends Element,Scope> findElementsAndOrigins(@NonNull Scope scope, ElementUtilities.ElementAcceptor acceptor)
Element.getEnclosingElement()
and
the mapped Scope whether the symbol is directly declared, or inherited. Non-member symbols (variables, parameters, try resources, ...)
map to Scope of their defining Method.
If an Element from outer Scope is hidden by a similar Element in inner scope, only the Element visible to the passed Scope is returned. For example, if both the starting (inner) class and its outer class define method m(), only InnerClass.m() will be returned.
Note that Scope.getEnclosingMethod()
returns non-null even for class scopes of local or anonymous classes; check both Scope.getEnclosingClass()
and Scope.getEnclosingMethod()
and their relationship to get the appropriate Element associated with the Scope.
public Iterable<? extends Element> getLocalMembersAndVars(Scope scope, ElementUtilities.ElementAcceptor acceptor)
public Iterable<? extends Element> getLocalVars(Scope scope, ElementUtilities.ElementAcceptor acceptor)
public Iterable<? extends TypeElement> getGlobalTypes(ElementUtilities.ElementAcceptor acceptor)
TypeElement
s:
public CharSequence getElementName(Element el, boolean fqn)
el
- elementfqn
- true if fully qualified name should be returnedpublic boolean isLocal(Element element)
public boolean alreadyDefinedIn(CharSequence name, ExecutableType method, TypeElement enclClass)
public boolean isMemberOf(Element e, TypeElement type)
public ExecutableElement getOverriddenMethod(ExecutableElement method)
public boolean implementsMethod(ExecutableElement element)
public List<? extends ExecutableElement> findUnimplementedMethods(TypeElement impl)
impl
- to inspectpublic List<? extends ExecutableElement> findUnimplementedMethods(TypeElement impl, boolean includeDefaults)
If the platform configured for the type is older than JDK8, the method is equivalent
to ElementUtilities.findUnimplementedMethods(javax.lang.model.element.TypeElement)
. If `includeDefaults'
is true
, returns also default methods as if the methods were required to be
reimplemented by the final class.
impl
- the implementation typeincludeDefaults
- if true, will also return interface default methods, which
are not overriden in supertypespublic List<? extends ExecutableElement> findOverridableMethods(TypeElement type)
type
- to inspectpublic boolean hasGetter(TypeElement type, VariableElement field, CodeStyle codeStyle)
type
- to inspectfield
- to search getter forcodeStyle
- public boolean hasSetter(TypeElement type, VariableElement field, CodeStyle codeStyle)
type
- to inspectfield
- to search setter forcodeStyle
- public boolean isErroneous(@NullAllowed Element e)
e
- Element to check or null
null
) or contains errors.public boolean isEffectivelyFinal(VariableElement e)
e
- variable to check for effectively final status@CheckForNull public Element findElement(@NonNull String description)
elementDescription
format is as follows:
.
field name.
method name(
comma separated parameter types)
.
simple name of enclosing type(
comma separated parameter types)
description
- the description of the element that should be checked for existencepublic ExecutableElement getDescriptorElement(TypeElement origin)
origin
- functional interface elementnull
if the origin
is not a functional interface.