问题描述
我有一个包裹
包一.二.三
在那个包里我有
public trait first
public class Second
和
pubic class Third
我想将所有类和接口(在一、二、三中)名称作为字符串.听说最好的方法是使用反射.
I want to get all the classes and interface(in one.two.three) names as Strings.heard that the best way to do it is using reflection.
谁能告诉我一种方法来做那件事.
can someone tell me a way to do that thing please.
推荐答案
嗯,这比人们想象的要复杂一些.
Well, that is a bit more complex task than one would thought.
例如,您可能在同一个包中但在不同位置拥有类(例如,由 UrlClassLoader
加载的 2 个 JAR 文件可能包含一个包 abc
).
For instance, you may have classes in the same package but in different locations (e.g., 2 JAR files loaded by an UrlClassLoader
may contain a package a.b.c
).
最好的方法是使用现有的工具和库,例如 Reflections API例如(我还不知道 Scala 包装器或替代品).它们为此类查询提供了强大且经过良好测试的界面.
The best way is to use already existent tools and libraries, such as the Reflections API for instance (I'm not aware of a Scala wrapper or alternative yet). They offer a robust and well tested interface for such queries.
这篇关于使用scala获取包中的所有类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!