Class MoreElements
Element instances.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classprivate static final classprivate static final classprivate static final classprivate static final classprivate static final class -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ExecutableElementasExecutable(Element element) Returns the givenElementinstance asExecutableElement.static PackageElementReturns the givenElementinstance asPackageElement.static TypeElementReturns the givenElementinstance asTypeElement.static TypeParameterElementasTypeParameter(Element element) Returns the givenElementinstance asTypeParameterElement.static VariableElementasVariable(Element element) Returns the givenElementinstance asVariableElement.private static com.google.common.collect.ImmutableSet<ExecutableElement> getAllMethods(TypeElement type, Overrides overrides) private static voidgetAllMethods(TypeElement type, com.google.common.collect.SetMultimap<String, ExecutableElement> methods) static com.google.common.collect.ImmutableSet<ExecutableElement> getAllMethods(TypeElement type, Types typeUtils, Elements elementUtils) Returns the set of all methods fromtype, including methods that it inherits from its ancestors.static com.google.common.base.Optional<AnnotationMirror> getAnnotationMirror(Element element, Class<? extends Annotation> annotationClass) Returns anAnnotationMirrorfor the annotation of typeannotationClassonelement, orOptional.absent()if no such annotation exists.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, non-static methods fromtype, including methods that it inherits from its ancestors.static PackageElementgetPackage(Element element) An alternate implementation ofElements.getPackageOf(javax.lang.model.element.Element)that does not require anElementsinstance.static <T extends Element>
com.google.common.base.Predicate<T> hasModifiers(Set<Modifier> modifiers) Returns aPredicatethat can be used to filter elements byModifier.static <T extends Element>
com.google.common.base.Predicate<T> hasModifiers(Modifier... modifiers) Returns aPredicatethat can be used to filter elements byModifier.static booleanisAnnotationPresent(Element element, Class<? extends Annotation> annotationClass) Returnstrueiff the given element has anAnnotationMirrorwhose annotation type has the same canonical name as that ofannotationClass.static booleanReturns true if the givenElementinstance is aTypeElement.(package private) static booleanmethodVisibleFromPackage(ExecutableElement method, PackageElement pkg) static booleanoverrides(ExecutableElement overrider, ExecutableElement overridden, TypeElement type, Types typeUtils) Tests whether one method, as a member of a given type, overrides another method.
-
Constructor Details
-
MoreElements
private MoreElements()
-
-
Method Details
-
getPackage
An alternate implementation ofElements.getPackageOf(javax.lang.model.element.Element)that does not require anElementsinstance.- Throws:
NullPointerException- ifelementisnull
-
asPackage
Returns the givenElementinstance asPackageElement.This method is functionally equivalent to an
instanceofcheck and a cast, but should always be used over that idiom as instructed in the documentation forElement.- Throws:
NullPointerException- ifelementisnullIllegalArgumentException- ifelementisn't aPackageElement.
-
isType
Returns true if the givenElementinstance is aTypeElement.This method is functionally equivalent to an
instanceofcheck, but should always be used over that idiom as instructed in the documentation forElement.- Throws:
NullPointerException- ifelementisnull
-
asType
Returns the givenElementinstance asTypeElement.This method is functionally equivalent to an
instanceofcheck and a cast, but should always be used over that idiom as instructed in the documentation forElement.- Throws:
NullPointerException- ifelementisnullIllegalArgumentException- ifelementisn't aTypeElement.
-
asTypeParameter
Returns the givenElementinstance asTypeParameterElement.This method is functionally equivalent to an
instanceofcheck and a cast, but should always be used over that idiom as instructed in the documentation forElement.- Throws:
NullPointerException- ifelementisnullIllegalArgumentException- ifelementisn't aTypeParameterElement.
-
asVariable
Returns the givenElementinstance asVariableElement.This method is functionally equivalent to an
instanceofcheck and a cast, but should always be used over that idiom as instructed in the documentation forElement.- Throws:
NullPointerException- ifelementisnullIllegalArgumentException- ifelementisn't aVariableElement.
-
asExecutable
Returns the givenElementinstance asExecutableElement.This method is functionally equivalent to an
instanceofcheck and a cast, but should always be used over that idiom as instructed in the documentation forElement.- Throws:
NullPointerException- ifelementisnullIllegalArgumentException- ifelementisn't aExecutableElement.
-
isAnnotationPresent
public static boolean isAnnotationPresent(Element element, Class<? extends Annotation> annotationClass) Returnstrueiff the given element has anAnnotationMirrorwhose 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 fornullas it avoids any interaction with annotation proxies. -
getAnnotationMirror
public static com.google.common.base.Optional<AnnotationMirror> getAnnotationMirror(Element element, Class<? extends Annotation> annotationClass) Returns anAnnotationMirrorfor the annotation of typeannotationClassonelement, 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 aPredicatethat can be used to filter elements byModifier. The predicate returnstrueif the inputElementhas 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 aPredicatethat can be used to filter elements byModifier. The predicate returnstrueif the inputElementhas 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, non-static methods fromtype, including methods that it inherits from its ancestors. Inherited methods that are overridden are not included in the result. So iftypedefinespublic 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
typeinherits those methods from different ancestors. For example, if it inherits from unrelated interfacesOneandTwowhich 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
implementsorextends.- Parameters:
type- the type whose own and inherited methods are to be returnedelementUtils- anElementsobject, 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, non-static methods fromtype, including methods that it inherits from its ancestors. Inherited methods that are overridden are not included in the result. So iftypedefinespublic 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
typeinherits those methods from different ancestors. For example, if it inherits from unrelated interfacesOneandTwowhich 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
implementsorextends.- Parameters:
type- the type whose own and inherited methods are to be returnedtypeUtils- aTypesobject, typically returned byprocessingEnv.getTypeUtils()elementUtils- anElementsobject, typically returned byprocessingEnv.getElementUtils()
-
getLocalAndInheritedMethods
private static com.google.common.collect.ImmutableSet<ExecutableElement> getLocalAndInheritedMethods(TypeElement type, Overrides overrides) -
overrides
public static boolean overrides(ExecutableElement overrider, ExecutableElement overridden, TypeElement type, 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:
trueif and only if the first method overrides the second
-
getAllMethods
public static com.google.common.collect.ImmutableSet<ExecutableElement> getAllMethods(TypeElement type, Types typeUtils, 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 iftypedefinespublic 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
typeinherits those methods from different ancestors. For example, if it inherits from unrelated interfacesOneandTwowhich 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
implementsorextends.- Parameters:
type- the type whose own and inherited methods are to be returnedtypeUtils- aTypesobject, typically returned byprocessingEnv.getTypeUtils()elementUtils- anElementsobject, typically returned byprocessingEnv.getElementUtils()
-
getAllMethods
private static com.google.common.collect.ImmutableSet<ExecutableElement> getAllMethods(TypeElement type, Overrides overrides) -
getAllMethods
private static void getAllMethods(TypeElement type, com.google.common.collect.SetMultimap<String, ExecutableElement> methods) -
methodVisibleFromPackage
-
getLocalAndInheritedMethods(TypeElement, Types, Elements)has better consistency between Java compilers.