common_helper/src/main/java/com/xiongyingqi/util/ClassLookupHelper.java
private static void lookupClassesInFileSystem(String packageName, File packagePath, final boolean recursive, ClassLoader loader, ClassFileFilter filter, Set<Class<?>> klasses) {
        if (!packagePath.exists() || !packagePath.isDirectory()) {
            return;
        }
        File[] dirfiles = packagePath.listFiles(new FileFilter() {
            public boolean accept(File file) {
                return (recursive && file.isDirectory()) || (file.getName().endsWith(".class"));
            }
        });

        String packageNamePrefix = "";
        if (packageName != null && packageName.length() > 0) {
            packageNamePrefix = packageName + '.';
        }
        for (File file : dirfiles) {
            if (file.isDirectory()) {
                lookupClassesInFileSystem(packageNamePrefix + file.getName(), file, recursive, loader, filter, klasses);
            } else {
                // 去掉后面的 .class 只留下类名
                String klassName = packageNamePrefix + file.getName().substring(0, file.getName().length() - 6);
                try {
                    if (filter == null || filter.accept(klassName, file, loader)) {
                        Class<?> klass = loader.loadClass(klassName);
                        klasses.add(klass);
                    }
                } catch (Throwable e) {
                }
            }
        }
    }
private static void lookupClassesInFileSystem(String packageName, File packagePath, final boolean recursive, ClassLoader loader, ClassFileFilter filter, Set<Class<?>> klasses) {
        if (!packagePath.exists() || !packagePath.isDirectory()) {
            return;
        }
        File[] dirfiles = packagePath.listFiles(new FileFilter() {
            public boolean accept(File file) {
                return (recursive && file.isDirectory()) || (file.getName().endsWith(".class"));
            }
        });

        String packageNamePrefix = "";
        if (packageName != null && packageName.length() > 0) {
            packageNamePrefix = packageName + '.';
        }
        for (File file : dirfiles) {
            if (file.isDirectory()) {
                lookupClassesInFileSystem(packageNamePrefix + file.getName(), file, recursive, loader, filter, klasses);
            } else {
                // 去掉后面的 .class 只留下类名
                String klassName = packageNamePrefix + file.getName().substring(0, file.getName().length() - 6);
                try {
                    if (filter == null || filter.accept(klassName, file, loader)) {
                        Class<?> klass = loader.loadClass(klassName);
                        klasses.add(klass);
                    }
                } catch (Throwable e) {
                }
            }
        }
    }
private static void lookupClassesInFileSystem ( String packageName , File packagePath , final boolean recursive , ClassLoader loader , ClassFileFilter filter , Set < Class < ? > > klasses ) { if ( ! packagePath . exists ( ) || ! packagePath . isDirectory ( ) ) { return ; } File [ ] dirfiles = packagePath . listFiles ( new FileFilter ( ) { public boolean accept ( File file ) { return ( recursive && file . isDirectory ( ) ) || ( file . getName ( ) . endsWith ( ".class" ) ) ; } } ) ; String packageNamePrefix = "" ; if ( packageName != null && packageName . length ( ) > 0 ) { packageNamePrefix = packageName + ' ' ; } for ( File file : dirfiles ) { if ( file . isDirectory ( ) ) { lookupClassesInFileSystem ( packageNamePrefix + file . getName ( ) , file , recursive , loader , filter , klasses ) ; } else { // 去掉后面的 .class 只留下类名 String klassName = packageNamePrefix + file . getName ( ) . substring ( 0 , file . getName ( ) . length ( ) - 6 ) ; try { if ( filter == null || filter . accept ( klassName , file , loader ) ) { Class < ? > klass = loader . loadClass ( klassName ) ; klasses . add ( klass ) ; } } catch ( Throwable e ) { } } } }
以文件的形式来获取包下的所有 Class

@param packageName JAVA包名
@param packagePath 包所在的文件目录
@param recursive   是否递归查找
@param loader      负责该包的 ClassLoader
@param filter      class 过滤器
@param klasses     返回找到的 class
以文件的形式来获取包下的所有 Class