private Set < ClassInfo > findClassByPackage ( final String packageName , final String packagePath , final Class < ? > parent , final Class < ? extends Annotation > annotation , final boolean recursive , Set < ClassInfo > classes ) throws ClassNotFoundException { // Get the directory of this package to create a File File dir = new File ( packagePath ) ; // If not exist or is not a direct return to the directory if ( ( ! dir . exists ( ) ) || ( ! dir . isDirectory ( ) ) ) { log . warn ( "The package [{}] not found." , packageName ) ; } // If present, get all the files under the package include the directory File [ ] dirFiles = accept ( dir , recursive ) ; // Loop all files if ( null != dirFiles && dirFiles . length > 0 ) { for ( File file : dirFiles ) { // If it is a directory, continue scanning if ( file . isDirectory ( ) ) { findClassByPackage ( packageName + ' ' + file . getName ( ) , file . getAbsolutePath ( ) , parent , annotation , recursive , classes ) ; } else { // If the java class file is removed later. Class only leave the class name String className = file . getName ( ) . substring ( 0 , file . getName ( ) . length ( ) - 6 ) ; Class < ? > clazz = Class . forName ( packageName + ' ' + className ) ; if ( null != parent && null != annotation ) { if ( null != clazz . getSuperclass ( ) && clazz . getSuperclass ( ) . equals ( parent ) && null != clazz . getAnnotation ( annotation ) ) { classes . add ( new ClassInfo ( clazz ) ) ; } continue ; } if ( null != parent ) { if ( null != clazz . getSuperclass ( ) && clazz . getSuperclass ( ) . equals ( parent ) ) { classes . add ( new ClassInfo ( clazz ) ) ; } else { if ( null != clazz . getInterfaces ( ) && clazz . getInterfaces ( ) . length > 0 && clazz . getInterfaces ( ) [ 0 ] . equals ( parent ) ) { classes . add ( new ClassInfo ( clazz ) ) ; } } continue ; } if ( null != annotation ) { if ( null != clazz . getAnnotation ( annotation ) ) { classes . add ( new ClassInfo ( clazz ) ) ; } continue ; } classes . add ( new ClassInfo ( clazz ) ) ; } } } return classes ; }
Get class by condition