@Beta
public final class MoreElements
extends java.lang.Object
Element instances.| Modifier and Type | Method and Description |
|---|---|
static javax.lang.model.element.ExecutableElement |
asExecutable(javax.lang.model.element.Element element)
Returns the given
Element instance as ExecutableElement. |
static javax.lang.model.element.PackageElement |
asPackage(javax.lang.model.element.Element element)
Returns the given
Element instance as PackageElement. |
static javax.lang.model.element.TypeElement |
asType(javax.lang.model.element.Element element)
Returns the given
Element instance as TypeElement. |
static javax.lang.model.element.TypeParameterElement |
asTypeParameter(javax.lang.model.element.Element element)
Returns the given
Element instance as TypeParameterElement. |
static javax.lang.model.element.VariableElement |
asVariable(javax.lang.model.element.Element element)
Returns the given
Element instance as VariableElement. |
static com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> |
getAllMethods(javax.lang.model.element.TypeElement type,
javax.lang.model.util.Types typeUtils,
javax.lang.model.util.Elements elementUtils)
Returns the set of all methods from
type, including methods that it inherits
from its ancestors. |
static com.google.common.base.Optional<javax.lang.model.element.AnnotationMirror> |
getAnnotationMirror(javax.lang.model.element.Element element,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Returns an
AnnotationMirror for the annotation of type annotationClass on
element, or Optional.absent() if no such annotation exists. |
static com.google.common.base.Optional<javax.lang.model.element.AnnotationMirror> |
getAnnotationMirror(javax.lang.model.element.Element element,
java.lang.String annotationName)
Returns an
AnnotationMirror for the annotation whose type's canonical name is on element, or Optional.absent() if no such annotation exists. |
static com.google.common.base.Optional<javax.lang.model.element.AnnotationMirror> |
getAnnotationMirror(javax.lang.model.element.Element element,
javax.lang.model.element.TypeElement annotation)
Returns an
AnnotationMirror for the annotation of type annotation on element, or Optional.absent() if no such annotation exists. |
static com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> |
getLocalAndInheritedMethods(javax.lang.model.element.TypeElement type,
javax.lang.model.util.Elements elementUtils)
Deprecated.
The method
getLocalAndInheritedMethods(TypeElement, Types, Elements)
has better consistency between Java compilers. |
static com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> |
getLocalAndInheritedMethods(javax.lang.model.element.TypeElement type,
javax.lang.model.util.Types typeUtils,
javax.lang.model.util.Elements elementUtils)
Returns the set of all non-private, non-static methods from
type, including methods
that it inherits from its ancestors. |
static javax.lang.model.element.PackageElement |
getPackage(javax.lang.model.element.Element element)
An alternate implementation of
Elements.getPackageOf(javax.lang.model.element.Element) that does not require an
Elements instance. |
static <T extends javax.lang.model.element.Element> |
hasModifiers(javax.lang.model.element.Modifier... modifiers)
Returns a
Predicate that can be used to filter elements by Modifier. |
static <T extends javax.lang.model.element.Element> |
hasModifiers(java.util.Set<javax.lang.model.element.Modifier> modifiers)
Returns a
Predicate that can be used to filter elements by Modifier. |
static boolean |
isAnnotationPresent(javax.lang.model.element.Element element,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Returns
true iff the given element has an AnnotationMirror whose annotation type has the same canonical name as that of
annotationClass. |
static boolean |
isAnnotationPresent(javax.lang.model.element.Element element,
java.lang.String annotationName)
Returns
true iff the given element has an AnnotationMirror whose annotation type has annotationName as its
canonical name. |
static boolean |
isAnnotationPresent(javax.lang.model.element.Element element,
javax.lang.model.element.TypeElement annotation)
Returns
true iff the given element has an AnnotationMirror whose annotation type has the same fully qualified name as that
of annotation. |
static boolean |
isType(javax.lang.model.element.Element element)
Returns true if the given
Element instance is a TypeElement. |
static boolean |
overrides(javax.lang.model.element.ExecutableElement overrider,
javax.lang.model.element.ExecutableElement overridden,
javax.lang.model.element.TypeElement type,
javax.lang.model.util.Types typeUtils)
Tests whether one method, as a member of a given type, overrides another method.
|
public static javax.lang.model.element.PackageElement getPackage(javax.lang.model.element.Element element)
Elements.getPackageOf(javax.lang.model.element.Element) that does not require an
Elements instance.java.lang.NullPointerException - if element is nullpublic static javax.lang.model.element.PackageElement asPackage(javax.lang.model.element.Element element)
Element instance as PackageElement.
This method is functionally equivalent to an instanceof check and a cast, but should
always be used over that idiom as instructed in the documentation for Element.
java.lang.NullPointerException - if element is nulljava.lang.IllegalArgumentException - if element isn't a PackageElement.public static boolean isType(javax.lang.model.element.Element element)
Element instance is a TypeElement.
This method is functionally equivalent to an instanceof check, but should
always be used over that idiom as instructed in the documentation for Element.
java.lang.NullPointerException - if element is nullpublic static javax.lang.model.element.TypeElement asType(javax.lang.model.element.Element element)
Element instance as TypeElement.
This method is functionally equivalent to an instanceof check and a cast, but should
always be used over that idiom as instructed in the documentation for Element.
java.lang.NullPointerException - if element is nulljava.lang.IllegalArgumentException - if element isn't a TypeElement.public static javax.lang.model.element.TypeParameterElement asTypeParameter(javax.lang.model.element.Element element)
Element instance as TypeParameterElement.
This method is functionally equivalent to an instanceof check and a cast, but should
always be used over that idiom as instructed in the documentation for Element.
java.lang.NullPointerException - if element is nulljava.lang.IllegalArgumentException - if element isn't a TypeParameterElement.public static javax.lang.model.element.VariableElement asVariable(javax.lang.model.element.Element element)
Element instance as VariableElement.
This method is functionally equivalent to an instanceof check and a cast, but should
always be used over that idiom as instructed in the documentation for Element.
java.lang.NullPointerException - if element is nulljava.lang.IllegalArgumentException - if element isn't a VariableElement.public static javax.lang.model.element.ExecutableElement asExecutable(javax.lang.model.element.Element element)
Element instance as ExecutableElement.
This method is functionally equivalent to an instanceof check and a cast, but should
always be used over that idiom as instructed in the documentation for Element.
java.lang.NullPointerException - if element is nulljava.lang.IllegalArgumentException - if element isn't a ExecutableElement.public static boolean isAnnotationPresent(javax.lang.model.element.Element element,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
true iff the given element has an AnnotationMirror whose annotation type has the same canonical name as that of
annotationClass. This method is a safer alternative to calling Element.getAnnotation(java.lang.Class<A>) and checking for null as it avoids any interaction with
annotation proxies.public static boolean isAnnotationPresent(javax.lang.model.element.Element element,
javax.lang.model.element.TypeElement annotation)
true iff the given element has an AnnotationMirror whose annotation type has the same fully qualified name as that
of annotation. This method is a safer alternative to calling Element.getAnnotation(java.lang.Class<A>) and checking for null as it avoids any interaction with
annotation proxies.public static boolean isAnnotationPresent(javax.lang.model.element.Element element,
java.lang.String annotationName)
true iff the given element has an AnnotationMirror whose annotation type has annotationName as its
canonical name. This method is a safer alternative to calling Element.getAnnotation(java.lang.Class<A>) and
checking for null as it avoids any interaction with annotation proxies.public static com.google.common.base.Optional<javax.lang.model.element.AnnotationMirror> getAnnotationMirror(javax.lang.model.element.Element element,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
AnnotationMirror for the annotation of type annotationClass on
element, or Optional.absent() if no such annotation exists. This method is a
safer alternative to calling Element.getAnnotation(java.lang.Class<A>) as it avoids any interaction with
annotation proxies.public static com.google.common.base.Optional<javax.lang.model.element.AnnotationMirror> getAnnotationMirror(javax.lang.model.element.Element element,
javax.lang.model.element.TypeElement annotation)
AnnotationMirror for the annotation of type annotation on element, or Optional.absent() if no such annotation exists. This method is a safer
alternative to calling Element.getAnnotation(java.lang.Class<A>) as it avoids any interaction with
annotation proxies.public static com.google.common.base.Optional<javax.lang.model.element.AnnotationMirror> getAnnotationMirror(javax.lang.model.element.Element element,
java.lang.String annotationName)
AnnotationMirror for the annotation whose type's canonical name is on element, or Optional.absent() if no such annotation exists. This method is a safer
alternative to calling Element.getAnnotation(java.lang.Class<A>) as it avoids any interaction with
annotation proxies.public static <T extends javax.lang.model.element.Element> com.google.common.base.Predicate<T> hasModifiers(javax.lang.model.element.Modifier... modifiers)
Predicate that can be used to filter elements by Modifier.
The predicate returns true if the input Element has all of the given
modifiers, perhaps in addition to others.
Here is an example how one could get a List of static methods of a class:
FluentIterable.from(ElementFilter.methodsIn(clazzElement.getEnclosedElements()))
.filter(MoreElements.hasModifiers(Modifier.STATIC).toList();
public static <T extends javax.lang.model.element.Element> com.google.common.base.Predicate<T> hasModifiers(java.util.Set<javax.lang.model.element.Modifier> modifiers)
Predicate that can be used to filter elements by Modifier.
The predicate returns true if the input Element has all of the given
modifiers, perhaps in addition to others.
Here is an example how one could get a List of methods with certain modifiers of a class:
Set<Modifier> modifiers = ...;
FluentIterable.from(ElementFilter.methodsIn(clazzElement.getEnclosedElements()))
.filter(MoreElements.hasModifiers(modifiers).toList();
@Deprecated
public static com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> getLocalAndInheritedMethods(javax.lang.model.element.TypeElement type,
javax.lang.model.util.Elements elementUtils)
getLocalAndInheritedMethods(TypeElement, Types, Elements)
has better consistency between Java compilers.type, including methods
that it inherits from its ancestors. Inherited methods that are overridden are not included in
the result. So if type defines public String toString(), the returned set will
contain that method, but not the toString() method defined by Object.
The returned set may contain more than one method with the same signature, if
type inherits those methods from different ancestors. For example, if it
inherits from unrelated interfaces One and Two which each define
void foo();, and if it does not itself override the foo() method,
then both One.foo() and Two.foo() will be in the returned set.
The order of the returned set is deterministic: within a class or interface, methods are in
the order they appear in the source code; methods in ancestors come before methods in
descendants; methods in interfaces come before methods in classes; and in a class or interface
that has more than one superinterface, the interfaces are in the order of their appearance in
implements or extends.
type - the type whose own and inherited methods are to be returnedelementUtils - an Elements object, typically returned by
processingEnv.getElementUtils()public static com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> getLocalAndInheritedMethods(javax.lang.model.element.TypeElement type,
javax.lang.model.util.Types typeUtils,
javax.lang.model.util.Elements elementUtils)
type, including methods
that it inherits from its ancestors. Inherited methods that are overridden are not included in
the result. So if type defines public String toString(), the returned set will
contain that method, but not the toString() method defined by Object.
The returned set may contain more than one method with the same signature, if
type inherits those methods from different ancestors. For example, if it
inherits from unrelated interfaces One and Two which each define
void foo();, and if it does not itself override the foo() method,
then both One.foo() and Two.foo() will be in the returned set.
The order of the returned set is deterministic: within a class or interface, methods are in
the order they appear in the source code; methods in ancestors come before methods in
descendants; methods in interfaces come before methods in classes; and in a class or interface
that has more than one superinterface, the interfaces are in the order of their appearance in
implements or extends.
type - the type whose own and inherited methods are to be returnedtypeUtils - a Types object, typically returned by
processingEnv.getTypeUtils()elementUtils - an Elements object, typically returned by
processingEnv.getElementUtils()public static boolean overrides(javax.lang.model.element.ExecutableElement overrider,
javax.lang.model.element.ExecutableElement overridden,
javax.lang.model.element.TypeElement type,
javax.lang.model.util.Types typeUtils)
This method does the same thing as Elements.overrides(ExecutableElement,
ExecutableElement, TypeElement), but in a way that is more consistent between compilers, in
particular between javac and ecj (the Eclipse compiler).
overrider - the first method, possible overrideroverridden - the second method, possibly being overriddentype - the type of which the first method is a membertrue if and only if the first method overrides the secondpublic static com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> getAllMethods(javax.lang.model.element.TypeElement type,
javax.lang.model.util.Types typeUtils,
javax.lang.model.util.Elements elementUtils)
type, including methods that it inherits
from its ancestors. Inherited methods that are overridden are not included in the
result. So if type defines public String toString(), the returned set
will contain that method, but not the toString() method defined by Object.
The returned set may contain more than one method with the same signature, if
type inherits those methods from different ancestors. For example, if it
inherits from unrelated interfaces One and Two which each define
void foo();, and if it does not itself override the foo() method,
then both One.foo() and Two.foo() will be in the returned set.
The order of the returned set is deterministic: within a class or interface, methods are in
the order they appear in the source code; methods in ancestors come before methods in
descendants; methods in interfaces come before methods in classes; and in a class or interface
that has more than one superinterface, the interfaces are in the order of their appearance in
implements or extends.
type - the type whose own and inherited methods are to be returnedtypeUtils - a Types object, typically returned by
processingEnv.getTypeUtils()elementUtils - an Elements object, typically returned by
processingEnv.getElementUtils()Copyright © 2021 Google LLC. All Rights Reserved.