如何从给定包中获取所有类的列表 [英] How I can get list of all classes from given bundle

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

问题描述

我有一个Eclipse捆绑软件,想从该捆绑软件中获取所有类的列表.

I have an Eclipse bundle and want to get a list of all classes from this bundle.

我该怎么做?

推荐答案

捆绑软件中的类的名称将显示在最后的classNameOfCurrentBundle列表中:

The name of the classes from the bundle will be in classNameOfCurrentBundle list in the end:

BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
// Getting all the class files (also from imported packages)
Collection<String> resources = bundleWiring.listResources("/", "*.class", BundleWiring.LISTRESOURCES_RECURSE);

List<String> classNamesOfCurrentBundle = new ArrayList<String>();
for (String resource : resources) {
    URL localResource = bundle.getEntry(resource);
    // Bundle.getEntry() returns null if the resource is not located in the specific bundle
    if (localResource != null) {
        String className = resource.replaceAll("/", ".");
        classNamesOfCurrentBundle.add(className);
    }
}

将className转换为类类型:

Converting the className to class type:

bundle.loadClass(className);

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

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