Skip to content

Commit

Permalink
1.1.3 数组类型支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Sep 7, 2024
1 parent c20ee05 commit d2b0255
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import java.util.function.Supplier
@Internal
open class LazyAnnotatedClass protected constructor(
source: String,
isArray: Boolean,
isInstant: Boolean,
getter: Supplier<Class<*>?>,
val annotations: List<ClassAnnotation>
) : LazyClass(source, isInstant, getter) {
) : LazyClass(source, isArray, isInstant, getter) {

fun getAnnotation(annotation: Class<out Annotation>): ClassAnnotation {
return annotations.first { it.source.name == annotation.name }
Expand All @@ -23,25 +24,33 @@ open class LazyAnnotatedClass protected constructor(
}

override fun toString(): String {
return "LazyAnnotatedClass(annotations=$annotations)"
return "LazyAnnotatedClass(${if (isArray) "Array[$name]" else name},@${annotations})"
}

companion object {

fun of(clazz: Class<*>, annotations: List<ClassAnnotation>): LazyAnnotatedClass {
return LazyAnnotatedClass(clazz.name, true, { clazz }, annotations)
return LazyAnnotatedClass(clazz.name, isArray = false, isInstant = true, getter = { clazz }, annotations = annotations)
}

fun of(clazz: Class<*>, annotations: List<ClassAnnotation>, isArray: Boolean): LazyAnnotatedClass {
return LazyAnnotatedClass(clazz.name, isArray, true, { clazz }, annotations)
}

fun of(source: String, annotations: List<ClassAnnotation>): LazyAnnotatedClass {
return LazyAnnotatedClass(source, false, { runCatching { Class.forName(source) }.getOrNull() }, annotations)
return LazyAnnotatedClass(source, isArray = false, isInstant = false, getter = { runCatching { Class.forName(source) }.getOrNull() }, annotations = annotations)
}

fun of(source: String, annotations: List<ClassAnnotation>, classFinder: ClassAnalyser.ClassFinder): LazyAnnotatedClass {
return LazyAnnotatedClass(source, false, { classFinder.findClass(source.replace('/', '.')) }, annotations)
return LazyAnnotatedClass(source, isArray = false, isInstant = false, getter = { classFinder.findClass(source.replace('/', '.')) }, annotations = annotations)
}

fun of(source: String, annotations: List<ClassAnnotation>, isArray: Boolean, classFinder: ClassAnalyser.ClassFinder): LazyAnnotatedClass {
return LazyAnnotatedClass(source, isArray, false, { classFinder.findClass(source.replace('/', '.')) }, annotations)
}

fun of(source: String, getter: Supplier<Class<*>?>, annotations: List<ClassAnnotation>): LazyAnnotatedClass {
return LazyAnnotatedClass(source, false, getter, annotations)
return LazyAnnotatedClass(source, isArray = false, isInstant = false, getter = getter, annotations = annotations)
}
}
}
24 changes: 17 additions & 7 deletions analyser/src/main/kotlin/org/tabooproject/reflex/LazyClass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,46 @@ import java.util.function.Supplier
* @author 坏黑
* @since 2022/1/21 6:47 PM
*/
open class LazyClass protected constructor(source: String, val isInstant: Boolean, val getter: Supplier<Class<*>?>) {
open class LazyClass protected constructor(source: String, val isArray: Boolean, val isInstant: Boolean, val getter: Supplier<Class<*>?>) {

val name = source.replace('/', '.')

val simpleName = name.substringAfterLast('.')

val instance by lazy(LazyThreadSafetyMode.NONE) { getter.get() }
val instance by lazy(LazyThreadSafetyMode.NONE) {
if (isArray) java.lang.reflect.Array.newInstance(getter.get(), 0).javaClass else getter.get()
}

fun notfound(): Nothing = throw ClassNotFoundException("Class not found: $name")

override fun toString(): String {
return "LazyClass($name)"
return "LazyClass(${if (isArray) "Array[$name]" else name})"
}

companion object {

fun of(clazz: Class<*>): LazyClass {
return LazyClass(clazz.name, true) { clazz }
return LazyClass(clazz.name, isArray = false, isInstant = true) { clazz }
}

fun of(clazz: Class<*>, isArray: Boolean): LazyClass {
return LazyClass(clazz.name, isArray, true) { clazz }
}

fun of(source: String): LazyClass {
return LazyClass(source, false) { runCatching { Class.forName(source.replace('/', '.')) }.getOrNull() }
return LazyClass(source, isArray = false, isInstant = false) { runCatching { Class.forName(source.replace('/', '.')) }.getOrNull() }
}

fun of(source: String, classFinder: ClassAnalyser.ClassFinder): LazyClass {
return LazyClass(source, false) { classFinder.findClass(source.replace('/', '.')) }
return LazyClass(source, isArray = false, isInstant = false) { classFinder.findClass(source.replace('/', '.')) }
}

fun of(source: String, isArray: Boolean, classFinder: ClassAnalyser.ClassFinder): LazyClass {
return LazyClass(source, isArray, false) { classFinder.findClass(source.replace('/', '.')) }
}

fun of(source: String, getter: Supplier<Class<*>?>): LazyClass {
return LazyClass(source, false, getter)
return LazyClass(source, isArray = false, isInstant = false, getter = getter)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class AsmClassMethod(
fun read() {
var visitParameterType = false
var visitReturnType = false
var visitArrayType = false
SignatureReader(descriptor).accept(object : SignatureWriter() {

override fun visitParameterType(): SignatureVisitor {
Expand All @@ -76,23 +77,30 @@ class AsmClassMethod(

override fun visitClassType(name: String) {
if (visitParameterType) {
localParameter.add(LazyAnnotatedClass.of(name, parameterAnnotations[localParameter.size] ?: emptyList(), classFinder))
localParameter.add(LazyAnnotatedClass.of(name, parameterAnnotations[localParameter.size] ?: emptyList(), visitArrayType, classFinder))
}
if (visitReturnType) {
localResult = LazyClass.of(name, classFinder)
localResult = LazyClass.of(name, visitArrayType, classFinder)
}
visitArrayType = false
super.visitClassType(name)
}

override fun visitBaseType(descriptor: Char) {
if (visitParameterType) {
localParameter += LazyAnnotatedClass.of(Reflection.getPrimitiveType(descriptor), parameterAnnotations[localParameter.size] ?: emptyList())
localParameter += LazyAnnotatedClass.of(Reflection.getPrimitiveType(descriptor), parameterAnnotations[localParameter.size] ?: emptyList(), visitArrayType)
}
if (visitReturnType) {
localResult = LazyClass.of(Reflection.getPrimitiveType(descriptor))
localResult = LazyClass.of(Reflection.getPrimitiveType(descriptor), visitArrayType)
}
visitArrayType = false
super.visitBaseType(descriptor)
}

override fun visitArrayType(): SignatureVisitor {
visitArrayType = true
return super.visitArrayType()
}
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.tabooproject.reflex.asm

import org.objectweb.asm.signature.SignatureReader
import org.objectweb.asm.signature.SignatureVisitor
import org.objectweb.asm.signature.SignatureWriter
import org.tabooproject.reflex.ClassAnalyser
import org.tabooproject.reflex.Internal
Expand All @@ -12,16 +13,23 @@ object AsmSignature {

fun signatureToClass(signature: String, classFinder: ClassAnalyser.ClassFinder): List<LazyClass> {
val list = ArrayList<LazyClass>()
var visitArrayType = false
SignatureReader(signature).accept(object : SignatureWriter() {

override fun visitClassType(name: String) {
super.visitClassType(name)
list.add(LazyClass.of(name, classFinder))
list.add(LazyClass.of(name, visitArrayType, classFinder))
}

override fun visitBaseType(descriptor: Char) {
super.visitBaseType(descriptor)
list.add(LazyClass.of(Reflection.getPrimitiveType(descriptor)))
list.add(LazyClass.of(Reflection.getPrimitiveType(descriptor), visitArrayType))
}

override fun visitArrayType(): SignatureVisitor {
super.visitArrayType()
visitArrayType = true
return this
}
})
if (list.lastOrNull()?.name == "void") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class AnalyserTestAsm {

var stringVar = "test"

var stringArrayVar = arrayOf("test1", "test2")

@AnalyserAnnotation("test3")
constructor() : this(0)

Expand All @@ -35,6 +37,10 @@ class AnalyserTestAsm {
return value
}

private fun methodArray(arr1: Array<String>, value: String, arr2: IntArray): DoubleArray {
return doubleArrayOf(1.0, 2.0)
}

companion object {

@JvmField
Expand Down Expand Up @@ -117,6 +123,18 @@ class AnalyserTestAsm {
assert(target.stringVar == "update")
}

@Test
fun testGetArrayVar() {
assert(analyse.getField("stringArrayVar").fieldType == Array<String>::class.java)
}

@Test
fun testSetArrayVer() {
val target = TestTargetAsm()
analyse.getField("stringArrayVar").set(target, arrayOf("update1", "update2"))
assert(target.stringArrayVar.contentEquals(arrayOf("update1", "update2")))
}

@Test
fun testGetStatic() {
analyse.getField("intRangeVal").get()!!
Expand All @@ -136,6 +154,12 @@ class AnalyserTestAsm {
assert(analyse.getMethodByType("method", Int::class.java).invoke(target, 10) == 10)
}

@Test
fun testInvokeArrayMethod() {
val target = TestTargetAsm()
analyse.getMethod("methodArray", arrayOf("test1", "test2"), "test", intArrayOf(1, 2)).invoke(target, arrayOf("test1", "test2"), "test", intArrayOf(1, 2))
}

@Test
fun testInvokeStaticMethod() {
assert(analyse.getMethod("methodStatic", 10).invokeStatic(10) == 10)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=org.tabooproject.reflex
version=1.1.2
version=1.1.3
kotlin.incremental=true
kotlin.incremental.java=true
kotlin.caching.enabled=true
Expand Down

0 comments on commit d2b0255

Please sign in to comment.