1
1
package runtime.reflect
2
2
3
- import runtime.wrappers.*
4
- import kotlin.reflect.*
3
+ import kotlin.reflect.KClass
5
4
6
5
fun <T : Any > KClass<T>.createInstance (): T {
7
6
@Suppress(" UNUSED_VARIABLE" )
@@ -10,101 +9,3 @@ fun <T : Any> KClass<T>.createInstance(): T {
10
9
return js(" new ctor()" )
11
10
}
12
11
13
- fun <T : Any > KClass<T>.createInstance (arg : Any ): T {
14
- @Suppress(" UNUSED_VARIABLE" )
15
- val ctor = this .js
16
-
17
- @Suppress(" UNUSED_VARIABLE" )
18
- val arg1 = arg
19
- return js(" new ctor(arg1)" )
20
- }
21
-
22
- fun KClass <* >.jsMetadata (): JsClassMetadata {
23
- return JsReflection .getClassMetadata(this )
24
- }
25
-
26
- val KClass <* >.jsFullClassName
27
- get() = if (jsMetadata().packageName != " " ) jsMetadata().packageName + " ." + simpleName!! else simpleName!!
28
-
29
- val KClass <* >.jsPackageName
30
- get() = jsMetadata().packageName
31
-
32
- fun KClass <* >.jsFindSubclasses (): Array <KClass <* >> {
33
- return JsReflection .findSubclasses(this )
34
- }
35
-
36
- fun <T > KClass <* >.jsFindCompanion (): T ? {
37
- val comp = js.asDynamic().Companion
38
- if (comp is T ) {
39
- return comp
40
- } else {
41
- return null
42
- }
43
- }
44
-
45
- class JsClassMetadata (val clazz : KClass <* >, val module : Any , val packageName : String , val kMetadata : KMetadata )
46
-
47
- object JsReflection {
48
-
49
- private val moduleClasses = HashMap <Any , Array <JsClassMetadata >>()
50
- private val classMetadata = HashMap <KClass <* >, JsClassMetadata > ()
51
-
52
- fun enrichReflection (module : Any ) {
53
- val classes = findAllClasses(" " , module)
54
- moduleClasses.put(module, classes)
55
- for (i in classes) {
56
- classMetadata[i.clazz] = i
57
- }
58
- }
59
-
60
- internal fun getClassMetadata (clazz : KClass <* >): JsClassMetadata {
61
- return classMetadata[clazz] ? : throw RuntimeException (" Class ${clazz.simpleName} is not enriched. Did you call enrichReflection on required module?" )
62
- }
63
-
64
- internal fun findSubclasses (base : KClass <* >): Array <KClass <* >> {
65
- return moduleClasses.values.flatMap {
66
- it.filter {
67
- it.kMetadata.interfaces.any { it == base.js }
68
- }.map { it.clazz }
69
- }.toTypedArray()
70
- }
71
-
72
- private fun findAllClasses (pkg : String , module : Any ): Array <JsClassMetadata > {
73
- val res = mutableListOf<JsClassMetadata >()
74
- module.getOwnPropertyNames().forEach {
75
-
76
- val propName = it
77
- // indexed properties
78
- if (propName.all { it in ' 0' .. ' 9' })
79
- return @forEach
80
- // closure functions
81
- if (propName.contains(' $' ))
82
- return @forEach
83
- if (propName == " prototype" )
84
- return @forEach
85
- if (propName == " constructor" )
86
- return @forEach
87
-
88
- // do not try to evaluate computable properties
89
- val descriptor = Object .getOwnPropertyDescriptor(module, propName).asDynamic()
90
- if (descriptor.value != null ) {
91
- val propValue = module.asDynamic()[propName]
92
- if (propValue != null ) {
93
- if (propValue[KotlinCompiler .metadata] != null ) {
94
- // Kotlin Class
95
- @Suppress(" UNCHECKED_CAST_TO_NATIVE_INTERFACE" )
96
- res.add(JsClassMetadata ((propValue as JsClass <* >).kotlin, module, pkg, propValue[KotlinCompiler .metadata]))
97
- } else {
98
- // Otherwise this is a new package
99
- if (js(" propValue instanceof Function" ))
100
- return @forEach
101
- val pkgPlus = if (pkg == " " ) it else " ${pkg} .${it} "
102
- res.addAll(findAllClasses(pkgPlus, propValue))
103
- }
104
- }
105
- }
106
- }
107
-
108
- return res.toTypedArray()
109
- }
110
- }
0 commit comments