Corn Classpath Scanner


    Corn CPS (Classpath Scanner) module is a small java module to help you to find classes and files in your classpath. The scanner is capable to scan jar files, classpath directories and web application directories. It's published in the maven central repository, so you can use corn-cps by adding the dependency below. 

pom.xml

<dependency>
    <groupId>net.sf.corn</groupId>
    <artifactId>corn-cps</artifactId>
    <version>${cps.version}</version>
</dependency>
 
    If you don't prefer to use maven, you can always download and use jar files from the project files from the sourceforge.net web site. 

Here are the some basic samples to show how to use CPS: 

1-Finding annotated classes in a package and it's sub-packages:

List<Class<?>> classes = CPScanner.scanClasses(new ClassFilter().packageName("net.sf.corn.cps.*").annotation(SampleAnnotation.class));

2-Finding interface implemented classes in a package: 

List<Class<?>> classes = CPScanner.scanClasses(new ClassFilter().packageName("net.sf.corn.cps.sample").interfaceClass(SampleInterface.class));

3-Finding xml files : 

List<URL> resources = CPScanner.scanResources(new ResourceFilter().packageName("net.sf.corn.cps.sample").resourceName("*.xml"));



      Classpath scanning is an expensive process, so scanning your classpath by narrowing your query as much as possible is the best strategy. In order to supply this strategy, a combined filters bag class class is offered within the module. You can put your filters inside it and join them with logical "or" operator or logical "and" operator. Below is the sample that finds annotated classes in packages that start from two  distinct root :

List<Class<?>> classes = CPScanner.scanClasses(new CombinedFilter()
                .appendFilter(new ResourceFilter().packageName(packageName))
                .appendFilter(
                        new ClassFilter()
                                .annotation(SampleAnnotation.class))
                .combineWithAnd());


For more examples please see the unit tests in the source package of the project.