Class MoreElements
- java.lang.Object
-
- com.google.auto.common.MoreElements
-
@Beta public final class MoreElements extends java.lang.Object
Static utility methods pertaining toElement
instances.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
MoreElements.CastingElementVisitor<T>
private static class
MoreElements.ExecutableElementVisitor
private static class
MoreElements.PackageElementVisitor
private static class
MoreElements.TypeElementVisitor
private static class
MoreElements.TypeParameterElementVisitor
private static class
MoreElements.VariableElementVisitor
-
Constructor Summary
Constructors Modifier Constructor Description private
MoreElements()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static javax.lang.model.element.ExecutableElement
asExecutable(javax.lang.model.element.Element element)
Returns the givenElement
instance asExecutableElement
.static javax.lang.model.element.PackageElement
asPackage(javax.lang.model.element.Element element)
Returns the givenElement
instance asPackageElement
.static javax.lang.model.element.TypeElement
asType(javax.lang.model.element.Element element)
Returns the givenElement
instance asTypeElement
.static javax.lang.model.element.TypeParameterElement
asTypeParameter(javax.lang.model.element.Element element)
Returns the givenElement
instance asTypeParameterElement
.static javax.lang.model.element.VariableElement
asVariable(javax.lang.model.element.Element element)
Returns the givenElement
instance asVariableElement
.private static com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement>
getAllMethods(javax.lang.model.element.TypeElement type, Overrides overrides)
private static void
getAllMethods(javax.lang.model.element.TypeElement type, com.google.common.collect.SetMultimap<java.lang.String,javax.lang.model.element.ExecutableElement> methods)
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 fromtype
, 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 anAnnotationMirror
for the annotation of typeannotationClass
onelement
, orOptional.absent()
if no such annotation exists.private static com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement>
getLocalAndInheritedMethods(javax.lang.model.element.TypeElement type, Overrides overrides)
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 methodgetLocalAndInheritedMethods(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 fromtype
, including methods that it inherits from its ancestors.static javax.lang.model.element.PackageElement
getPackage(javax.lang.model.element.Element element)
An alternate implementation ofElements.getPackageOf(javax.lang.model.element.Element)
that does not require anElements
instance.static <T extends javax.lang.model.element.Element>
com.google.common.base.Predicate<T>hasModifiers(java.util.Set<javax.lang.model.element.Modifier> modifiers)
Returns aPredicate
that can be used to filter elements byModifier
.static <T extends javax.lang.model.element.Element>
com.google.common.base.Predicate<T>hasModifiers(javax.lang.model.element.Modifier... modifiers)
Returns aPredicate
that can be used to filter elements byModifier
.static boolean
isAnnotationPresent(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
Returnstrue
iff the given element has anAnnotationMirror
whose annotation type has the same canonical name as that ofannotationClass
.static boolean
isType(javax.lang.model.element.Element element)
Returns true if the givenElement
instance is aTypeElement
.(package private) static boolean
methodVisibleFromPackage(javax.lang.model.element.ExecutableElement method, javax.lang.model.element.PackageElement pkg)
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.
-
-
-
Method Detail
-
getPackage
public static javax.lang.model.element.PackageElement getPackage(javax.lang.model.element.Element element)
An alternate implementation ofElements.getPackageOf(javax.lang.model.element.Element)
that does not require anElements
instance.- Throws:
java.lang.NullPointerException
- ifelement
isnull
-
asPackage
public static javax.lang.model.element.PackageElement asPackage(javax.lang.model.element.Element element)
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:
java.lang.NullPointerException
- ifelement
isnull
java.lang.IllegalArgumentException
- ifelement
isn't aPackageElement
.
-
isType
public static boolean isType(javax.lang.model.element.Element element)
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:
java.lang.NullPointerException
- ifelement
isnull
-
asType
public static javax.lang.model.element.TypeElement asType(javax.lang.model.element.Element element)
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:
java.lang.NullPointerException
- ifelement
isnull
java.lang.IllegalArgumentException
- ifelement
isn't aTypeElement
.
-
asTypeParameter
public static javax.lang.model.element.TypeParameterElement asTypeParameter(javax.lang.model.element.Element element)
Returns the givenElement
instance asTypeParameterElement
.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:
java.lang.NullPointerException
- ifelement
isnull
java.lang.IllegalArgumentException
- ifelement
isn't aTypeParameterElement
.
-
asVariable
public static javax.lang.model.element.VariableElement asVariable(javax.lang.model.element.Element element)
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:
java.lang.NullPointerException
- ifelement
isnull
java.lang.IllegalArgumentException
- ifelement
isn't aVariableElement
.
-
asExecutable
public static javax.lang.model.element.ExecutableElement asExecutable(javax.lang.model.element.Element element)
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:
java.lang.NullPointerException
- ifelement
isnull
java.lang.IllegalArgumentException
- ifelement
isn't aExecutableElement
.
-
isAnnotationPresent
public static boolean isAnnotationPresent(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.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 annotation proxies.
-
getAnnotationMirror
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)
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 javax.lang.model.element.Element> com.google.common.base.Predicate<T> hasModifiers(javax.lang.model.element.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 javax.lang.model.element.Element> com.google.common.base.Predicate<T> hasModifiers(java.util.Set<javax.lang.model.element.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<javax.lang.model.element.ExecutableElement> getLocalAndInheritedMethods(javax.lang.model.element.TypeElement type, javax.lang.model.util.Elements elementUtils)
Deprecated.The methodgetLocalAndInheritedMethods(TypeElement, Types, Elements)
has better consistency between Java compilers.Returns the set of all non-private, non-static 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.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
orextends
.- 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<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 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.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
orextends
.- 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<javax.lang.model.element.ExecutableElement> getLocalAndInheritedMethods(javax.lang.model.element.TypeElement type, Overrides overrides)
-
overrides
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)
Tests whether one method, as a member of a given type, overrides another method.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).- Parameters:
overrider
- the first method, possible overrideroverridden
- the second method, possibly being overriddentype
- the type of which the first method is a member- Returns:
true
if and only if the first method overrides the second
-
getAllMethods
public 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 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.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
orextends
.- 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()
-
getAllMethods
private static com.google.common.collect.ImmutableSet<javax.lang.model.element.ExecutableElement> getAllMethods(javax.lang.model.element.TypeElement type, Overrides overrides)
-
getAllMethods
private static void getAllMethods(javax.lang.model.element.TypeElement type, com.google.common.collect.SetMultimap<java.lang.String,javax.lang.model.element.ExecutableElement> methods)
-
methodVisibleFromPackage
static boolean methodVisibleFromPackage(javax.lang.model.element.ExecutableElement method, javax.lang.model.element.PackageElement pkg)
-
-