Class MoreElements


  • @Beta
    public final class MoreElements
    extends Object
    Static utility methods pertaining to Element instances.
    Author:
    Gregory Kick
    • Method Detail

      • 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
      • hasModifiers

        public static com.google.common.base.Predicate<Element> 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 com.google.common.base.Predicate<Element> 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

        public static com.google.common.collect.ImmutableSet<ExecutableElement> getLocalAndInheritedMethods​(TypeElement type,
                                                                                                            Elements elementUtils)
        Returns the set of all non-private 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.

        Parameters:
        type - the type whose own and inherited methods are to be returned
        elementUtils - an Elements object, typically returned by processingEnv.{@link javax.annotation.processing.ProcessingEnvironment.getElementUtils() getElementUtils()}