Class MoreElements

java.lang.Object
com.google.auto.common.MoreElements

@Beta public final class MoreElements extends Object
Static utility methods pertaining to Element instances.
  • Constructor Details

    • MoreElements

      private MoreElements()
  • Method Details

    • getPackage

      public static PackageElement getPackage(Element element)
      An alternate implementation of Elements.getPackageOf(javax.lang.model.element.Element) that does not require an Elements instance.
      Throws:
      NullPointerException - if element is null
    • asPackage

      public static PackageElement asPackage(Element element)
      Returns the given 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.

      Throws:
      NullPointerException - if element is null
      IllegalArgumentException - if element isn't a PackageElement.
    • isType

      public static boolean isType(Element element)
      Returns true if the given 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.

      Throws:
      NullPointerException - if element is null
    • asType

      public static TypeElement asType(Element element)
      Returns the given 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.

      Throws:
      NullPointerException - if element is null
      IllegalArgumentException - if element isn't a TypeElement.
    • asTypeParameter

      public static TypeParameterElement asTypeParameter(Element element)
      Returns the given Element instance as TypeParameterElement.

      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.

      Throws:
      NullPointerException - if element is null
      IllegalArgumentException - if element isn't a TypeParameterElement.
    • asVariable

      public static VariableElement asVariable(Element element)
      Returns the given 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.

      Throws:
      NullPointerException - if element is null
      IllegalArgumentException - if element isn't a VariableElement.
    • asExecutable

      public static ExecutableElement asExecutable(Element element)
      Returns the given 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.

      Throws:
      NullPointerException - if element is null
      IllegalArgumentException - if element isn't a ExecutableElement.
    • isAnnotationPresent

      public static boolean isAnnotationPresent(Element element, Class<? extends Annotation> annotationClass)
      Returns 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.
    • getAnnotationMirror

      public static com.google.common.base.Optional<AnnotationMirror> getAnnotationMirror(Element element, Class<? extends Annotation> annotationClass)
      Returns an 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.
    • hasModifiers

      public static <T extends Element> com.google.common.base.Predicate<T> hasModifiers(Modifier... modifiers)
      Returns a 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();
       
    • hasModifiers

      public static <T extends Element> com.google.common.base.Predicate<T> hasModifiers(Set<Modifier> modifiers)
      Returns a 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();
       
    • getLocalAndInheritedMethods

      @Deprecated public static com.google.common.collect.ImmutableSet<ExecutableElement> getLocalAndInheritedMethods(TypeElement type, Elements elementUtils)
      Deprecated.
      The method getLocalAndInheritedMethods(TypeElement, Types, Elements) has better consistency between Java compilers.
      Returns the set of all non-private, non-static methods from type, including methods that it inherits from its ancestors. Inherited methods that are overridden are not included in the result. So if type defines public String toString(), the returned set will contain that method, but not the toString() method defined by Object.

      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 interfaces One and Two which each define void foo();, and if it does not itself override the foo() method, then both One.foo() and Two.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 or extends.

      Parameters:
      type - the type whose own and inherited methods are to be returned
      elementUtils - an Elements object, typically returned by processingEnv.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 from type, including methods that it inherits from its ancestors. Inherited methods that are overridden are not included in the result. So if type defines public String toString(), the returned set will contain that method, but not the toString() method defined by Object.

      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 interfaces One and Two which each define void foo();, and if it does not itself override the foo() method, then both One.foo() and Two.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 or extends.

      Parameters:
      type - the type whose own and inherited methods are to be returned
      typeUtils - a Types object, typically returned by processingEnv.getTypeUtils()
      elementUtils - an Elements object, typically returned by processingEnv.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 overrider
      overridden - the second method, possibly being overridden
      type - 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<ExecutableElement> getAllMethods(TypeElement type, Types typeUtils, Elements elementUtils)
      Returns the set of all methods from type, including methods that it inherits from its ancestors. Inherited methods that are overridden are not included in the result. So if type defines public String toString(), the returned set will contain that method, but not the toString() method defined by Object.

      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 interfaces One and Two which each define void foo();, and if it does not itself override the foo() method, then both One.foo() and Two.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 or extends.

      Parameters:
      type - the type whose own and inherited methods are to be returned
      typeUtils - a Types object, typically returned by processingEnv.getTypeUtils()
      elementUtils - an Elements object, typically returned by processingEnv.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

      static boolean methodVisibleFromPackage(ExecutableElement method, PackageElement pkg)