【问题标题】:Retrieve a list of all packages in a project with Google reflections library?使用 Google 反射库检索项目中所有包的列表?
【发布时间】:2013-06-25 05:10:00
【问题描述】:

我说的是reflections 库。是否有可能获得我让代码编译的项目中包含的所有包的列表?

我已尝试使用以下代码括号,但我不想插入项目名称。

Reflections reflections = new Reflections(/* Project name here... */);

     Set<Class<? extends Object>> allClasses = 
         reflections.getSubTypesOf(Object.class);

【问题讨论】:

标签: java packages reflections


【解决方案1】:

不使用 ronmamo/reflections 而是使用 Google Guava's ClassPath,可以执行以下操作:

Set<ClassInfo> classInfos = ClassPath.from(Main.class.getClassLoader()).getTopLevelClassesRecursive("org.spongepowered");
List<String> packageNames = classInfos.stream().map(classInfo -> classInfo.getPackageName()).distinct().collect(Collectors.toList());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2013-07-23
    • 2012-08-21
    • 1970-01-01
    相关资源
    最近更新 更多