Class DeclarationFilter


  • public class DeclarationFilter
    extends Object
    A filter for selecting just the items of interest from a collection of declarations. The filter is said to select or to match those declarations. Filters can be created in several ways: by the static methods described below, by negating or composing existing filters, or by subclasses that implement arbitrary matching rules.

    A subclass can create an arbitrary filter simply by implementing the matches(Declaration) method.

    Examples.

    Selecting the public declarations from a collection:

         result = FILTER_PUBLIC.filter(decls);              
    Selecting class declarations (including enums):
         classFilter = DeclarationFilter.getFilter(ClassDeclaration.class);
         result = classFilter.filter(decls);                
    Selecting class declarations but excluding enums:
         enumFilter = DeclarationFilter.getFilter(EnumDeclaration.class);
         compoundFilter = classFilter.and(enumFilter.not());
         result = compoundFilter.filter(decls);             
    Selecting declarations named "Bob":
         nameFilter = new DeclarationFilter() {
                          public boolean matches(Declaration d) {
                              return d.getSimpleName().equals("Bob");
                          }
                      };
         result = nameFilter.filter(decls);         
    Since:
    1.5
    • Field Detail

      • FILTER_PUBLIC

        public static final DeclarationFilter FILTER_PUBLIC
        A filter that selects only public declarations.
      • FILTER_PROTECTED

        public static final DeclarationFilter FILTER_PROTECTED
        A filter that selects only protected declarations.
      • FILTER_PUBLIC_OR_PROTECTED

        public static final DeclarationFilter FILTER_PUBLIC_OR_PROTECTED
        A filter that selects only public or protected declarations.
      • FILTER_PACKAGE

        public static final DeclarationFilter FILTER_PACKAGE
        A filter that selects only package-private (default) declarations.
      • FILTER_PRIVATE

        public static final DeclarationFilter FILTER_PRIVATE
        A filter that selects only private declarations.
    • Constructor Detail

      • DeclarationFilter

        public DeclarationFilter()
        Constructs an identity filter: one that selects all declarations.
    • Method Detail

      • getFilter

        public static DeclarationFilter getFilter​(Collection<Modifier> mods)
        Returns a filter that selects declarations containing all of a collection of modifiers.
        Parameters:
        mods - the modifiers to match (non-null)
        Returns:
        a filter that matches declarations containing mods
      • getFilter

        public static DeclarationFilter getFilter​(Class<? extends Declaration> kind)
        Returns a filter that selects declarations of a particular kind. For example, there may be a filter that selects only class declarations, or only fields. The filter will select declarations of the specified kind, and also any subtypes of that kind; for example, a field filter will also select enum constants.
        Parameters:
        kind - the kind of declarations to select
        Returns:
        a filter that selects declarations of a particular kind
      • and

        public DeclarationFilter and​(DeclarationFilter f)
        Returns a filter that selects those declarations selected by both this filter and another.
        Parameters:
        f - filter to be composed with this one
        Returns:
        a filter that selects those declarations selected by both this filter and another
      • or

        public DeclarationFilter or​(DeclarationFilter f)
        Returns a filter that selects those declarations selected by either this filter or another.
        Parameters:
        f - filter to be composed with this one
        Returns:
        a filter that selects those declarations selected by either this filter or another
      • not

        public DeclarationFilter not()
        Returns a filter that selects those declarations not selected by this filter.
        Returns:
        a filter that selects those declarations not selected by this filter
      • matches

        public boolean matches​(Declaration decl)
        Tests whether this filter matches a given declaration. The default implementation always returns true; subclasses should override this.
        Parameters:
        decl - the declaration to match
        Returns:
        true if this filter matches the given declaration
      • filter

        public <D extends DeclarationCollection<D> filter​(Collection<D> decls)
        Returns the declarations matched by this filter. The result is a collection of the same type as the argument; the two-parameter version of filter offers control over the result type.
        Type Parameters:
        D - type of the declarations being filtered
        Parameters:
        decls - declarations being filtered
        Returns:
        the declarations matched by this filter
      • filter

        public <D extends DeclarationCollection<D> filter​(Collection<? extends Declaration> decls,
                                                            Class<D> resType)
        Returns the declarations matched by this filter, with the result being restricted to declarations of a given kind. Similar to the simpler single-parameter version of filter, but the result type is specified explicitly.
        Type Parameters:
        D - type of the declarations being returned
        Parameters:
        decls - declarations being filtered
        resType - type of the declarations being returned -- the reflective view of D
        Returns:
        the declarations matched by this filter, restricted to those of the specified type