Skip to content

Commit 1fc398c

Browse files
committed
feat: add support for generic return types and default values in MethodAccessor and MethodCondition
1 parent 767ed0f commit 1fc398c

5 files changed

Lines changed: 70 additions & 2 deletions

File tree

kavaref-android/src/main/kotlin/com/highcapable/kavaref/platform/MethodAccessor.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package com.highcapable.kavaref.platform
2626
import android.os.Build
2727
import java.lang.reflect.Member
2828
import java.lang.reflect.Method
29+
import java.lang.reflect.Type
2930

3031
/**
3132
* Accessor for [Method] to provide platform-specific features.
@@ -35,8 +36,10 @@ internal class MethodAccessor(override val member: Member) : MemberAccessor(memb
3536
private val method = member as Method
3637

3738
val returnType: Class<*> get() = method.returnType
39+
val genericReturnType: Type get() = method.genericReturnType
3840
val isBridge: Boolean get() = method.isBridge
3941
val isDefault: Boolean get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) method.isDefault else false
42+
val defaultValue: Any? get() = method.defaultValue
4043

4144
override fun toString() = member.toString()
4245
}

kavaref-core/src/main/kotlin/com/highcapable/kavaref/condition/MethodCondition.kt

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import com.highcapable.kavaref.condition.type.Modifiers
3030
import com.highcapable.kavaref.resolver.MethodResolver
3131
import com.highcapable.kavaref.resolver.processor.MemberProcessor
3232
import java.lang.reflect.Method
33+
import java.lang.reflect.Type
3334

3435
/**
3536
* Condition for [Method] of [MethodResolver].
@@ -42,6 +43,12 @@ class MethodCondition<T : Any> : ExecutableCondition<Method, MethodResolver<T>,
4243
/** @see Method.getReturnType */
4344
var returnTypeCondition: ((Class<*>) -> Boolean)? = null
4445

46+
/** @see Method.getGenericReturnType */
47+
var genericReturnType: TypeMatcher? = null
48+
49+
/** @see Method.getGenericReturnType */
50+
var genericReturnTypeCondition: ((Type) -> Boolean)? = null
51+
4552
/** @see Method.isBridge */
4653
var isBridge: Boolean? = null
4754

@@ -54,6 +61,12 @@ class MethodCondition<T : Any> : ExecutableCondition<Method, MethodResolver<T>,
5461
/** @see Method.isDefault */
5562
var isDefaultNot: Boolean? = null
5663

64+
/** @see Method.getDefaultValue */
65+
var defaultValue: Any? = null
66+
67+
/** @see Method.getDefaultValue */
68+
var defaultValueCondition: ((Any?) -> Boolean)? = null
69+
5770
override fun name(name: String) = apply { super.name(name) }
5871
override fun name(condition: (String) -> Boolean) = apply { super.name(condition) }
5972
override fun modifiers(vararg modifiers: Modifiers) = apply { super.modifiers(*modifiers) }
@@ -105,6 +118,16 @@ class MethodCondition<T : Any> : ExecutableCondition<Method, MethodResolver<T>,
105118
this.returnTypeCondition = condition
106119
}
107120

121+
/** @see Method.getGenericReturnType */
122+
fun genericReturnType(type: TypeMatcher) = apply {
123+
this.genericReturnType = type
124+
}
125+
126+
/** @see Method.getGenericReturnType */
127+
fun genericReturnType(condition: (Type) -> Boolean) = apply {
128+
this.genericReturnTypeCondition = condition
129+
}
130+
108131
/** @see Method.isBridge */
109132
fun isBridge(isBridge: Boolean) = apply {
110133
this.isBridge = isBridge
@@ -125,16 +148,30 @@ class MethodCondition<T : Any> : ExecutableCondition<Method, MethodResolver<T>,
125148
this.isDefaultNot = isDefault
126149
}
127150

151+
/** @see Method.getDefaultValue */
152+
fun defaultValue(value: Any?) = apply {
153+
this.defaultValue = value
154+
}
155+
156+
/** @see Method.getDefaultValue */
157+
fun defaultValue(condition: (Any?) -> Boolean) = apply {
158+
this.defaultValueCondition = condition
159+
}
160+
128161
override fun initializeCopiedData(newSelf: MemberCondition<Method, MethodResolver<T>, T>) {
129162
super.initializeCopiedData(newSelf)
130163

131164
(newSelf as? MethodCondition)?.also {
132165
it.returnType = returnType
133166
it.returnTypeCondition = returnTypeCondition
167+
it.genericReturnType = genericReturnType
168+
it.genericReturnTypeCondition = genericReturnTypeCondition
134169
it.isBridge = isBridge
135170
it.isBridgeNot = isBridgeNot
136171
it.isDefault = isDefault
137172
it.isDefaultNot = isDefaultNot
173+
it.defaultValue = defaultValue
174+
it.defaultValueCondition = defaultValueCondition
138175
}
139176
}
140177

@@ -144,10 +181,14 @@ class MethodCondition<T : Any> : ExecutableCondition<Method, MethodResolver<T>,
144181
(other as? MethodCondition)?.also { condition ->
145182
condition.returnType?.let { returnType = it }
146183
condition.returnTypeCondition?.let { returnTypeCondition = it }
184+
condition.genericReturnType?.let { genericReturnType = it }
185+
condition.genericReturnTypeCondition?.let { genericReturnTypeCondition = it }
147186
condition.isBridge?.let { isBridge = it }
148187
condition.isBridgeNot?.let { isBridgeNot = it }
149188
condition.isDefault?.let { isDefault = it }
150189
condition.isDefaultNot?.let { isDefaultNot = it }
190+
condition.defaultValue?.let { defaultValue = it }
191+
condition.defaultValueCondition?.let { defaultValueCondition = it }
151192
}
152193
}
153194

@@ -164,18 +205,26 @@ class MethodCondition<T : Any> : ExecutableCondition<Method, MethodResolver<T>,
164205
get() = super.conditionStringMap + mapOf(
165206
RETURN_TYPE to returnType,
166207
RETURN_TYPE_CONDITION to returnTypeCondition,
208+
GENERIC_RETURN_TYPE to genericReturnType,
209+
GENERIC_RETURN_TYPE_CONDITION to genericReturnTypeCondition,
167210
IS_BRIDGE to isBridge,
168211
IS_BRIDGE_NOT to isBridgeNot,
169212
IS_DEFAULT to isDefault,
170-
IS_DEFAULT_NOT to isDefaultNot
213+
IS_DEFAULT_NOT to isDefaultNot,
214+
DEFAULT_VALUE to defaultValue,
215+
DEFAULT_VALUE_CONDITION to defaultValueCondition
171216
)
172217

173218
companion object {
174219
const val RETURN_TYPE = "returnType"
175220
const val RETURN_TYPE_CONDITION = "returnTypeCondition"
221+
const val GENERIC_RETURN_TYPE = "genericReturnType"
222+
const val GENERIC_RETURN_TYPE_CONDITION = "genericReturnTypeCondition"
176223
const val IS_BRIDGE = "isBridge"
177224
const val IS_BRIDGE_NOT = "isBridgeNot"
178225
const val IS_DEFAULT = "isDefault"
179226
const val IS_DEFAULT_NOT = "isDefaultNot"
227+
const val DEFAULT_VALUE = "defaultValue"
228+
const val DEFAULT_VALUE_CONDITION = "defaultValueCondition"
180229
}
181230
}

kavaref-core/src/main/kotlin/com/highcapable/kavaref/resolver/processor/MemberProcessor.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,20 @@ object MemberProcessor {
173173
.filter(configuration, MethodCondition.RETURN_TYPE_CONDITION, condition.returnTypeCondition) { key, value ->
174174
runOrElse { key(value.returnType) }
175175
}
176+
.filter(configuration, MethodCondition.GENERIC_RETURN_TYPE, condition.genericReturnType) { key, value ->
177+
key.matches(value.genericReturnType)
178+
}
179+
.filter(configuration, MethodCondition.GENERIC_RETURN_TYPE_CONDITION, condition.genericReturnTypeCondition) { key, value ->
180+
runOrElse { key(value.genericReturnType) }
181+
}
176182
.filter(configuration, MethodCondition.IS_BRIDGE, condition.isBridge) { key, value -> value.isBridge == key }
177183
.filter(configuration, MethodCondition.IS_BRIDGE_NOT, condition.isBridgeNot) { key, value -> value.isBridge != key }
178184
.filter(configuration, MethodCondition.IS_DEFAULT, condition.isDefault) { key, value -> value.isDefault == key }
179185
.filter(configuration, MethodCondition.IS_DEFAULT_NOT, condition.isDefaultNot) { key, value -> value.isDefault != key }
186+
.filter(configuration, MethodCondition.DEFAULT_VALUE, condition.defaultValue) { key, value -> value.defaultValue == key }
187+
.filter(configuration, MethodCondition.DEFAULT_VALUE_CONDITION, condition.defaultValueCondition) { key, value ->
188+
runOrElse { key(value.defaultValue) }
189+
}
180190
.endAccess<Method>()
181191
.resolve(configuration)
182192

kavaref-jvm/src/main/kotlin/com/highcapable/kavaref/platform/MethodAccessor.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package com.highcapable.kavaref.platform
2525

2626
import java.lang.reflect.Member
2727
import java.lang.reflect.Method
28+
import java.lang.reflect.Type
2829

2930
/**
3031
* Accessor for [Method] to provide platform-specific features.
@@ -34,8 +35,10 @@ internal class MethodAccessor(override val member: Member) : MemberAccessor(memb
3435
private val method = member as Method
3536

3637
val returnType: Class<*> get() = method.returnType
38+
val genericReturnType: Type get() = method.genericReturnType
3739
val isBridge: Boolean get() = method.isBridge
3840
val isDefault: Boolean get() = method.isDefault
41+
val defaultValue: Any? get() = method.defaultValue
3942

4043
override fun toString() = member.toString()
4144
}

kavaref-runtime-stub/src/main/kotlin/com/highcapable/kavaref/platform/MethodAccessor.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@
1919
*
2020
* This file is created by fankes on 2026/6/5.
2121
*/
22-
@file:Suppress("unused")
22+
@file:Suppress("unused", "RedundantNullableReturnType")
2323

2424
package com.highcapable.kavaref.platform
2525

2626
import java.lang.reflect.Member
2727
import java.lang.reflect.Method
28+
import java.lang.reflect.Type
2829

2930
/**
3031
* Accessor for [Method] to provide platform-specific features.
3132
*/
3233
class MethodAccessor(override val member: Member) : MemberAccessor(member) {
3334
val returnType: Class<*> get() = error("Stub!")
35+
val genericReturnType: Type get() = error("Stub!")
3436
val isBridge: Boolean get() = error("Stub!")
3537
val isDefault: Boolean get() = error("Stub!")
38+
val defaultValue: Any? get() = error("Stub!")
3639
}

0 commit comments

Comments
 (0)