Skip to content

Commit

Permalink
增加字符串支持
Browse files Browse the repository at this point in the history
  • Loading branch information
ltttttttttttt committed Aug 15, 2023
1 parent 8630f30 commit eb442a7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ ksp {
use

```kotlin
//Constructing Objects Using Non parametric Constructors
KClass.newInstance()
//or
//Constructing Objects Using Parametric Constructors
KClass.newInstance(parameters...)
//By string method
VirtualReflectionUtil.newInstance("com.lt.virtual_reflection.bean.A")
```
2 changes: 2 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,6 @@ ksp {
KClass.newInstance()
//使用有参构造函数构造对象
KClass.newInstance(参数...)
//通过字符串方式
VirtualReflectionUtil.newInstance("com.lt.virtual_reflection.bean.A")
```
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,35 @@ internal class VirtualReflectionSymbolProcessor(private val environment: SymbolP
//记录有参数的构造方法,稍后处理
val haveArgsConstructor = ArrayList<KSClassConstructorInfo>()
file.appendText(
"fun <T : Any> kotlin.reflect.KClass<T>.$functionName(): T {\n" +
" return when (simpleName) {\n"
"fun <T : Any> kotlin.reflect.KClass<T>.$functionName(): T =\n" +
" VirtualReflectionUtil.$functionName(qualifiedName!!) as T\n" +
"\n" +
"fun <T : Any> kotlin.reflect.KClass<T>.$functionName(vararg args: Any?): T =\n" +
" VirtualReflectionUtil.$functionName(qualifiedName!!, *args) as T\n" +
"\n" +
"object VirtualReflectionUtil {\n" +
" fun $functionName(name: String): Any = when (name) {\n"
)
//处理空参构造方法
classConstructorList.forEach {
if (it.constructorArgsType.isNotEmpty()) {
haveArgsConstructor.add(it)
return@forEach
}
file.appendText(" \"${it.className}\" -> ${it.packageName}.${it.className}()\n")
val name = "${it.packageName}.${it.className}"
file.appendText(" \"$name\" -> $name()\n")
}
file.appendText(
" else -> throw RuntimeException(\"\$this \$simpleName: Not find in VirtualReflection config\")\n" +
" } as T\n" +
"}\n\n"
" else -> throw RuntimeException(\"\$name: Not find in VirtualReflection config\")\n" +
" }\n" +
"\n" +
" fun $functionName(name: String, vararg args: Any?): Any = when {\n"
)
//处理有参构造方法
file.appendText(
"fun <T : Any> kotlin.reflect.KClass<T>.$functionName(vararg args: Any?): T {\n" +
" return when {\n"
)
haveArgsConstructor.forEach {
val name = "${it.packageName}.${it.className}"
file.appendText(
" simpleName == \"${it.className}\" && args.size == ${it.constructorArgsType.size} -> ${it.packageName}.${it.className}("
" name == \"$name\" && args.size == ${it.constructorArgsType.size} -> $name("
)
//处理参数强转
it.constructorArgsType.forEachIndexed { index, s ->
Expand All @@ -97,8 +102,8 @@ internal class VirtualReflectionSymbolProcessor(private val environment: SymbolP
file.appendText(")\n")
}
file.appendText(
" else -> throw RuntimeException(\"\$this \$simpleName: Not find in VirtualReflection config\")\n" +
" } as T\n" +
" else -> throw RuntimeException(\"\$name: Not find in VirtualReflection config\")\n" +
" }\n" +
"}"
)
file.close()
Expand Down

0 comments on commit eb442a7

Please sign in to comment.