Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.kasource.commons.reflection.filter;
  
  import java.util.List;
  
Builder for FieldFilter.

This class offers functionality for building more complex filter compositions in an expressive manner. The filters added will be evaluated with AND operator. This builder also allows both NOT and OR as additional operators.

Examples: FieldFilter publicResourceInjectionTargets = new FieldFilterBuilder().isPublic().annotated(javax.annotation.Resource.class).build();

Author(s):
rikardwi
 
 public class FieldFilterBuilder {
     private enum Operator {NONE, NOT, OR};
     private List<FieldFilterfilters = new ArrayList<FieldFilter>();
     private Operator operator = .;
    
    
Adds a filter and applies the current operator.

Parameters:
filter Filter to add.
 
     private void add(FieldFilter filter) {
         switch() {
         case :
             .add(new NegationFieldFilter(filter));
              = .;
             break;
         case :
             .set(.size() - 1, 
                         new OrFieldFilter(.get(.size() - 1), filter));
              = .;
             break;
         default:
             .add(filter);
             break;
         }
     }
    
    
Sets the current operator to NOT.

Returns:
This builder to support method chaining.
 
     public FieldFilterBuilder not() {
          = .;
         return this;
     }
    
    
Sets the current operator to OR.

Returns:
This builder to support method chaining.
 
     public FieldFilterBuilder or() {
          = .;
         return this;
     }
    
    
Filter for fields which name matches to supplied regular expression.

Parameters:
nameRegExp Regular expression to match field name with.
Returns:
This builder to support method chaining.
 
     public FieldFilterBuilder name(String nameRegExp) {
         add(new NameFieldFilter(nameRegExp));
         return this;
     }
    
    
Filter fields that is annotated with the supplied annotation.

Parameters:
annotation The annotation to be present on filtered fields.
Returns:
This builder to support method chaining.
    public FieldFilterBuilder annotated(Class<? extends Annotationannotation) {
        add(new AnnotatedFieldFilter(annotation));
        return this;
    }
    
    
Adds filter for fields annotated with an annotation which is annotated with the supplied annotation. Think springs stereotype annotations:

Parameters:
annotation The meta annotation to find.
Returns:
This builder to support method chaining.
Service:
and @Repository is annotated with @Component. Thus I can find any @Component classes (by looking for the meta annotation Component) even though they are actually annotated with Service or Repository with a metaAnnotated(org.springframework.stereotype.Component) filter.
    public FieldFilterBuilder metaAnnotated(Class<? extends Annotationannotation) {
        add(new MetaAnnotatedFieldFilter(annotation));
        return this;
    }
    
    
Adds filter for fields which type is assignable from a superType. extendsType(java.lang.Number) will be true for fields of type java.lang.Integer, since java.lang.Integer extends java.lang.Number.

Parameters:
superType the class any matching classes should extend or implement.
Returns:
This builder to support method chaining.
    public FieldFilterBuilder extendsType(Class<?> superType) {
        add(new AssignableFromFieldFilter(superType));
        return this;
    }
    
    
Adds filter for fields which type is assignable to an extendedType. superType(java.lang.Integer) will be true for a fields of type java.lang.Number, since java.lang.Number is a superType of java.lang.Integer.

Parameters:
extendedType The class that any matching should be a super class of.
Returns:
This builder to support method chaining.
    public FieldFilterBuilder superType(Class<?> extendedType) {
        add(new AssignableToFieldFilter(extendedType));
        return this;
    }
    
    
Filter fields which type passes the supplied ClassFilter.

Parameters:
filter The class filter to apply of candidate fields type.
Returns:
This builder to support method chaining.
    public FieldFilterBuilder typeFilter(ClassFilter filter) {
        add(new FieldClassFieldFilter(filter));
        return this;
    }
    
    
Filter fields which are enum constants.

Returns:
This builder to support method chaining.
        add(new IsEnumConstantFieldFilter());
        return this;
    }
    
    
Filter public fields.

Returns:
This builder to support method chaining.
    public FieldFilterBuilder isPublic() {
        add(new ModifierFieldFilter(.));
        return this;
    }
    
    

Returns:
This builder to support method chaining.
    public FieldFilterBuilder isProtected() {
        return this;
    }
    
    
Filter private fields.

Returns:
This builder to support method chaining.
    public FieldFilterBuilder isPrivate() {
        add(new ModifierFieldFilter(.));
        return this;
    }
    
    
Filter static fields.

Returns:
This builder to support method chaining.
    public FieldFilterBuilder isStatic() {
        add(new ModifierFieldFilter(.));
        return this;
    }
    
    
Filter transient fields.

Returns:
This builder to support method chaining.
    public FieldFilterBuilder isTransient() {
        return this;
    }
    
    
Filter final fields.

Returns:
This builder to support method chaining.
    public FieldFilterBuilder isFinal() {
        add(new ModifierFieldFilter(.));
        return this;
    }
    
    
Filter volatile fields.

Returns:
This builder to support method chaining.
    public FieldFilterBuilder isVolatile() {
        return this;
    }
    
    
Filter fields with default access.

Returns:
This builder to support method chaining.
    public FieldFilterBuilder isDefault() {
        return this;
    }
    
    
Filter fields that has the supplied modifiers.

Parameters:
modifiers Modifiers to match on candidate field.
Returns:
This builder to support method chaining.
    public FieldFilterBuilder byModifiers(int modifiers) {
        add(new ModifierFieldFilter(modifiers));
        return this;
    }
    
    
Build and returns a FieldFilter.

Returns:
FieldFilter built.
Throws:
java.lang.IllegalStateException if no filter was added before build() was invoked.
    public FieldFilter build() throws IllegalStateException {
        if (.isEmpty()) {
            throw new IllegalStateException("No field filters configured!");
        }
        if (.size() == 1) {
           return .get(0); 
        }
        FieldFilter[] fieldFilters = new FieldFilter[.size()];
        .toArray(fieldFilters);
        return new FieldFilterList(fieldFilters);
    }
New to GrepCode? Check out our FAQ X