Components registration - package scanning

This commit is contained in:
Emmanuel BENOîT 2015-09-16 17:50:13 +02:00
parent 0c23f613d6
commit 6d0753c7a8
11 changed files with 169 additions and 2 deletions
src/main/java/info/ebenoit/ebul/cmp

View file

@ -1,6 +1,7 @@
package info.ebenoit.ebul.cmp;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@ -15,7 +16,9 @@ import info.ebenoit.ebul.func.ThrowingBiConsumer;
import info.ebenoit.ebul.func.ThrowingConsumer;
import info.ebenoit.ebul.func.ThrowingSupplier;
import info.ebenoit.ebul.reflection.Annotations;
import info.ebenoit.ebul.reflection.Classes;
import info.ebenoit.ebul.reflection.MemberFinder;
import info.ebenoit.ebul.reflection.PackageScanner;
@ -55,6 +58,39 @@ public final class NewComponentInfo< T >
}
/**
* Scans a package for classes that correspond to annotated components and processes them using
* {@link #fromClass(Class)}
*
* @param name
* the name of the package to scan
* @param recursive
* <code>true</code> if child packages are to be scanned as well
* @return the list of component registration records
* @throws IOException
* if an I/O error occurs while scanning the packages
* @throws ClassNotFoundException
* if one of the classes cannot be loaded
* @throws ComponentDefinitionException
* if one of the loaded definitions is invalid
*/
public static ArrayList< NewComponentInfo< ? > > scanPackage( final String name , final boolean recursive )
throws IOException , ClassNotFoundException , ComponentDefinitionException
{
final PackageScanner packageScanner = new PackageScanner( name , recursive );
final ArrayList< Class< ? > > classes = packageScanner.findClasses( //
c -> ( ( c.getModifiers( ) & ( Modifier.ABSTRACT | Modifier.INTERFACE ) ) == 0
&& Classes.hasAncestorMatching( c , x -> x.isAnnotationPresent( Component.class ) , true ) ) );
final ArrayList< NewComponentInfo< ? > > results = new ArrayList< >( );
final int n = classes.size( );
for ( int i = 0 ; i < n ; i++ ) {
results.add( NewComponentInfo.fromClass( classes.get( i ) ) );
}
return results;
}
/**
* Creates a new component information record based on a component's annotated class.
*
@ -540,7 +576,7 @@ public final class NewComponentInfo< T >
}
@SuppressWarnings( "unchecked" )
ThrowingBiConsumer< Object , Object > ci = (ThrowingBiConsumer< Object , Object >) injector;
final ThrowingBiConsumer< Object , Object > ci = (ThrowingBiConsumer< Object , Object >) injector;
injectors.add( ci );
this.dependencies.add( dependency );