【问题标题】:How I can get list of all classes from given bundle我如何从给定的包中获取所有类的列表
【发布时间】:2014-03-27 13:23:16
【问题描述】:

我有一个 Eclipse 包,想从这个包中获取所有类的列表。

我该怎么做?

【问题讨论】:

  • 来自 JUnit 插件测试,例如(不是来自 Eclipse GUI 界面)。

标签: eclipse osgi equinox


【解决方案1】:

bundle 中的类的名称将在最后的 classNameOfCurrentBundle 列表中:

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转换为类类型:

bundle.loadClass(className);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-17
  • 2018-10-23
  • 2011-08-23
  • 1970-01-01
  • 2011-11-14
  • 2021-08-08
相关资源
最近更新 更多