如何枚举包中的所有类并将它们添加到List? [英] How can I enumerate all classes in a package and add them to a List?

查看:382
本文介绍了如何枚举包中的所有类并将它们添加到List?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要枚举包中的所有类并将它们添加到List中。单个类的非动态版本如下:

I need to enumerate all classes in a package and add them to a List. The non-dynamic version for a single class goes like this:

List allClasses = new ArrayList();
allClasses.add(String.class);

如何动态地将所有类添加到包及其所有子包中?

How can I do this dynamically to add all classes in a package and all its subpackages?

更新: 阅读完早期答案后,我绝对是真的我试图解决另一个次要问题,所以让我说明一下。我知道这是可能的,因为其他工具可以做到这一点。查看新问题这里

更新: 再次阅读本文,我可以看到它是如何被误读的。我想在编译后从文件系统中枚举所有MY PROJECT'S类。

Update: Reading this again, I can see how it's being misread. I'm looking to enumerate all of MY PROJECT'S classes from the file system after compilation.

推荐答案

****更新1(2012)****

****UPDATE 1 (2012)****

好的,我终于开始清理下面的代码片段了。我坚持使用它自己的github项目甚至添加了测试。

OK, I've finally gotten around to cleaning up the code snippet below. I stuck it into it's own github project and even added tests.

https://github.com/ddopson/java-class-enumerator

****更新2(2016)** **

****UPDATE 2 (2016)****

对于更强大且功能丰富的类路径扫描程序,请参阅 https://github.com/lukehutch/fast-classpath-scanner/wiki 。我建议先阅读我的代码片段以获得高级别的理解,然后使用lukehutch的工具进行制作。

For an even more robust and feature-rich classpath scanner, see https://github.com/lukehutch/fast-classpath-scanner/wiki. I'd recommend first reading my code snippet to gain a high level understanding, then using lukehutch's tool for production purposes.

**** Original Post(2010)** **

****Original Post (2010)****

严格来说,无法列出包中的类。这是因为包实际上只是一个命名空间(例如com.epicapplications.foo.bar),并且类路径中的任何jar文件都可能将类添加到包中。更糟糕的是,类加载器将按需加载类,并且类路径的一部分可能位于网络连接的另一端。

Strictly speaking, it isn't possible to list the classes in a package. This is because a package is really nothing more than a namespace (eg com.epicapplications.foo.bar), and any jar-file in the classpath could potentially add classes into a package. Even worse, the classloader will load classes on demand, and part of the classpath might be on the other side of a network connection.

可以解决更严格的问题问题。例如,JAR文件中的所有类,或JAR文件在特定包中定义的所有类。无论如何,这是更常见的情况。

It is possible to solve a more restrictive problem. eg, all classes in a JAR file, or all classes that a JAR file defines within a particular package. This is the more common scenario anyways.

不幸的是,没有任何框架代码可以轻松完成此任务。您必须以类似于ClassLoader查找类定义的方式扫描文件系统。

Unfortunately, there isn't any framework code to make this task easy. You have to scan the filesystem in a manner similar to how the ClassLoader would look for class definitions.

Web上有大量样本用于类文件旧目录。这些天我们大多数人都使用JAR文件。

There are a lot of samples on the web for class files in plain-old-directories. Most of us these days work with JAR files.

为了解决JAR文件问题,试试这个......

To get things working with JAR files, try this...

private static ArrayList<Class<?>> getClassesForPackage(Package pkg) {
    String pkgname = pkg.getName();
    ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
    // Get a File object for the package
    File directory = null;
    String fullPath;
    String relPath = pkgname.replace('.', '/');
    System.out.println("ClassDiscovery: Package: " + pkgname + " becomes Path:" + relPath);
    URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
    System.out.println("ClassDiscovery: Resource = " + resource);
    if (resource == null) {
        throw new RuntimeException("No resource for " + relPath);
    }
    fullPath = resource.getFile();
    System.out.println("ClassDiscovery: FullPath = " + resource);

    try {
        directory = new File(resource.toURI());
    } catch (URISyntaxException e) {
        throw new RuntimeException(pkgname + " (" + resource + ") does not appear to be a valid URL / URI.  Strange, since we got it from the system...", e);
    } catch (IllegalArgumentException e) {
        directory = null;
    }
    System.out.println("ClassDiscovery: Directory = " + directory);

    if (directory != null && directory.exists()) {
        // Get the list of the files contained in the package
        String[] files = directory.list();
        for (int i = 0; i < files.length; i++) {
            // we are only interested in .class files
            if (files[i].endsWith(".class")) {
                // removes the .class extension
                String className = pkgname + '.' + files[i].substring(0, files[i].length() - 6);
                System.out.println("ClassDiscovery: className = " + className);
                try {
                    classes.add(Class.forName(className));
                } 
                catch (ClassNotFoundException e) {
                    throw new RuntimeException("ClassNotFoundException loading " + className);
                }
            }
        }
    }
    else {
        try {
            String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
            JarFile jarFile = new JarFile(jarPath);         
            Enumeration<JarEntry> entries = jarFile.entries();
            while(entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                String entryName = entry.getName();
                if(entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) {
                    System.out.println("ClassDiscovery: JarEntry: " + entryName);
                    String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
                    System.out.println("ClassDiscovery: className = " + className);
                    try {
                        classes.add(Class.forName(className));
                    } 
                    catch (ClassNotFoundException e) {
                        throw new RuntimeException("ClassNotFoundException loading " + className);
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
        }
    }
    return classes;
}

这篇关于如何枚举包中的所有类并将它们添加到List?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆