@@ -4,11 +4,15 @@ import io.github.wechaty.MockPuppet
44import io.github.wechaty.Puppet
55import io.github.wechaty.WechatyOptions
66import io.github.wechaty.grpc.GrpcPuppet
7+ import io.github.wechaty.schemas.PuppetOptions
78import io.github.wechaty.utils.JsonUtils
9+ import org.reflections.Reflections
810import org.slf4j.LoggerFactory
911import java.util.concurrent.CompletableFuture
1012import java.util.concurrent.Future
1113
14+ const val REFLECTION_BASE_PACKAGE = " io.github.wechaty"
15+
1216class PuppetManager {
1317
1418 companion object {
@@ -18,13 +22,48 @@ class PuppetManager {
1822 fun resolveInstance (wechatyOptions : WechatyOptions ): Future <Puppet > {
1923 log.info(" PuppetManager resolveInstance(${JsonUtils .write(wechatyOptions)} )" )
2024
21- return if (" wechaty-puppet-hostie" == wechatyOptions.puppet) {
22- CompletableFuture .completedFuture(GrpcPuppet (wechatyOptions.puppetOptions!! ))
23- } else {
24- CompletableFuture .completedFuture(MockPuppet (wechatyOptions.puppetOptions!! ))
25+ // return if ("io.github.wechaty.grpc.GrpcPuppet" == wechatyOptions.puppet) {
26+ // CompletableFuture.completedFuture(GrpcPuppet(wechatyOptions.puppetOptions!!))
27+ // } else {
28+ // CompletableFuture.completedFuture(MockPuppet(wechatyOptions.puppetOptions!!))
29+ // }
30+ val reflections = Reflections (REFLECTION_BASE_PACKAGE )
31+ val subTypes: Set <* > = reflections.getSubTypesOf(Puppet ::class .java)
32+
33+ for (subType in subTypes) {
34+ val subTypeClass = subType as Class <* >
35+ if (wechatyOptions.puppet == subTypeClass.canonicalName) {
36+ val declaredConstructor = subTypeClass.getDeclaredConstructor(PuppetOptions ::class .java)
37+ return CompletableFuture .completedFuture(declaredConstructor.newInstance(wechatyOptions.puppetOptions!! ) as Puppet )
38+ }
2539 }
40+ throw RuntimeException (" instant puppet implementation error. Please check your wechatyOptions.puppet" )
2641 }
2742 }
2843
2944
3045}
46+
47+ fun main () {
48+ // val serviceLoader:ServiceLoader<Puppet> = ServiceLoader.load(Puppet::class.java)
49+ // for (puppet in serviceLoader) {
50+ // println(puppet)
51+ // }
52+
53+ // val kClass:KClass<Puppet> = Puppet::class
54+ // print(kClass)
55+ val reflections: Reflections = Reflections (" io.github.wechaty" )
56+ val subTypes: Set <* > = reflections.getSubTypesOf(Puppet ::class .java)
57+ for (subType in subTypes) {
58+ val subTypeClass = subType as Class <* >
59+ if (" io.github.wechaty.grpc.GrpcPuppet" == subTypeClass.canonicalName) {
60+
61+ val declaredConstructor = subTypeClass.getDeclaredConstructor(PuppetOptions ::class .java)
62+
63+ val newInstance = declaredConstructor.newInstance(PuppetOptions ())
64+
65+ }
66+ }
67+
68+ }
69+
0 commit comments