是否有可能找到给定包中的所有类或接口?(快速看了一下e.g. Package,似乎没有。)


当前回答

FindAllClassesUsingPlainJavaReflectionTest.java

@Slf4j
class FindAllClassesUsingPlainJavaReflectionTest {

  private static final Function<Throwable, RuntimeException> asRuntimeException = throwable -> {
    log.error(throwable.getLocalizedMessage());
    return new RuntimeException(throwable);
  };

  private static final Function<String, Collection<Class<?>>> findAllPackageClasses = basePackageName -> {

    Locale locale = Locale.getDefault();
    Charset charset = StandardCharsets.UTF_8;
    val fileManager = ToolProvider.getSystemJavaCompiler()
                                  .getStandardFileManager(/* diagnosticListener */ null, locale, charset);

    StandardLocation location = StandardLocation.CLASS_PATH;
    JavaFileObject.Kind kind = JavaFileObject.Kind.CLASS;
    Set<JavaFileObject.Kind> kinds = Collections.singleton(kind);
    val javaFileObjects = Try.of(() -> fileManager.list(location, basePackageName, kinds, /* recurse */ true))
                             .getOrElseThrow(asRuntimeException);

    String pathToPackageAndClass = basePackageName.replace(".", File.separator);
    Function<String, String> mapToClassName = s -> {
      String prefix = Arrays.stream(s.split(pathToPackageAndClass))
                            .findFirst()
                            .orElse("");
      return s.replaceFirst(prefix, "")
              .replaceAll(File.separator, ".");
    };

    return StreamSupport.stream(javaFileObjects.spliterator(), /* parallel */ true)
                        .filter(javaFileObject -> javaFileObject.getKind().equals(kind))
                        .map(FileObject::getName)
                        .map(fileObjectName -> fileObjectName.replace(".class", ""))
                        .map(mapToClassName)
                        .map(className -> Try.of(() -> Class.forName(className))
                                             .getOrElseThrow(asRuntimeException))
                        .collect(Collectors.toList());
  };

  @Test
  @DisplayName("should get classes recursively in given package")
  void test() {
    Collection<Class<?>> classes = findAllPackageClasses.apply(getClass().getPackage().getName());
    assertThat(classes).hasSizeGreaterThan(4);
    classes.stream().map(String::valueOf).forEach(log::info);
  }
}

PS:为了简化处理错误的样板文件等,我在这里使用了vavr和lombok库

其他实现可以在我的GitHub daggerok/java-reflection-find- annotation -classes-or-methods repo中找到

其他回答

在包测试中定义要扫描的类

package test;

public class A {
  private class B {}

  enum C {}

  record D() {}
}

对于org.reflections:reflections:0.10.2,它为我工作如下:

使用反射库扫描包测试中的类

@Test
void t() {
  final String packagePath = "test";
  final Reflections reflections =
    new Reflections(packagePath, Scanners.SubTypes.filterResultsBy(v -> true));
  reflections.getAll(Scanners.SubTypes).forEach(System.out::println);
}

输出

java.lang.constant.Constable
java.lang.Enum
java.lang.Comparable
java.lang.Record
java.lang.Object
java.io.Serializable
test.A$C
test.A$D
test.A$B
test.A

对于io.github.classgraph:classgraph:4.8.146,它为我工作如下:


@Test
void t() {
  final String packagePath = "test";
  try (ScanResult scanResult = new ClassGraph()
    .enableClassInfo()
    .ignoreClassVisibility()
    .acceptPackages(packagePath)
    .scan()) {
    
    scanResult.getAllClasses()
      .forEach(v -> {
        System.out.println(v.getName());
      });
  }
}

输出

test.A
test.A$B
test.A$C
test.A$D

是的,你可以使用很少的API,这是我喜欢做的,面对这个问题,我使用hibernate核心&必须找到类,其中注释了某个注释。

使这些自定义注释使用,您将标记哪些类您想要获得。

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface EntityToBeScanned {

}

然后用它来标记你的课

@EntityToBeScanned 
public MyClass{

}

创建具有以下方法的实用程序类

public class ClassScanner {

    public static Set<Class<?>> allFoundClassesAnnotatedWithEntityToBeScanned(){
        Reflections reflections = new Reflections(".*");
        Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(EntityToBeScanned.class);
        return annotated;
    }

}

调用allfoundclassesannotatedwithentitytobescans()方法获取找到的类集。

你将需要下列的参考书目

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>21.0</version>
    </dependency>
<!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.22.0-CR1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.reflections/reflections -->
<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.9.10</version>
</dependency>

这将扫描类加载器和所有父加载器,以查找jar文件和目录。 jar文件和由jar的类路径引用的目录也会被加载。

this code is testet with Java 8,11,18. on 8 everything works perfectly using the URLClassLoader and the getURLs() method. on 11 it works fine using reflections, but the JVM prints a warning on the stderr stream (not redirectible with System.setErr() with my JVM) on 18 the reflections are useless (throws NoSuchMethod/Field), and the only thing (where I know that it works) is to use the getResource() method. When the class loader loades the resources of the given package from the file system a simple path url is returned. When the class loader loades the resources from a jar a url like 'jar:file:[jar-path]!/[in-jar-path]' is returned.

我使用了答案https://stackoverflow.com/a/1157352/18252455(来自一个重复的问题),并添加了读取类路径和搜索目录url的功能。

/**
 * orig description:<br>
 * Scans all classloaders for the current thread for loaded jars, and then scans
 * each jar for the package name in question, listing all classes directly under
 * the package name in question. Assumes directory structure in jar file and class
 * package naming follow java conventions (i.e. com.example.test.MyTest would be in
 * /com/example/test/MyTest.class)
 * <p>
 * in addition this method also scans for directories, where also is assumed, that the classes are
 * placed followed by the java conventions. (i.e. <code>com.example.test.MyTest</code> would be in
 * <code>directory/com/example/test/MyTest.class</code>)
 * <p>
 * this method also reads the jars Class-Path for other jars and directories. for the jars and
 * directories referred in the jars are scanned with the same rules as defined here.<br>
 * it is ensured that no jar/directory is scanned exactly one time.
 * <p>
 * if {@code bailError} is <code>true</code> all errors will be wrapped in a
 * {@link RuntimeException}
 * and then thrown.<br>
 * a {@link RuntimeException} will also be thrown if something unexpected happens.<br>
 * 
 * @param packageName
 *            the name of the package for which the classes should be searched
 * @param allowSubPackages
 *            <code>true</code> is also classes in sub packages should be found
 * @param loader
 *            the {@link ClassLoader} which should be used to find the URLs and to load classes
 * @param bailError
 *            if all {@link Exception} should be re-thrown wrapped in {@link RuntimeException} and
 *            if a {@link RuntimeException} should be thrown, when something is not as expected.
 * @see https://stackoverflow.com/questions/1156552/java-package-introspection
 * @see https://stackoverflow.com/a/1157352/18252455
 * @see https://creativecommons.org/licenses/by-sa/2.5/
 * @see https://creativecommons.org/licenses/by-sa/2.5/legalcode
 */
public static Set <Class <?>> tryGetClassesForPackage(String packageName, boolean allowSubPackages, ClassLoader loader, boolean bailError) {
    Set <URL> jarUrls = new HashSet <URL>();
    Set <Path> directorys = new HashSet <Path>();
    findClassPools(loader, jarUrls, directorys, bailError, packageName);
    Set <Class <?>> jarClasses = findJarClasses(allowSubPackages, packageName, jarUrls, directorys, loader, bailError);
    Set <Class <?>> dirClasses = findDirClasses(allowSubPackages, packageName, directorys, loader, bailError);
    jarClasses.addAll(dirClasses);
    return jarClasses;
}

private static Set <Class <?>> findDirClasses(boolean subPackages, String packageName, Set <Path> directorys, ClassLoader loader, boolean bailError) {
    Filter <Path> filter;
    Set <Class <?>> result = new HashSet <>();
    for (Path startPath : directorys) {
        String packagePath = packageName.replace(".", startPath.getFileSystem().getSeparator());
        final Path searchPath = startPath.resolve(packagePath).toAbsolutePath();
        if (subPackages) {
            filter = p -> {
                p = p.toAbsolutePath();
                Path other;
                if (p.getNameCount() >= searchPath.getNameCount()) {
                    other = searchPath;
                } else {
                    other = searchPath.subpath(0, p.getNameCount());
                }
                if (p.startsWith(other)) {
                    return true;
                } else {
                    return false;
                }
            };
        } else {
            filter = p -> {
                p = p.toAbsolutePath();
                if (p.getNameCount() > searchPath.getNameCount() + 1) {
                    return false;
                } else if (p.toAbsolutePath().startsWith(searchPath)) {
                    return true;
                } else {
                    return false;
                }
            };
        }
        if (Files.exists(searchPath)) {
            findDirClassFilesRecursive(filter, searchPath, startPath, result, loader, bailError);
        } // the package does not have to exist in every directory
    }
    return result;
}

private static void findDirClassFilesRecursive(Filter <Path> filter, Path path, Path start, Set <Class <?>> classes, ClassLoader loader, boolean bailError) {
    try (DirectoryStream <Path> dirStream = Files.newDirectoryStream(path, filter)) {
        for (Path p : dirStream) {
            if (Files.isDirectory(p)) {
                findDirClassFilesRecursive(filter, p, start, classes, loader, bailError);
            } else {
                Path subp = p.subpath(start.getNameCount(), p.getNameCount());
                String str = subp.toString();
                if (str.endsWith(".class")) {
                    str = str.substring(0, str.length() - 6);
                    String sep = p.getFileSystem().getSeparator();
                    if (str.startsWith(sep)) {
                        str = str.substring(sep.length());
                    }
                    if (str.endsWith(sep)) {
                        str = str.substring(0, str.length() - sep.length());
                    }
                    String fullClassName = str.replace(sep, ".");
                    try {
                        Class <?> cls = Class.forName(fullClassName, false, loader);
                        classes.add(cls);
                    } catch (ClassNotFoundException e) {
                        if (bailError) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
    } catch (IOException e) {
        if (bailError) {
            throw new RuntimeException(e);
        }
    }
}

private static Set <Class <?>> findJarClasses(boolean subPackages, String packageName, Set <URL> nextJarUrls, Set <Path> directories, ClassLoader loader, boolean bailError) {
    String packagePath = packageName.replace('.', '/');
    Set <Class <?>> result = new HashSet <>();
    Set <URL> allJarUrls = new HashSet <>();
    while (true) {
        Set <URL> thisJarUrls = new HashSet <>(nextJarUrls);
        thisJarUrls.removeAll(allJarUrls);
        if (thisJarUrls.isEmpty()) {
            break;
        }
        allJarUrls.addAll(thisJarUrls);
        for (URL url : thisJarUrls) {
            try (JarInputStream stream = new JarInputStream(url.openStream())) {
                // may want better way to open url connections
                readJarClassPath(stream, nextJarUrls, directories, bailError);
                JarEntry entry = stream.getNextJarEntry();
                while (entry != null) {
                    String name = entry.getName();
                    int i = name.lastIndexOf("/");
                    
                    if (i > 0 && name.endsWith(".class")) {
                        try {
                            if (subPackages) {
                                if (name.substring(0, i).startsWith(packagePath)) {
                                    Class <?> cls = Class.forName(name.substring(0, name.length() - 6).replace("/", "."), false, loader);
                                    result.add(cls);
                                }
                            } else {
                                if (name.substring(0, i).equals(packagePath)) {
                                    Class <?> cls = Class.forName(name.substring(0, name.length() - 6).replace("/", "."), false, loader);
                                    result.add(cls);
                                }
                            }
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                    entry = stream.getNextJarEntry();
                }
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

private static void readJarClassPath(JarInputStream stream, Set <URL> jarUrls, Set <Path> directories, boolean bailError) {
    Object classPathObj = stream.getManifest().getMainAttributes().get(new Name("Class-Path"));
    if (classPathObj == null) {
        return;
    }
    if (classPathObj instanceof String) {
        String[] entries = ((String) classPathObj).split("\\s+");// should also work with a single space (" ")
        for (String entry : entries) {
            try {
                URL url = new URL(entry);
                addFromUrl(jarUrls, directories, url, bailError);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    } else if (bailError) {
        throw new RuntimeException("the Class-Path attribute is no String: " + classPathObj.getClass().getName() + " tos='" + classPathObj + "'");
    }
}

private static void findClassPools(ClassLoader classLoader, Set <URL> jarUrls, Set <Path> directoryPaths, boolean bailError, String packageName) {
    packageName = packageName.replace('.', '/');
    while (classLoader != null) {
        if (classLoader instanceof URLClassLoader) {
            for (URL url : ((URLClassLoader) classLoader).getURLs()) {
                addFromUrl(jarUrls, directoryPaths, url, bailError);
                System.out.println("rurl-class-loade.url[n]r->'" + url + "'");
            }
        } else {
            URL res = classLoader.getResource("");
            if (res != null) {
                addFromUrl(jarUrls, directoryPaths, res, bailError);
            }
            res = classLoader.getResource("/");
            if (res != null) {
                addFromUrl(jarUrls, directoryPaths, res, bailError);
            }
            res = classLoader.getResource("/" + packageName);
            if (res != null) {
                res = removePackageFromUrl(res, packageName, bailError);
                if (res != null) {
                    addFromUrl(jarUrls, directoryPaths, res, bailError);
                }
            }
            res = classLoader.getResource(packageName);
            if (res != null) {
                res = removePackageFromUrl(res, packageName, bailError);
                if (res != null) {
                    addFromUrl(jarUrls, directoryPaths, res, bailError);
                }
            }
            addFromUnknownClass(classLoader, jarUrls, directoryPaths, bailError, 8);
        }
        classLoader = classLoader.getParent();
    }
}

private static URL removePackageFromUrl(URL res, String packagePath, boolean bailError) {
    packagePath = "/" + packagePath;
    String urlStr = res.toString();
    if ( !urlStr.endsWith(packagePath)) {
        if (bailError) {
            throw new RuntimeException("the url string does not end with the packagepath! packagePath='" + packagePath + "' urlStr='" + urlStr + "'");
        } else {
            return null;
        }
    }
    urlStr = urlStr.substring(0, urlStr.length() - packagePath.length());
    if (urlStr.endsWith("!")) {
        urlStr = urlStr.substring(0, urlStr.length() - 1);
    }
    if (urlStr.startsWith("jar:")) {
        urlStr = urlStr.substring(4);
    }
    try {
        return new URL(urlStr);
    } catch (MalformedURLException e) {
        if (bailError) {
            throw new RuntimeException(e);
        } else {
            return null;
        }
    }
}

private static void addFromUnknownClass(Object instance, Set <URL> jarUrls, Set <Path> directoryPaths, boolean bailError, int maxDeep) {
    Class <?> cls = instance.getClass();
    while (cls != null) {
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            Class <?> type = field.getType();
            Object value;
            try {
                value = getValue(instance, field);
                if (value != null) {
                    addFromUnknownValue(value, jarUrls, directoryPaths, bailError, type, field.getName(), maxDeep - 1);
                }
            } catch (IllegalArgumentException | IllegalAccessException | SecurityException e) {
                if (bailError) {
                    final String version = System.getProperty("java.version");
                    String vers = version;
                    if (vers.startsWith("1.")) {
                        vers = vers.substring(2);
                    }
                    int dotindex = vers.indexOf('.');
                    if (dotindex != -1) {
                        vers = vers.substring(0, dotindex);
                    }
                    int versNum;
                    try {
                        versNum = Integer.parseInt(vers);
                    } catch (NumberFormatException e1) {
                        throw new RuntimeException("illegal version: '" + version + "' lastError: " + e.getMessage(), e);
                    }
                    if (versNum <= 11) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        cls = cls.getSuperclass();
    }
    
}

private static Object getValue(Object instance, Field field) throws IllegalArgumentException, IllegalAccessException, SecurityException {
    try {
        boolean flag = field.isAccessible();
        boolean newflag = flag;
        try {
            field.setAccessible(true);
            newflag = true;
        } catch (Exception e) {}
        try {
            return field.get(instance);
        } finally {
            if (flag != newflag) {
                field.setAccessible(flag);
            }
        }
    } catch (IllegalArgumentException | IllegalAccessException | SecurityException e) {
        try {
            Field override = AccessibleObject.class.getDeclaredField("override");
            boolean flag = override.isAccessible();
            boolean newFlag = flag;
            try {
                override.setAccessible(true);
                flag = true;
            } catch (Exception s) {}
            override.setBoolean(field, true);
            if (flag != newFlag) {
                override.setAccessible(flag);
            }
            return field.get(instance);
        } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e1) {
            e.addSuppressed(e1);
            throw e;
        }
    }
}

private static void addFromUnknownValue(Object value, Set <URL> jarUrls, Set <Path> directoryPaths, boolean bailError, Class <?> type, String fieldName, int maxDeep) {
    if (Collection.class.isAssignableFrom(type)) {
        for (Object obj : (Collection <?>) value) {
            URL url = null;
            try {
                if (obj instanceof URL) {
                    url = (URL) obj;
                } else if (obj instanceof Path) {
                    url = ((Path) obj).toUri().toURL();
                } else if (obj instanceof File) {
                    url = ((File) obj).toURI().toURL();
                }
            } catch (MalformedURLException e) {
                if (bailError) {
                    throw new RuntimeException(e);
                }
            }
            if (url != null) {
                addFromUrl(jarUrls, directoryPaths, url, bailError);
            }
        }
    } else if (URL[].class.isAssignableFrom(type)) {
        for (URL url : (URL[]) value) {
            addFromUrl(jarUrls, directoryPaths, url, bailError);
        }
    } else if (Path[].class.isAssignableFrom(type)) {
        for (Path path : (Path[]) value) {
            try {
                addFromUrl(jarUrls, directoryPaths, path.toUri().toURL(), bailError);
            } catch (MalformedURLException e) {
                if (bailError) {
                    throw new RuntimeException(e);
                }
            }
        }
    } else if (File[].class.isAssignableFrom(type)) {
        for (File file : (File[]) value) {
            try {
                addFromUrl(jarUrls, directoryPaths, file.toURI().toURL(), bailError);
            } catch (MalformedURLException e) {
                if (bailError) {
                    throw new RuntimeException(e);
                }
            }
        }
    } else if (maxDeep > 0) {
        addFromUnknownClass(value, jarUrls, directoryPaths, bailError, maxDeep - 1);
    }
}

private static void addFromUrl(Set <URL> jarUrls, Set <Path> directoryPaths, URL url, boolean bailError) {
    if (url.getFile().endsWith(".jar") || url.getFile().endsWith(".zip")) {
        // may want better way to detect jar files
        jarUrls.add(url);
    } else {
        try {
            Path path = Paths.get(url.toURI());
            if (Files.isDirectory(path)) {
                directoryPaths.add(path);
            } else if (bailError) {
                throw new RuntimeException("unknown url for class loading: " + url);
            }
        } catch (URISyntaxException e) {
            if (bailError) {
                throw new RuntimeException(e);
            }
        }
    }
}

进口:

import java.io.File;
import java.io.IOException;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.DirectoryStream;
import java.nio.file.DirectoryStream.Filter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.Attributes.Name;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

几乎所有的答案要么使用reflection,要么从文件系统读取类文件。如果尝试从文件系统读取类,则在将应用程序打包为JAR或其他格式时可能会出错。此外,您可能不想为此目的使用单独的库。

这里有另一种方法,它是纯java,不依赖于文件系统。

import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class PackageUtil {

    public static Collection<Class> getClasses(final String pack) throws Exception {
        final StandardJavaFileManager fileManager = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
        return StreamSupport.stream(fileManager.list(StandardLocation.CLASS_PATH, pack, Collections.singleton(JavaFileObject.Kind.CLASS), false).spliterator(), false)
                .map(javaFileObject -> {
                    try {
                        final String[] split = javaFileObject.getName()
                                .replace(".class", "")
                                .replace(")", "")
                                .split(Pattern.quote(File.separator));

                        final String fullClassName = pack + "." + split[split.length - 1];
                        return Class.forName(fullClassName);
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                    }

                })
                .collect(Collectors.toCollection(ArrayList::new));
    }
}

Java 8不是必须的。你可以使用for循环代替流。 你可以这样测试

public static void main(String[] args) throws Exception {
    final String pack = "java.nio.file"; // Or any other package
    PackageUtil.getClasses(pack).stream().forEach(System.out::println);
}

我无法为如此简单的东西找到一个简短的工作片段。所以在这里,我自己做了一段时间后:

Reflections reflections =
    new Reflections(new ConfigurationBuilder()
            .filterInputsBy(new FilterBuilder().includePackage(packagePath))
            .setUrls(ClasspathHelper.forPackage(packagePath))
            .setScanners(new SubTypesScanner(false)));

Set<String> typeList = reflections.getAllTypes();

它使用组织反射。