@Beta
public final class MoreElements
extends java.lang.Object
Element
instances.Modifier and Type | Field and Description |
---|---|
private static javax.lang.model.element.ElementVisitor<javax.lang.model.element.ExecutableElement,java.lang.Void> |
EXECUTABLE_ELEMENT_VISITOR |
private static javax.lang.model.element.ElementVisitor<javax.lang.model.element.PackageElement,java.lang.Void> |
PACKAGE_ELEMENT_VISITOR |
private static javax.lang.model.element.ElementVisitor<javax.lang.model.element.TypeElement,java.lang.Void> |
TYPE_ELEMENT_VISITOR |
private static javax.lang.model.element.ElementVisitor<javax.lang.model.element.VariableElement,java.lang.Void> |
VARIABLE_ELEMENT_VISITOR |
Modifier | Constructor and Description |
---|---|
private |
MoreElements() |
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.VariableElement |
asVariable(javax.lang.model.element.Element element)
Returns the given
Element instance as VariableElement . |
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 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 com.google.common.base.Predicate<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 com.google.common.base.Predicate<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 |
isType(javax.lang.model.element.Element element)
Returns true if the given
Element instance is a TypeElement . |
private static final javax.lang.model.element.ElementVisitor<javax.lang.model.element.PackageElement,java.lang.Void> PACKAGE_ELEMENT_VISITOR
private static final javax.lang.model.element.ElementVisitor<javax.lang.model.element.TypeElement,java.lang.Void> TYPE_ELEMENT_VISITOR
private static final javax.lang.model.element.ElementVisitor<javax.lang.model.element.VariableElement,java.lang.Void> VARIABLE_ELEMENT_VISITOR
private static final javax.lang.model.element.ElementVisitor<javax.lang.model.element.ExecutableElement,java.lang.Void> EXECUTABLE_ELEMENT_VISITOR
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 null
public 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 null
java.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 null
public 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 null
java.lang.IllegalArgumentException
- if element
isn't a TypeElement
.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 null
java.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 null
java.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 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.Predicate<javax.lang.model.element.Element> 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 com.google.common.base.Predicate<javax.lang.model.element.Element> 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();