如何获取包中的所有类名? [英] How to get all classes names in a package?

查看:113
本文介绍了如何获取包中的所有类名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个包含扩展JPanel的类的包,我想将它们作为选项卡动态添加。一开始我使用了一个工厂,我注册了所有的类,它的工作,但现在我想加载包中的所有类,而不知道他们的名字。我已经尝试过几个项目,包括反思图书馆(我觉得很混乱),我不能'让他们上班我感谢任何帮助。

So I have a package that has classes that extend JPanel and I want to add them as tabs dynamically. At the beginning I used a factory and I registered all the classes in it and it worked, but now I want load all the classes in the package without knowing their names. I've tried several things including Reflections library (which I found very confusing) and I couldn't get them to work. I appreciate any help.

这是我的一个尝试:

public static void registerTab() {
    String pkg = TabA.class.getPackage().getName();
    String relPath = pkg.replace('.', '/');

    URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
    if (resource == null) {
        throw new RuntimeException("Unexpected problem: No resource for "
                + relPath);
    }

    File f = new File(resource.getPath());

    String[] files = f.list();

    for (int i = 0; i < files.length; i++) {

        String fileName = files[i];
        String className = null;
        String fileNm = null;

        if (fileName.endsWith(".class")) {

            fileNm = fileName.substring(0, fileName.length() - 6);
            className = pkg + '.' + fileNm;
        }

        if (className != null) {

            if (!tabClasses.containsKey(className))
                tabClasses.put(fileNm, className);
        }
    }
}


推荐答案

这是一个自定义解决方案,我开发了一个软件包的所有类:

Here is a custom solution I developed to find all the classes of a package:

public class ClassFinder {

    private static final char PKG_SEPARATOR = '.';

    private static final char DIR_SEPARATOR = '/';

    private static final String CLASS_FILE_SUFFIX = ".class";

    private static final String BAD_PACKAGE_ERROR = "Unable to get resources from path '%s'. Are you sure the package '%s' exists?";

    public static List<Class<?>> find(String scannedPackage) {
        String scannedPath = scannedPackage.replace(PKG_SEPARATOR, DIR_SEPARATOR);
        URL scannedUrl = Thread.currentThread().getContextClassLoader().getResource(scannedPath);
        if (scannedUrl == null) {
            throw new IllegalArgumentException(String.format(BAD_PACKAGE_ERROR, scannedPath, scannedPackage));
        }
        File scannedDir = new File(scannedUrl.getFile());
        List<Class<?>> classes = new ArrayList<Class<?>>();
        for (File file : scannedDir.listFiles()) {
            classes.addAll(find(file, scannedPackage));
        }
        return classes;
    }

    private static List<Class<?>> find(File file, String scannedPackage) {
        List<Class<?>> classes = new ArrayList<Class<?>>();
        String resource = scannedPackage + PKG_SEPARATOR + file.getName();
        if (file.isDirectory()) {
            for (File child : file.listFiles()) {
                classes.addAll(find(child, resource));
            }
        } else if (resource.endsWith(CLASS_FILE_SUFFIX)) {
            int endIndex = resource.length() - CLASS_FILE_SUFFIX.length();
            String className = resource.substring(0, endIndex);
            try {
                classes.add(Class.forName(className));
            } catch (ClassNotFoundException ignore) {
            }
        }
        return classes;
    }

}

然后,只需使用: p>

Then, just use:

List<Class<?>> classes = ClassFinder.find("com.package");

这篇关于如何获取包中的所有类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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