Skip to content

Commit 9daa1bb

Browse files
committed
modify PuppetManager use reflections
1 parent fd8e812 commit 9daa1bb

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

wechaty/pom.xml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@
6363
<!-- <groupId>org.apache.logging.log4j</groupId>-->
6464
<!-- <artifactId>log4j-api</artifactId>-->
6565
<!-- </dependency>-->
66-
<!-- <dependency>-->
67-
<!-- <groupId>org.apache.logging.log4j</groupId>-->
68-
<!-- <artifactId>log4j-core</artifactId>-->
69-
<!-- </dependency>-->
70-
<!-- <dependency>-->
71-
<!-- <groupId>org.apache.logging.log4j</groupId>-->
72-
<!-- <artifactId>log4j-slf4j-impl</artifactId>-->
73-
<!-- </dependency>-->
66+
<dependency>
67+
<groupId>org.apache.logging.log4j</groupId>
68+
<artifactId>log4j-core</artifactId>
69+
<scope>test</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.apache.logging.log4j</groupId>
73+
<artifactId>log4j-slf4j-impl</artifactId>
74+
<scope>test</scope>
75+
</dependency>
7476

7577
<!-- https://mvnrepository.com/artifact/com.lmax/disruptor -->
7678
<!-- <dependency>-->
@@ -110,6 +112,11 @@
110112
<artifactId>junit</artifactId>
111113
<scope>test</scope>
112114
</dependency>
115+
<dependency>
116+
<groupId>org.reflections</groupId>
117+
<artifactId>reflections</artifactId>
118+
<version>0.9.12</version>
119+
</dependency>
113120

114121
</dependencies>
115122

wechaty/src/main/kotlin/io/github/wechaty/WechatyOptions.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class WechatyOptions {
99

1010
var name:String = "Wechaty"
1111

12-
var puppet:String = "wechaty-puppet-hostie"
12+
// var puppet:String = "wechaty-puppet-hostie"
13+
var puppet:String = "io.github.wechaty.grpc.GrpcPuppet"
1314

1415
var puppetOptions:PuppetOptions? = null
1516

wechaty/src/main/kotlin/io/github/wechaty/user/manager/PuppetManager.kt

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import io.github.wechaty.MockPuppet
44
import io.github.wechaty.Puppet
55
import io.github.wechaty.WechatyOptions
66
import io.github.wechaty.grpc.GrpcPuppet
7+
import io.github.wechaty.schemas.PuppetOptions
78
import io.github.wechaty.utils.JsonUtils
9+
import org.reflections.Reflections
810
import org.slf4j.LoggerFactory
911
import java.util.concurrent.CompletableFuture
1012
import java.util.concurrent.Future
1113

14+
const val REFLECTION_BASE_PACKAGE = "io.github.wechaty"
15+
1216
class 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+

wechaty/src/test/kotlin/io/github/wechaty/user/RoomTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class RoomTest {
3636
puppet = MockPuppet(PuppetOptions())
3737
val wechatyOptions = WechatyOptions()
3838
wechatyOptions.name = "MockWechaty"
39-
wechatyOptions.puppet = "wechaty-puppet-mock"
39+
wechatyOptions.puppet = "io.github.wechaty.MockPuppet"
4040
wechatyOptions.puppetOptions = PuppetOptions()
4141
wechaty = Wechaty.instance(wechatyOptions)
4242
wechaty.start()

0 commit comments

Comments
 (0)