英文:
Unable to fetch classes of a package using Reflection
问题
[![在上面的图片中][1]][1]
[1]: https://i.stack.imgur.com/TnqIJ.jpg
从上面的图片中,当我尝试获取“tests”下的所有类时,我得到了一个空数组。
Reflections reflections = new Reflections("tests");
//Reflections reflections = new Reflections("src.test.java.tests"); 尝试过,但数组仍为空
Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Object.class); //在这里得到了空数组
Class[] arrayCls = new Class[allClasses.size()];
allClasses.toArray(arrayCls);
有人可以帮忙告诉我如何获取类的数组吗?
英文:
From the above image, when i am trying to fetch all classes under "tests", i am getting empty array.
Reflections reflections = new Reflections("tests");
//Reflections reflections = new Reflections("src.test.java.tests"); Tried but still empty array
Set<Class<? extends Object>> allClasses = reflections.getSubTypesOf(Object.class); //Getting empty array here
Class[] arrayCls=new Class[allClasses.size()];
allClasses.toArray(arrayCls);
Can someone please help me how to get array of classes?
答案1
得分: 1
这个解决方案对我有效,我在 src 文件夹下有一个名为 testing 的包,其中直接包含类和包含类的子包,它返回一个包含相应类的正确集合。Reflections 构造函数的第一个参数必须是你的包名,末尾附加一个 '.':
Reflections reflections = new Reflections("testing.", new SubTypesScanner(false));
Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);
不要忘记将相应的 JAR 文件添加到你的项目中:
annotations-2.0.1.jar、guava-15.0、javassist-3.19.0-GA、reflections-0.9.10,你可以从这里下载它们:https://jar-download.com/artifacts/org.reflections/reflections/0.9.10/source-code。
更新:
这将在 tests 包下起作用。它基于 Guava 14 中的 ClassPath 类。这里使用 TestNGRunner 类加载器来获取放置在与 TestNGRunner 相同的 src 包(src/test/java)下的类。检查是通过类的完整名称(包名 + 类名)来进行的,这就是为什么我使用 startsWith("tests."):
final ClassLoader loader = TestNGRunner.class.getClassLoader();
Set<Class<? extends Object>> allClasses = new HashSet<Class<? extends Object>>();
for (final ClassPath.ClassInfo info : ClassPath.from(loader).getTopLevelClasses()) {
if (info.getName().startsWith("tests.")) {
allClasses.add(info.load());
}
}
英文:
This solution worked for me, i have a packed named testing under src file which contain directly classes and sub packages with classes and it return to correct set containg the corresponding classes. The first parameter of Reflections constructor must be your packageName appended with a '.' in the end :
Reflections reflections = new Reflections("testing.", new SubTypesScanner(false));
Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);
Don't forget to add the corresponding jars to your project :
annotations-2.0.1.jar,guava-15.0,javassist-3.19.0-GA,reflections-0.9.10 which you can download them from here https://jar-download.com/artifacts/org.reflections/reflections/0.9.10/source-code.
Update:
This will work under tests package. It is based on ClassPath class from Guava 14. The TestNGRunner class loader is used here to fetch the classes placed under the same src package of TestNGRunner which is the src/test/java. The check is done the full name of the class (packageName+.+className) that's why i used startsWith("tests.") :
final ClassLoader loader = TestNGRunner.class.getClassLoader();
Set<Class<? extends Object>> allClasses = new HashSet<Class<? extends Object>>();
for (final ClassPath.ClassInfo info : ClassPath.from(loader).getTopLevelClasses()) {
if (info.getName().startsWith("tests.")) {
allClasses.add(info.load());
}
}
答案2
得分: 0
SubTypesScanner已经被弃用。您可以改用以下方式:
var reflections = new Reflections(packageToScan, SubTypes.filterResultsBy(s -> true));
英文:
SubTypesScanner is now depreciated. You can use this instead:
var reflections = new Reflections(packageToScan, SubTypes.filterResultsBy(s -> true));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论