Java Element Filters Example

Filter Provider

Implement myfilters.MyFilterProvider as follows:

package myjavaelementfilters; 

import java.util.HashSet; 
import java.util.Set; 

import com.agilej.filters.api.exp.IFilter; 
import com.agilej.filters.api.exp.IFilterProvider; 

/** 
 * This is an example filter provider. 
 * It is called by AgileJ StructureViews to discover custom Java element filters. 
 */ 
public class MyFilterProvider implements IFilterProvider {

    /** 
     * This set contains an instance of each of the filters we define 
     */ 
    final private Set<IFilter> filters;
     
    public MyFilterProvider() { 
        filters = new HashSet<IFilter>(); 
        filters.add(new InternalTypeFilter()); 
    } 

    @Override 
    public Set<IFilter> getFilters() { 
        return filters; 
    } 

    @Override 
    public String getName() { 
        return "my filter provider"; 
    } 
} 

Notice that IFilterProvider returns a set of IElementFilter instances, but each instance is an implementation of one of

  • ITypeFilter,
  • IMethodFilter or
  • IFieldFilter.

Each filter has an includes method, which is where the main filtering business takes place. This method accepts two parameters. The first is an Eclipse JDT element which is the subject of the filtering process, and the second is a collection of Strings.

In the example above we add just one ITypeFilter, the definition for which is as follows:

Type Filter

package myjavaelementfilters; 

import java.util.List; 

import org.eclipse.jdt.core.IPackageFragment; 
import org.eclipse.jdt.core.IType; 

import com.agilej.filters.api.exp.IFilter; 
import com.agilej.filters.api.exp.ITypeFilter; 

/** 
 * A filter for types declared in an 'internal' package 
 */ 
public class InternalTypeFilter implements ITypeFilter {

    /** 
     * @return a meaningful description of what this filter includes 
     */ 
    @Override 
    public String getDescription() { 
        return "Types declared in a package with '.internal.' in its name"; 
    } 

    /** 
     * This is how the filter will be called in the filter script 
     * Runtime parameters (switches) are named within curley brackets 
     */ 
    @Override 
    public String getSignature() { 
        return "internal types"; 
    } 

    /** 
     * Return true if the results of this filter can be  
     * used in a batch run and viewed in a browser 
     */ 
    @Override 
    public boolean isExportable() {
        return true; 
    } 

    /** 
     * Used for sorting filters as they are listed 
     */ 
    @Override 
    public int compareTo(IFilter otherFilter) {
        return getSignature().compareTo(otherFilter.getSignature());
    } 

    /** 
     * Return the actual switch values for the given IType 
     */ 
    @Override 
    public List<String> getExportedSwitchValues(IType iType) {
        // this filter has no switches 
        return null; 
    } 

    /** 
     * The stereotype label to add to included types 
     */ 
    @Override 
    public String getStereotype() { 
        return "Internal"; 
    } 

    /** 
     * Perform the actual filtering  
     * @param iType the type to filter   
     * @param switchValues a list of runtime switch values 
     */ 
    @Override 
    public boolean includes(IType iType, List<String> switchValues) throws Exception {
        IPackageFragment packageFragment = iType.getPackageFragment(); 
        return packageFragment.getElementName().contains(".internal."); 
    } 
} 

Copyright © AgileJ Ltd. All rights reserved.

AgileJ StructureViews is a plugin for Eclipse which generates UML class diagrams reverse engineered from Java source code.

Learn More

Stay Up To Date


Get In Touch

AgileJ Ltd
Trident Court, 1 Oakcroft Road
Chessington, Surrey
KT9 1BD
United Kingdom
+44 20 8123 2318