Skip to content

Commit

Permalink
[1.0.10] Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Jul 20, 2022
1 parent 5390bc6 commit 6ed964f
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ abstract class ClassConstructor(name: String, owner: Class<*>) : ClassMember(nam

abstract fun instance(vararg values: Any?): Any?

val parameterTypes: Array<Class<*>>
get() = parameter.map { p -> p.instance ?: Unknown::class.java }.toTypedArray()
val parameterTypes by lazy(LazyThreadSafetyMode.NONE) {
parameter.map { p -> p.instance ?: Unknown::class.java }.toTypedArray()
}

override fun toString(): String {
return "ClassConstructor(parameter=$parameter)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ abstract class ClassMethod(name: String, owner: Class<*>) : ClassMember(name, ow
val returnType: Class<*>
get() = result.instance ?: Unknown::class.java

val parameterTypes: Array<Class<*>>
get() = parameter.map { p -> p.instance ?: Unknown::class.java }.toTypedArray()
val parameterTypes by lazy(LazyThreadSafetyMode.NONE) {
parameter.map { p -> p.instance ?: Unknown::class.java }.toTypedArray()
}

override fun toString(): String {
return "ClassMethod(result=$result)"
Expand Down
26 changes: 20 additions & 6 deletions analyser/src/main/kotlin/org/tabooproject/reflex/ClassStructure.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
package org.tabooproject.reflex

import java.util.LinkedList

/**
* @author 坏黑
* @since 2022/1/21 6:41 PM
*/
abstract class ClassStructure(
val owner: Class<*>,
val annotations: List<ClassAnnotation>,
val fields: List<ClassField>,
val methods: List<ClassMethod>,
val constructors: List<ClassConstructor>,
annotations: List<ClassAnnotation>,
fields: List<ClassField>,
methods: List<ClassMethod>,
constructors: List<ClassConstructor>,
) {

val name by lazy { kotlin.runCatching { owner.name }.getOrNull() }
val annotations = LinkedList(annotations)

val fields = LinkedList(fields)

val methods = LinkedList(methods)

val constructors = LinkedList(constructors)

val simpleName by lazy { kotlin.runCatching { owner.simpleName }.getOrNull() }
val name by lazy(LazyThreadSafetyMode.NONE) {
kotlin.runCatching { owner.name }.getOrNull()
}

val simpleName by lazy(LazyThreadSafetyMode.NONE) {
kotlin.runCatching { owner.simpleName }.getOrNull()
}

abstract fun getField(name: String): ClassField

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.lang.invoke.MethodType
@Internal
abstract class JavaClassConstructor(name: String, owner: Class<*>) : ClassConstructor(name, owner) {

private val handle: MethodHandle by lazy {
private val handle by lazy(LazyThreadSafetyMode.NONE) {
UnsafeAccess.lookup.findConstructor(owner, MethodType.methodType(Void.TYPE, parameterTypes))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import java.lang.invoke.MethodHandle
@Internal
abstract class JavaClassField(name: String, owner: Class<*>) : ClassField(name, owner) {

private val handleGetter: MethodHandle by lazy {
private val handleGetter by lazy(LazyThreadSafetyMode.NONE) {
if (isStatic) {
UnsafeAccess.lookup.findStaticGetter(owner, name, fieldType)
} else {
UnsafeAccess.lookup.findGetter(owner, name, fieldType)
}
}

private val handleSetter: MethodHandle by lazy {
private val handleSetter by lazy(LazyThreadSafetyMode.NONE) {
if (isStatic) {
UnsafeAccess.lookup.findStaticSetter(owner, name, fieldType)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.lang.invoke.MethodType
@Internal
abstract class JavaClassMethod(name: String, owner: Class<*>) : ClassMethod(name, owner) {

private val handle: MethodHandle by lazy {
private val handle by lazy(LazyThreadSafetyMode.NONE) {
if (isStatic) {
UnsafeAccess.lookup.findStatic(owner, name, MethodType.methodType(returnType, parameterTypes))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ open class LazyClass(source: String) {

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

open val instance by lazy { kotlin.runCatching { Class.forName(name) }.getOrNull() }
open val instance by lazy(LazyThreadSafetyMode.NONE) { kotlin.runCatching { Class.forName(name) }.getOrNull() }

override fun toString(): String {
return "LazyClass(name='$name')"
Expand Down
6 changes: 4 additions & 2 deletions analyser/src/main/kotlin/org/tabooproject/reflex/LazyEnum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import java.util.concurrent.ConcurrentHashMap
* @author 坏黑
* @since 2022/1/24 9:27 PM
*/
@Suppress("UNCHECKED_CAST")
@Suppress("UNCHECKED_CAST", "TYPE_MISMATCH_WARNING")
class LazyEnum(val source: LazyClass, val name: String) {

val instance: Enum<*> by lazy { allOf(source.instance as Class<Enum<*>>)[name]!! }
val instance by lazy(LazyThreadSafetyMode.NONE) {
allOf(source.instance as Class<Enum<*>>)[name]!!
}

override fun toString(): String {
return "LazyEnum(source=$source, name='$name')"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import java.lang.reflect.Constructor
@Internal
class InstantClassConstructor(owner: Class<*>, private val constructor: Constructor<*>) : JavaClassConstructor("<init>", owner) {

val annotationsLocal by lazy {
val annotationsLocal by lazy(LazyThreadSafetyMode.NONE) {
constructor.declaredAnnotations.map { InstantAnnotation(it) }
}

val parameterLocal by lazy {
val parameterLocal by lazy(LazyThreadSafetyMode.NONE) {
val parameterAnnotations = constructor.parameterAnnotations
constructor.parameterTypes.mapIndexed { idx, it -> InstantAnnotatedClass(it, parameterAnnotations[idx].map { i -> InstantAnnotation(i) }) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import java.lang.reflect.Modifier
@Internal
class InstantClassMethod(owner: Class<*>, private val method: Method) : JavaClassMethod(method.name, owner) {

val annotationsLocal by lazy {
val annotationsLocal by lazy(LazyThreadSafetyMode.NONE) {
method.declaredAnnotations.map { InstantAnnotation(it) }
}

val parameterLocal by lazy {
val parameterLocal by lazy(LazyThreadSafetyMode.NONE) {
val parameterAnnotations = method.parameterAnnotations
method.parameterTypes.mapIndexed { idx, it -> InstantAnnotatedClass(it, parameterAnnotations[idx].map { i -> InstantAnnotation(i) }) }
}
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.0.9
version=1.0.10
kotlin.incremental=true
kotlin.incremental.java=true
kotlin.caching.enabled=true
Expand Down
33 changes: 18 additions & 15 deletions reflex/src/main/kotlin/org/tabooproject/reflex/Reflex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,27 @@ class Reflex {
* @param name 方法名称
* @param parameter 方法参数
* @param isStatic 是否为静态方法
* @param findToParent 是否查找父类方法
*/
fun <T> Any.invokeMethod(name: String, vararg parameter: Any?, isStatic: Boolean = false): T? {
fun <T> Any.invokeMethod(name: String, vararg parameter: Any?, isStatic: Boolean = false, findToParent: Boolean = true): T? {
return if (isStatic && this is Class<*>) {
ReflexClass.of(this).getMethod(name, true, *parameter).invokeStatic(*parameter) as T?
ReflexClass.of(this).getMethod(name, findToParent, *parameter).invokeStatic(*parameter) as T?
} else {
ReflexClass.of(javaClass).getMethod(name, true, *parameter).invoke(this, *parameter) as T?
ReflexClass.of(javaClass).getMethod(name, findToParent, *parameter).invoke(this, *parameter) as T?
}
}

/**
* 获取字段
* @param path 字段名称,使用 "/" 符号进行递归获取
* @param isStatic 是否为静态字段
* @param findToParent 是否查找父类字段
*/
fun <T> Any.getProperty(path: String, isStatic: Boolean = false): T? {
fun <T> Any.getProperty(path: String, isStatic: Boolean = false, findToParent: Boolean = true): T? {
return if (path.contains('/')) {
getLocalProperty<Any>(path.substringBefore('/'), isStatic)?.getProperty(path.substringAfter('/'), isStatic)
getLocalProperty<Any>(path.substringBefore('/'), isStatic)?.getProperty(path.substringAfter('/'), isStatic, findToParent)
} else {
getLocalProperty(path, isStatic)
getLocalProperty(path, isStatic, findToParent)
}
}

Expand All @@ -60,28 +62,29 @@ class Reflex {
* @param path 字段名称,使用 "/" 符号进行递归获取
* @param value 值
* @param isStatic 是否为静态字段
* @param findToParent 是否查找到父类字段
*/
fun Any.setProperty(path: String, value: Any?, isStatic: Boolean = false) {
fun Any.setProperty(path: String, value: Any?, isStatic: Boolean = false, findToParent: Boolean = true) {
if (path.contains('/')) {
getLocalProperty<Any>(path.substringBefore('/'), isStatic)!!.setProperty(path.substringAfter('/'), value, isStatic)
getLocalProperty<Any>(path.substringBefore('/'), isStatic)!!.setProperty(path.substringAfter('/'), value, isStatic, findToParent)
} else {
setLocalProperty(path, value, isStatic)
setLocalProperty(path, value, isStatic, findToParent)
}
}

private fun <T> Any.getLocalProperty(name: String, isStatic: Boolean = false): T? {
private fun <T> Any.getLocalProperty(name: String, isStatic: Boolean = false, findToParent: Boolean = true): T? {
return if (isStatic && this is Class<*>) {
ReflexClass.of(this).getField(name, true).get() as T?
ReflexClass.of(this).getField(name, findToParent).get() as T?
} else {
ReflexClass.of(javaClass).getField(name, true).get(this) as T?
ReflexClass.of(javaClass).getField(name, findToParent).get(this) as T?
}
}

private fun Any.setLocalProperty(name: String, value: Any?, isStatic: Boolean = false) {
private fun Any.setLocalProperty(name: String, value: Any?, isStatic: Boolean = false, findToParent: Boolean = true) {
if (isStatic && this is Class<*>) {
ReflexClass.of(this).getField(name, true).setStatic(value)
ReflexClass.of(this).getField(name, findToParent).setStatic(value)
} else {
ReflexClass.of(javaClass).getField(name, true).set(this, value)
ReflexClass.of(javaClass).getField(name, findToParent).set(this, value)
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions reflex/src/main/kotlin/org/tabooproject/reflex/ReflexClass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import java.util.concurrent.ConcurrentHashMap
*/
class ReflexClass(val structure: ClassStructure) {

val superclass by lazy { if (structure.owner.superclass != Any::class.java) of(structure.owner.superclass) else null }
val interfaces by lazy { structure.owner.interfaces.map { of(it) } }
val superclass by lazy(LazyThreadSafetyMode.NONE) {
if (structure.owner.superclass != Any::class.java) of(structure.owner.superclass) else null
}

val interfaces by lazy(LazyThreadSafetyMode.NONE) {
structure.owner.interfaces.map { of(it) }
}

fun getField(name: String, findToParent: Boolean = true): ClassField {
var field = name
Expand Down Expand Up @@ -56,11 +61,15 @@ class ReflexClass(val structure: ClassStructure) {

private val analyseMap = ConcurrentHashMap<String, ReflexClass>()

fun of(clazz: Class<*>): ReflexClass {
if (analyseMap.containsKey(clazz.name)) {
fun of(clazz: Class<*>, saving: Boolean = true): ReflexClass {
if (saving && analyseMap.containsKey(clazz.name)) {
return analyseMap[clazz.name]!!
}
return ReflexClass(ClassAnalyser.analyse(clazz)).apply { analyseMap[clazz.name] = this }
return ReflexClass(ClassAnalyser.analyse(clazz)).apply {
if (saving) {
analyseMap[clazz.name] = this
}
}
}
}
}

0 comments on commit 6ed964f

Please sign in to comment.