Package com.sun.mirror.util
Class DeclarationFilter
java.lang.Object
com.sun.mirror.util.DeclarationFilter
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:
Selecting class declarations (including enums):result = FILTER_PUBLIC.filter(decls);
Selecting class declarations but excluding enums:classFilter = DeclarationFilter.getFilter(ClassDeclaration.class); result = classFilter.filter(decls);
Selecting declarations named "Bob":enumFilter = DeclarationFilter.getFilter(EnumDeclaration.class); compoundFilter = classFilter.and(enumFilter.not()); result = compoundFilter.filter(decls);
nameFilter = new DeclarationFilter() { public boolean matches(Declaration d) { return d.getSimpleName().equals("Bob"); } }; result = nameFilter.filter(decls);
- Since:
- 1.5
- Version:
- 1.2 04/07/19
- Author:
- Joseph D. Darcy, Scott Seligman
-
Field Summary
Modifier and TypeFieldDescriptionstatic final DeclarationFilter
A filter that selects only package-private (default) declarations.static final DeclarationFilter
A filter that selects onlyprivate
declarations.static final DeclarationFilter
A filter that selects onlyprotected
declarations.static final DeclarationFilter
A filter that selects onlypublic
declarations.static final DeclarationFilter
A filter that selects onlypublic
orprotected
declarations. -
Constructor Summary
ConstructorDescriptionConstructs an identity filter: one that selects all declarations. -
Method Summary
Modifier and TypeMethodDescriptionReturns a filter that selects those declarations selected by both this filter and another.<D extends Declaration>
Collection<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.<D extends Declaration>
Collection<D>filter
(Collection<D> decls) Returns the declarations matched by this filter.static DeclarationFilter
getFilter
(Class<? extends Declaration> kind) Returns a filter that selects declarations of a particular kind.static DeclarationFilter
getFilter
(Collection<Modifier> mods) Returns a filter that selects declarations containing all of a collection of modifiers.boolean
matches
(Declaration decl) Tests whether this filter matches a given declaration.not()
Returns a filter that selects those declarations not selected by this filter.Returns a filter that selects those declarations selected by either this filter or another.
-
Field Details
-
FILTER_PUBLIC
A filter that selects onlypublic
declarations. -
FILTER_PROTECTED
A filter that selects onlyprotected
declarations. -
FILTER_PUBLIC_OR_PROTECTED
A filter that selects onlypublic
orprotected
declarations. -
FILTER_PACKAGE
A filter that selects only package-private (default) declarations. -
FILTER_PRIVATE
A filter that selects onlyprivate
declarations.
-
-
Constructor Details
-
DeclarationFilter
public DeclarationFilter()Constructs an identity filter: one that selects all declarations.
-
-
Method Details
-
getFilter
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
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
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
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
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
Tests whether this filter matches a given declaration. The default implementation always returnstrue
; subclasses should override this.- Parameters:
decl
- the declaration to match- Returns:
true
if this filter matches the given declaration
-
filter
Returns the declarations matched by this filter. The result is a collection of the same type as the argument; the two-parameter version offilter
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 Declaration> Collection<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 offilter
, but the result type is specified explicitly.- Type Parameters:
D
- type of the declarations being returned- Parameters:
decls
- declarations being filteredresType
- type of the declarations being returned -- the reflective view ofD
- Returns:
- the declarations matched by this filter, restricted to those of the specified type
-