Class MoreElements
Element
instances.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
private static final class
private static final class
private static final class
private static final class
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ExecutableElement
asExecutable
(Element element) Returns the givenElement
instance asExecutableElement
.static PackageElement
Returns the givenElement
instance asPackageElement
.static TypeElement
Returns the givenElement
instance asTypeElement
.static VariableElement
asVariable
(Element element) Returns the givenElement
instance asVariableElement
.static com.google.common.base.Optional
<AnnotationMirror> getAnnotationMirror
(Element element, Class<? extends Annotation> annotationClass) Returns anAnnotationMirror
for the annotation of typeannotationClass
onelement
, orOptional.absent()
if no such annotation exists.private static void
getLocalAndInheritedMethods
(PackageElement pkg, TypeElement type, com.google.common.collect.SetMultimap<String, ExecutableElement> methods) private static com.google.common.collect.ImmutableSet
<ExecutableElement> getLocalAndInheritedMethods
(TypeElement type, Overrides overrides) static com.google.common.collect.ImmutableSet
<ExecutableElement> getLocalAndInheritedMethods
(TypeElement type, Elements elementUtils) Deprecated.static com.google.common.collect.ImmutableSet
<ExecutableElement> getLocalAndInheritedMethods
(TypeElement type, Types typeUtils, Elements elementUtils) Returns the set of all non-private methods fromtype
, including methods that it inherits from its ancestors.static PackageElement
getPackage
(Element element) An alternate implementation ofElements.getPackageOf(javax.lang.model.element.Element)
that does not require anElements
instance.static <T extends Element>
com.google.common.base.Predicate<T> hasModifiers
(Set<Modifier> modifiers) Returns aPredicate
that can be used to filter elements byModifier
.static <T extends Element>
com.google.common.base.Predicate<T> hasModifiers
(Modifier... modifiers) Returns aPredicate
that can be used to filter elements byModifier
.static boolean
isAnnotationPresent
(Element element, Class<? extends Annotation> annotationClass) Returnstrue
iff the given element has anAnnotationMirror
whose annotation type has the same canonical name as that ofannotationClass
.static boolean
Returns true if the givenElement
instance is aTypeElement
.(package private) static boolean
methodVisibleFromPackage
(ExecutableElement method, PackageElement pkg)
-
Constructor Details
-
MoreElements
private MoreElements()
-
-
Method Details
-
getPackage
An alternate implementation ofElements.getPackageOf(javax.lang.model.element.Element)
that does not require anElements
instance.- Throws:
NullPointerException
- ifelement
isnull
-
asPackage
Returns the givenElement
instance asPackageElement
.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 forElement
.- Throws:
NullPointerException
- ifelement
isnull
IllegalArgumentException
- ifelement
isn't aPackageElement
.
-
isType
Returns true if the givenElement
instance is aTypeElement
.This method is functionally equivalent to an
instanceof
check, but should always be used over that idiom as instructed in the documentation forElement
.- Throws:
NullPointerException
- ifelement
isnull
-
asType
Returns the givenElement
instance asTypeElement
.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 forElement
.- Throws:
NullPointerException
- ifelement
isnull
IllegalArgumentException
- ifelement
isn't aTypeElement
.
-
asVariable
Returns the givenElement
instance asVariableElement
.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 forElement
.- Throws:
NullPointerException
- ifelement
isnull
IllegalArgumentException
- ifelement
isn't aVariableElement
.
-
asExecutable
Returns the givenElement
instance asExecutableElement
.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 forElement
.- Throws:
NullPointerException
- ifelement
isnull
IllegalArgumentException
- ifelement
isn't aExecutableElement
.
-
isAnnotationPresent
public static boolean isAnnotationPresent(Element element, Class<? extends Annotation> annotationClass) Returnstrue
iff the given element has anAnnotationMirror
whose annotation type has the same canonical name as that ofannotationClass
. This method is a safer alternative to callingElement.getAnnotation(java.lang.Class<A>)
and checking fornull
as it avoids any interaction with . This() { super(); } . This() { super(); } annotation proxies. -
getAnnotationMirror
public static com.google.common.base.Optional<AnnotationMirror> getAnnotationMirror(Element element, Class<? extends Annotation> annotationClass) Returns anAnnotationMirror
for the annotation of typeannotationClass
onelement
, orOptional.absent()
if no such annotation exists. This method is a safer alternative to callingElement.getAnnotation(java.lang.Class<A>)
as it avoids any interaction with annotation proxies. -
hasModifiers
public static <T extends Element> com.google.common.base.Predicate<T> hasModifiers(Modifier... modifiers) Returns aPredicate
that can be used to filter elements byModifier
. The predicate returnstrue
if the inputElement
has all of the givenmodifiers
, 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();
-
hasModifiers
public static <T extends Element> com.google.common.base.Predicate<T> hasModifiers(Set<Modifier> modifiers) Returns aPredicate
that can be used to filter elements byModifier
. The predicate returnstrue
if the inputElement
has all of the givenmodifiers
, 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();
-
getLocalAndInheritedMethods
@Deprecated public static com.google.common.collect.ImmutableSet<ExecutableElement> getLocalAndInheritedMethods(TypeElement type, Elements elementUtils) Deprecated.The methodgetLocalAndInheritedMethods(TypeElement, Types, Elements)
has better consistency between Java compilers.Returns the set of all non-private methods fromtype
, including methods that it inherits from its ancestors. Inherited methods that are overridden are not included in the result. So iftype
definespublic String toString()
, the returned set will contain that method, but not thetoString()
method defined byObject
.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 interfacesOne
andTwo
which each definevoid foo();
, and if it does not itself override thefoo()
method, then bothOne.foo()
andTwo.foo()
will be in the returned set.- Parameters:
type
- the type whose own and inherited methods are to be returnedelementUtils
- anElements
object, typically returned byprocessingEnv
.getElementUtils()
-
getLocalAndInheritedMethods
public static com.google.common.collect.ImmutableSet<ExecutableElement> getLocalAndInheritedMethods(TypeElement type, Types typeUtils, Elements elementUtils) Returns the set of all non-private methods fromtype
, including methods that it inherits from its ancestors. Inherited methods that are overridden are not included in the result. So iftype
definespublic String toString()
, the returned set will contain that method, but not thetoString()
method defined byObject
.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 interfacesOne
andTwo
which each definevoid foo();
, and if it does not itself override thefoo()
method, then bothOne.foo()
andTwo.foo()
will be in the returned set.- Parameters:
type
- the type whose own and inherited methods are to be returnedtypeUtils
- aTypes
object, typically returned byprocessingEnv
.getTypeUtils()
elementUtils
- anElements
object, typically returned byprocessingEnv
.getElementUtils()
-
getLocalAndInheritedMethods
private static com.google.common.collect.ImmutableSet<ExecutableElement> getLocalAndInheritedMethods(TypeElement type, Overrides overrides) -
getLocalAndInheritedMethods
private static void getLocalAndInheritedMethods(PackageElement pkg, TypeElement type, com.google.common.collect.SetMultimap<String, ExecutableElement> methods) -
methodVisibleFromPackage
-
getLocalAndInheritedMethods(TypeElement, Types, Elements)
has better consistency between Java compilers.