Is it possible to find all kotlin classes in a given package?

I also need only annotated classes but it's not a big deal. Any suggestions ?

推荐答案

由于类加载器的实现,JVM上的Kotlin在这方面与Java遇到了相同的问题.

Class loaders are not required to tell the VM which classes it can provide, instead they are just handed requests for classes, and have to return a class or throw an exception.

来源及更多信息:Can you find all classes in a package using reflection?

为了总结链接线程,有许多解决方案允许您判断当前的类路径.

  • Reflections库非常简单,有很多附加功能,比如获取类的所有子类型,get all types/members annotated with some annotation, optionally with annotation parameters matching等等.
  • GuavaClassPath个,返回ClassInfo个POJO,这对于你的用例来说还不够,但是知道番石榴几乎在任何地方都是有用的.
  • Write your own by querying classloader resources and code sources. Would not suggest this route unless you absolutely cannot add library dependencies.

Kotlin相关问答推荐

相当于roomdb中的DateTime Bigint列的是什么

如何在Spring Boot中注册新的集合工厂

在 Kotlin 中将两个字节转换为 UIn16

在 detekt 配置文件中找不到某些属性

如何访问嵌套在另一个 map 中的 map 中的值(在 kotlin 中)

如何使用子变量在 Kotlin 中初始化父级

如何在 Hibernate Panache 中进行部分搜索

为什么我们在 kotlin 中需要 noinline?

Kotlin:内部类如何访问在外部类中声明为参数的变量?

Kotlin 代码是如何编译成原生代码的?

如何将超过 2 个 api 调用的结果与 Coroutines Flow 结合起来?

如何从不同的功能发出流量值? Kotlin 协程

如何处理 Kotlin 中的异常?

Kotlin:什么是 kotlin.String!类型

Android Studio 和 Kotlin:Unresolved reference: also

如何使用kotlin中的反射查找包中的所有类

Kapt不适用于Android Studio 3.0中的AutoValue

Kotlin使用运行时断言进行空判断?

添加抽象的私有getter和公共setter的正确方法是什么?

如何为 Java 调用者声明返回类型为void的 Kotlin Lambda?