get all the class names in a package with scala

旧巷老猫 提交于 2021-02-07 14:13:29

问题


I have a package

package one.two.three

and in that package I have

public trait first

public class Second and

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.


回答1:


Well, that is a bit more complex task than one would thought.

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).

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.



来源:https://stackoverflow.com/questions/21015700/get-all-the-class-names-in-a-package-with-scala

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!