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:
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
- Version:
- 1.2 04/07/19
- Author:
- Joseph D. Darcy, Scott Seligman
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DeclarationFilterA filter that selects only package-private (default) declarations.static final DeclarationFilterA filter that selects onlyprivatedeclarations.static final DeclarationFilterA filter that selects onlyprotecteddeclarations.static final DeclarationFilterA filter that selects onlypublicdeclarations.static final DeclarationFilterA filter that selects onlypublicorprotecteddeclarations. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs 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 DeclarationFiltergetFilter(Class<? extends Declaration> kind) Returns a filter that selects declarations of a particular kind.static DeclarationFiltergetFilter(Collection<Modifier> mods) Returns a filter that selects declarations containing all of a collection of modifiers.booleanmatches(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 onlypublicdeclarations. -
FILTER_PROTECTED
A filter that selects onlyprotecteddeclarations. -
FILTER_PUBLIC_OR_PROTECTED
A filter that selects onlypublicorprotecteddeclarations. -
FILTER_PACKAGE
A filter that selects only package-private (default) declarations. -
FILTER_PRIVATE
A filter that selects onlyprivatedeclarations.
-
-
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:
trueif 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 offilteroffers 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
-