Skip to content

Commit

Permalink
可以通过functionNameWithVirtualReflection修改生成的方法名
Browse files Browse the repository at this point in the history
  • Loading branch information
ltttttttttttt committed Mar 17, 2023
1 parent ffbedb8 commit 75b73e1
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Configure packages that require virtual reflection, Your app dir, build.gradle.k
ksp {
//Configure multiple paths separated by spaces
arg("packageListWithVirtualReflection", "com.lt.virtual_reflection.bean/*your package*/")
//Configure the generated function name, which defaults to newInstance
//arg("functionNameWithVirtualReflection", xxx)
}
```

Expand Down
5 changes: 4 additions & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ allprojects {

Step 2.在app模块目录内的build.gradle.kts内添加:

version = [![](https://jitpack.io/v/ltttttttttttt/VirtualReflection.svg)](https://jitpack.io/#ltttttttttttt/VirtualReflection)
version
= [![](https://jitpack.io/v/ltttttttttttt/VirtualReflection.svg)](https://jitpack.io/#ltttttttttttt/VirtualReflection)

```kotlin
plugins {
Expand All @@ -57,6 +58,8 @@ Step 3.使用VirtualReflection
ksp {
//配置多个路径用空格隔开
arg("packageListWithVirtualReflection", "com.lt.virtual_reflection.bean/*你的包路径*/")
//配置生成的方法名,默认是newInstance
//arg("functionNameWithVirtualReflection", xxx)
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.lt.reflection.ifNullOfEmpty
internal class KspOptions(environment: SymbolProcessorEnvironment) {
private val options = environment.options
private val packageList = "packageListWithVirtualReflection"
private val functionName = "functionNameWithVirtualReflection"

/**
* 具体哪些包(完全相等)中的类需要虚拟反射功能,空格隔开
Expand All @@ -19,4 +20,10 @@ internal class KspOptions(environment: SymbolProcessorEnvironment) {
options[packageList].ifNullOfEmpty { return listOf() }
.split(" ")
.map { "$it." }

/**
* 生成的方法名,默认newInstance
*/
fun getFunctionName(): String =
options[functionName].ifNullOfEmpty { return "newInstance" }
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ internal class VirtualReflectionSymbolProcessor(private val environment: SymbolP
override fun process(resolver: Resolver): List<KSAnnotated> {
if (isHandled) return listOf()
isHandled = true
val packageList = KspOptions(environment).getPackageList()
val kspOptions = KspOptions(environment)
val packageList = kspOptions.getPackageList()
val functionName = kspOptions.getFunctionName()
val ret = mutableListOf<KSAnnotated>()
val classConstructorList = ArrayList<KSClassConstructorInfo>()
resolver.getAllFiles()
Expand All @@ -45,12 +47,15 @@ internal class VirtualReflectionSymbolProcessor(private val environment: SymbolP
}
}
}
createFile(classConstructorList)
createFile(classConstructorList, functionName)
//返回无法处理的符号
return ret
}

private fun createFile(classConstructorList: ArrayList<KSClassConstructorInfo>) {
private fun createFile(
classConstructorList: ArrayList<KSClassConstructorInfo>,
functionName: String
) {
val file = environment.codeGenerator.createNewFile(
Dependencies(
true,
Expand All @@ -60,7 +65,7 @@ internal class VirtualReflectionSymbolProcessor(private val environment: SymbolP
//记录有参数的构造方法,稍后处理
val haveArgsConstructor = ArrayList<KSClassConstructorInfo>()
file.appendText(
"fun <T : Any> kotlin.reflect.KClass<T>.newInstance(): T {\n" +
"fun <T : Any> kotlin.reflect.KClass<T>.$functionName(): T {\n" +
" return when (simpleName) {\n"
)
//处理空参构造方法
Expand All @@ -78,7 +83,7 @@ internal class VirtualReflectionSymbolProcessor(private val environment: SymbolP
)
//处理有参构造方法
file.appendText(
"fun <T : Any> kotlin.reflect.KClass<T>.newInstance(vararg args: Any?): T {\n" +
"fun <T : Any> kotlin.reflect.KClass<T>.$functionName(vararg args: Any?): T {\n" +
" return when {\n"
)
haveArgsConstructor.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.google.devtools.ksp.isPrivate
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSVisitorVoid
import com.google.devtools.ksp.symbol.Modifier
import com.lt.reflection.getKSTypeInfo
import com.lt.reflection.options.KSClassConstructorInfo

Expand All @@ -23,6 +24,7 @@ internal class VirtualReflectionVisitor(
*/
override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) {
//获取class信息并创建kt文件
if (Modifier.PRIVATE in classDeclaration.modifiers) return
val packageName = classDeclaration.packageName.asString()
val className = classDeclaration.simpleName.asString()
classDeclaration.getConstructors()
Expand Down
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ android {
"packageListWithVirtualReflection",
"com.lt.virtual_reflection.bean com.lt.virtual_reflection.bean_d"
)
//arg(
// "functionNameWithVirtualReflection",
// "newInstance2"
//)
}
}

Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/com/lt/virtual_reflection/bean/B.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package com.lt.virtual_reflection.bean
* effect :
* warning:
*/
class B constructor() {
internal class B constructor() {
var a = 0
var b = ""

Expand All @@ -14,9 +14,11 @@ class B constructor() {
this.b = b ?: ""
}

constructor(a: Long) : this()
private constructor(a: Long) : this()

override fun toString(): String {
return "B(a=$a, b='$b')"
}
}
}

private class B2()

0 comments on commit 75b73e1

Please sign in to comment.