Class MoreElements
- java.lang.Object
-
- com.google.auto.common.MoreElements
-
@Beta public final class MoreElements extends java.lang.ObjectStatic utility methods pertaining toElementinstances.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classMoreElements.CastingElementVisitor<T>private static classMoreElements.ExecutableElementVisitorprivate static classMoreElements.PackageElementVisitorprivate static classMoreElements.TypeElementVisitorprivate static classMoreElements.VariableElementVisitor
-
Constructor Summary
Constructors Modifier Constructor Description privateMoreElements()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static javax.lang.model.element.ExecutableElementasExecutable(javax.lang.model.element.Element element)Returns the givenElementinstance asExecutableElement.static javax.lang.model.element.PackageElementasPackage(javax.lang.model.element.Element element)Returns the givenElementinstance asPackageElement.static javax.lang.model.element.TypeElementasType(javax.lang.model.element.Element element)Returns the givenElementinstance asTypeElement.static javax.lang.model.element.VariableElementasVariable(javax.lang.model.element.Element element)Returns the givenElementinstance asVariableElement.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 anAnnotationMirrorfor the annotation of typeannotationClassonelement, orOptional.absent()if no such annotation exists.private static voidgetLocalAndInheritedMethods(javax.lang.model.element.PackageElement pkg, javax.lang.model.element.TypeElement type, com.google.common.collect.SetMultimap<java.lang.String,javax.lang.model.element.ExecutableElement> methods)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 methods fromtype, including methods that it inherits from its ancestors.static javax.lang.model.element.PackageElementgetPackage(javax.lang.model.element.Element element)An alternate implementation ofElements.getPackageOf(javax.lang.model.element.Element)that does not require anElementsinstance.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 aPredicatethat 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 aPredicatethat can be used to filter elements byModifier.static booleanisAnnotationPresent(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)Returnstrueiff the given element has anAnnotationMirrorwhose annotation type has the same canonical name as that ofannotationClass.static booleanisType(javax.lang.model.element.Element element)Returns true if the givenElementinstance is aTypeElement.(package private) static booleanmethodVisibleFromPackage(javax.lang.model.element.ExecutableElement method, javax.lang.model.element.PackageElement pkg)
-
-
-
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 anElementsinstance.- Throws:
java.lang.NullPointerException- ifelementisnull
-
asPackage
public static javax.lang.model.element.PackageElement asPackage(javax.lang.model.element.Element element)
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:
java.lang.NullPointerException- ifelementisnulljava.lang.IllegalArgumentException- ifelementisn't aPackageElement.
-
isType
public static boolean isType(javax.lang.model.element.Element element)
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:
java.lang.NullPointerException- ifelementisnull
-
asType
public static javax.lang.model.element.TypeElement asType(javax.lang.model.element.Element element)
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:
java.lang.NullPointerException- ifelementisnulljava.lang.IllegalArgumentException- ifelementisn't aTypeElement.
-
asVariable
public static javax.lang.model.element.VariableElement asVariable(javax.lang.model.element.Element element)
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:
java.lang.NullPointerException- ifelementisnulljava.lang.IllegalArgumentException- ifelementisn't aVariableElement.
-
asExecutable
public static javax.lang.model.element.ExecutableElement asExecutable(javax.lang.model.element.Element element)
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:
java.lang.NullPointerException- ifelementisnulljava.lang.IllegalArgumentException- ifelementisn't aExecutableElement.
-
isAnnotationPresent
public static boolean isAnnotationPresent(javax.lang.model.element.Element element, java.lang.Class<? extends java.lang.annotation.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 . This() { super(); } . This() { super(); } 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 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 javax.lang.model.element.Element> com.google.common.base.Predicate<T> hasModifiers(javax.lang.model.element.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 javax.lang.model.element.Element> com.google.common.base.Predicate<T> hasModifiers(java.util.Set<javax.lang.model.element.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<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 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.- 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<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 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.- 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<javax.lang.model.element.ExecutableElement> getLocalAndInheritedMethods(javax.lang.model.element.TypeElement type, Overrides overrides)
-
getLocalAndInheritedMethods
private static void getLocalAndInheritedMethods(javax.lang.model.element.PackageElement pkg, 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)
-
-