使用scala获取包中的所有类名 [英] get all the class names in a package with scala

查看:43
本文介绍了使用scala获取包中的所有类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包裹

包一.二.三

在那个包里我有

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获取包中的所有类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆