我根据您的描述对其进行了测试,并按预期工作:
单独的类路径:
package com.dummy
import sub.types.test.B
import sub.types.test.C
class D : B(){}
class E : C(){}
class F : B(){}
密封类和测试:
package sub.types.test
import org.reflections.Reflections
import org.reflections.scanners.SubTypesScanner
sealed class A
open class B(): A()
open class C(): A()
fun main() {
val ref = Reflections("com.dummy", SubTypesScanner(false))
var clazzes: Set<Class<*>> = ref.getSubTypesOf(A::class.java)
println("A subclasses")
println(clazzes)
clazzes = ref.getSubTypesOf(B::class.java)
println("B subclasses")
println(clazzes)
}
控制台输出:
12:38:19.102 [main] INFO org.reflections.Reflections - Reflections took 64 ms to scan 1 urls, producing 2 keys and 3 values
12:38:19.104 [main] DEBUG org.reflections.Reflections - expanded subtype sub.types.test.A -> sub.types.test.C
12:38:19.104 [main] DEBUG org.reflections.Reflections - expanded subtype sub.types.test.A -> sub.types.test.B
A subclasses
[class sub.types.test.C, class com.dummy.D, class com.dummy.E, class sub.types.test.B, class com.dummy.F]
B subclasses
[class com.dummy.D, class com.dummy.F]
它不是 0 个类,而是所有继承类。我希望这能解决一些问题。