common_helper/src/main/java/com/xiongyingqi/util/ClassLookupHelper.java
private static void lookupClassesInJar(String packageName, URL jarUrl, boolean recursive, ClassLoader loader, ClassFileFilter filter, Set<Class<?>> klasses) {
        String packageDirName = "";
        if (packageName != null && packageName.length() > 0) {
            packageDirName = packageName.replace('.', '/') + '/';
        }

        JarFile jar = null;
        try {
            if ("jar".equals(jarUrl.getProtocol())) {
                jar = ((JarURLConnection) jarUrl.openConnection()).getJarFile();
            } else {
                jar = new JarFile(jarUrl.getFile());
            }
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                // 获取jar里的一个实体 可以是目录和一些jar包里的其他文件 如META-INF等文件
                JarEntry entry = entries.nextElement();
                if (entry.isDirectory()) {
                    continue;
                }

                String name = entry.getName();
                if (name.charAt(0) == '/') {
                    name = name.substring(1);
                }
                if (name.startsWith(packageDirName) && name.endsWith(".class")) {
                    if (name.lastIndexOf('/') > packageDirName.length()) {
                        // 在子包内
                        if (!recursive) continue;
                    }
                    // 去掉后面的 .class 只留下类名
                    String klassName = name.substring(0, name.length() - 6);
                    klassName = klassName.replace('/', '.');
                    try {
                        if (filter == null || filter.accept(klassName, jar, entry, loader)) {
                            Class<?> klass = loader.loadClass(klassName);
                            klasses.add(klass);
                        }
                    } catch (Throwable e) {
                    }
                }
            }
        } catch (IOException e) {
        } finally {
        }
    }
private static void lookupClassesInJar(String packageName, URL jarUrl, boolean recursive, ClassLoader loader, ClassFileFilter filter, Set<Class<?>> klasses) {
        String packageDirName = "";
        if (packageName != null && packageName.length() > 0) {
            packageDirName = packageName.replace('.', '/') + '/';
        }

        JarFile jar = null;
        try {
            if ("jar".equals(jarUrl.getProtocol())) {
                jar = ((JarURLConnection) jarUrl.openConnection()).getJarFile();
            } else {
                jar = new JarFile(jarUrl.getFile());
            }
            Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                // 获取jar里的一个实体 可以是目录和一些jar包里的其他文件 如META-INF等文件
                JarEntry entry = entries.nextElement();
                if (entry.isDirectory()) {
                    continue;
                }

                String name = entry.getName();
                if (name.charAt(0) == '/') {
                    name = name.substring(1);
                }
                if (name.startsWith(packageDirName) && name.endsWith(".class")) {
                    if (name.lastIndexOf('/') > packageDirName.length()) {
                        // 在子包内
                        if (!recursive) continue;
                    }
                    // 去掉后面的 .class 只留下类名
                    String klassName = name.substring(0, name.length() - 6);
                    klassName = klassName.replace('/', '.');
                    try {
                        if (filter == null || filter.accept(klassName, jar, entry, loader)) {
                            Class<?> klass = loader.loadClass(klassName);
                            klasses.add(klass);
                        }
                    } catch (Throwable e) {
                    }
                }
            }
        } catch (IOException e) {
        } finally {
        }
    }
以在 Jar 包中获取指定包下的所有 Class

@param packageName JAVA包名
@param jarUrl      Jar    包文件对应的 URL
@param recursive   是否递归查找
@param loader      负责该包的 ClassLoader
@param filter      class 过滤器
@param klasses     返回找到的 class