Skip to content

Commit 57a6870

Browse files
committed
feat: 版本兼容,方法路由支持base64(json)、json对象
1 parent 89d5b4b commit 57a6870

File tree

8 files changed

+156
-153
lines changed

8 files changed

+156
-153
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.wzbos.android.rudolph
2+
3+
import java.lang.reflect.Type
4+
5+
open class ExtraType @JvmOverloads constructor(
6+
var type: Type,
7+
val base64: Boolean = false,
8+
val json: Boolean = false
9+
)
10+
11+
class UnknownExtraType(
12+
val className: String,
13+
) : ExtraType(type = Any::class.java, json = true, base64 = true)

rudolph-annotations/src/main/java/cn/wzbos/android/rudolph/RouteInfo.kt

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class RouteInfo(builder: Builder) {
1313
@Deprecated("请使用targetClass", replaceWith = ReplaceWith("targetClass"))
1414
val target: String?
1515
val targetClass: Class<*>?
16-
val extras: MutableMap<String, Type>?
16+
val extras: MutableMap<String, ExtraType>?
1717
var type: RouteType?
1818
private set
1919

@@ -32,12 +32,12 @@ class RouteInfo(builder: Builder) {
3232
var url: MutableList<String>? = null
3333
var target: String? = null
3434
var targetClass: Class<*>? = null
35-
var extras: MutableMap<String, Type>? = null
35+
var extras: MutableMap<String, ExtraType>? = null
3636
var routeType: RouteType? = null
3737
var tag: String? = null
3838
var interceptors: MutableList<Class<out RouteInterceptor>>? = null
3939

40-
@Deprecated("已过期")
40+
@Deprecated("已过期", replaceWith = ReplaceWith("url(path)"))
4141
fun path(path: String): Builder {
4242
this.url = mutableListOf(path)
4343
return this
@@ -48,7 +48,7 @@ class RouteInfo(builder: Builder) {
4848
return this
4949
}
5050

51-
51+
@Deprecated("已过期", replaceWith = ReplaceWith("targetClass(target)"))
5252
fun target(target: String): Builder {
5353
this.target = target
5454
return this
@@ -59,27 +59,45 @@ class RouteInfo(builder: Builder) {
5959
return this
6060
}
6161

62+
@Deprecated("已过期")
6263
fun extra(key: String, className: String): Builder {
63-
val type = when (className) {
64-
Boolean::class.java.name -> Boolean::class.java
65-
Byte::class.java.name -> Byte::class.java
66-
Char::class.java.name -> Char::class.java
67-
Short::class.java.name -> Short::class.java
68-
Double::class.java.name -> Double::class.java
69-
Float::class.java.name -> Float::class.java
70-
Int::class.java.name -> Int::class.java
71-
Long::class.java.name -> Long::class.java
72-
else -> Class.forName(className)
64+
return try {
65+
val type = when (className) {
66+
Boolean::class.java.name -> Boolean::class.java
67+
Byte::class.java.name -> Byte::class.java
68+
Char::class.java.name -> Char::class.java
69+
Short::class.java.name -> Short::class.java
70+
Double::class.java.name -> Double::class.java
71+
Float::class.java.name -> Float::class.java
72+
Int::class.java.name -> Int::class.java
73+
Long::class.java.name -> Long::class.java
74+
Void::class.java.name -> Void::class.java
75+
else -> Class.forName(className)
76+
}
77+
extra(key, ExtraType(type))
78+
} catch (e: Exception) {
79+
println("类型转换失败! (target=${target}, key:${key}, className:${className})")
80+
e.printStackTrace()
81+
//兼容老版本,此处统一转换为Any类型
82+
extra(key, UnknownExtraType(className))
7383
}
74-
extra(key, type)
75-
return this
7684
}
7785

78-
fun extra(key: String, cls: Type): Builder {
86+
@JvmOverloads
87+
fun extra(
88+
key: String,
89+
type: Type,
90+
base64: Boolean = false,
91+
json: Boolean = false
92+
): Builder {
93+
return extra(key, ExtraType(type, base64 = base64, json = json))
94+
}
95+
96+
fun extra(key: String, extraType: ExtraType): Builder {
7997
if (extras == null) {
8098
extras = LinkedHashMap()
8199
}
82-
extras?.put(key, cls)
100+
extras?.put(key, extraType)
83101
return this
84102
}
85103

rudolph-annotations/src/main/java/cn/wzbos/android/rudolph/annotations/Route.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import kotlin.reflect.KClass
1414
AnnotationTarget.PROPERTY_SETTER
1515
)
1616
annotation class Route(
17+
val value: String = "",
18+
1719
/**
1820
* 完整的路由地址,
1921
* 例如:
@@ -23,8 +25,6 @@ annotation class Route(
2325
*/
2426
val urls: Array<String> = [],
2527

26-
val value: String = "",
27-
2828
/**
2929
* 当前路由自定义标签
3030
*/

rudolph-compiler/src/main/java/cn/wzbos/android/rudolph/RudolphProcessor.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,17 @@ class RudolphProcessor : AbstractProcessor() {
333333
val clsName: String = ClassName.get(field.asType()).toString()
334334
if (clsName.contains("<") && clsName.contains(">")) {
335335
builder.addCode(
336-
"\n\t.extra(\$S,new \$T<\$T>(){}.getType())",
336+
"\n\t.extra(\$S, new \$T<\$T>(){}.getType(), ${param.base64}, ${param.json})",
337337
argName,
338338
TypeToken,
339339
ClassName.get(field.asType())
340340
)
341341
} else {
342-
builder.addCode("\n\t.extra(\$S,\$T.class)", argName, ClassName.get(field.asType()))
342+
builder.addCode(
343+
"\n\t.extra(\$S, \$T.class, ${param.base64}, ${param.json})",
344+
argName,
345+
ClassName.get(field.asType())
346+
)
343347
}
344348
}
345349
builder.addCode(".build());")
@@ -492,12 +496,13 @@ class RudolphProcessor : AbstractProcessor() {
492496
}
493497

494498
private fun getRoutePath(element: Element?, route: Route): String {
495-
val url = route.value
496-
return if (url.isEmpty()) {
497-
"/" + element.toString().lowercase(Locale.getDefault())
498-
} else {
499-
route.value.first().lowercase(Locale.getDefault())
499+
if (route.urls.isNotEmpty()) {
500+
return route.urls.first().lowercase(Locale.getDefault())
501+
}
502+
if (route.value.isNotBlank()) {
503+
return route.value.lowercase(Locale.getDefault())
500504
}
505+
return "/" + element.toString().lowercase(Locale.getDefault())
501506
}
502507

503508

rudolph/src/main/java/cn/wzbos/android/rudolph/router/MethodRouter.kt

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package cn.wzbos.android.rudolph.router
22

3-
import android.app.Application
43
import android.content.Context
5-
import cn.wzbos.android.rudolph.Consts
6-
import cn.wzbos.android.rudolph.Rudolph
4+
import cn.wzbos.android.rudolph.UnknownExtraType
75
import cn.wzbos.android.rudolph.annotations.Route
86
import cn.wzbos.android.rudolph.exception.ErrorCode
97
import cn.wzbos.android.rudolph.exception.ErrorMessage
@@ -17,7 +15,7 @@ class MethodRouter internal constructor(builder: UriRouter.Builder<*>) : Router<
1715
const val TAG = "MethodRouter"
1816
}
1917

20-
private val uriKvs: MutableMap<String, String?>? = builder.uriAllParams
18+
private val uriKvs: MutableMap<String, String?> = builder.uriAllParams
2119

2220
@Deprecated(message = "use execute", replaceWith = ReplaceWith("execute(context)"))
2321
fun open(context: Context?): Any? {
@@ -42,40 +40,26 @@ class MethodRouter internal constructor(builder: UriRouter.Builder<*>) : Router<
4240
val route = m.getAnnotation(Route::class.java)
4341
if (route != null && route.match(rawUrl)) {
4442
val values: MutableList<Any?> = ArrayList()
45-
val params = extraTypes
46-
params?.forEach { (argName, argType) ->
47-
var argValue: Any? = null
48-
if (Consts.RAW_URI == argName) {
49-
argValue = rawUrl
50-
} else {
51-
when {
52-
Application::class.java == argType -> {
53-
argValue = Rudolph.context
54-
}
55-
Context::class.java == argType -> {
56-
argValue = context ?: Rudolph.context
57-
}
58-
else -> {
59-
kotlin.run {
60-
uriKvs?.forEach { (key, value) ->
61-
if (argName.equals(key, ignoreCase = true)) {
62-
argValue = if (value.isNullOrEmpty()) {
63-
null
64-
} else {
65-
TypeUtils.getObject(
66-
context,
67-
argName,
68-
value,
69-
argType
70-
)
71-
}
72-
return@run
73-
}
74-
}
75-
}
76-
}
43+
extraTypes?.onEachIndexed { index, entry ->
44+
//兼容2.1.0之前的版本,反射获取参数类型
45+
if (entry.value is UnknownExtraType) {
46+
entry.value.type = m.parameterTypes[index]
47+
}
48+
}?.forEach { (argName, argType) ->
49+
var value: String? = null
50+
for (element in uriKvs) {
51+
if (argName.equals(element.key, ignoreCase = true)) {
52+
value = element.value
53+
break
7754
}
7855
}
56+
57+
val argValue: Any? = TypeUtils.getObject(
58+
context,
59+
argName,
60+
value,
61+
argType
62+
)
7963
values.add(argValue)
8064
}
8165
val obj = m.invoke(null, *values.toTypedArray())

rudolph/src/main/java/cn/wzbos/android/rudolph/router/RouteBuilder.kt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import cn.wzbos.android.rudolph.Rudolph.getRouter
99
import cn.wzbos.android.rudolph.logger.RLog
1010
import cn.wzbos.android.rudolph.utils.TypeUtils
1111
import java.io.Serializable
12-
import java.lang.reflect.Type
1312
import java.net.URLDecoder
1413
import java.util.*
1514

@@ -24,7 +23,6 @@ abstract class RouteBuilder<B : RouteBuilder<B, R>?, R : Router<*>?>(val rawUrl:
2423
const val TAG = "RouteBuilder"
2524
}
2625

27-
2826
override var extras: Bundle = Bundle()
2927
var scheme: MutableList<String>? = null
3028
var host: MutableList<String>? = null
@@ -38,16 +36,13 @@ abstract class RouteBuilder<B : RouteBuilder<B, R>?, R : Router<*>?>(val rawUrl:
3836
private set
3937
var routeTag: String? = null
4038
private set
41-
var extraTypes: MutableMap<String, Type>? = null
39+
var extraTypes: MutableMap<String, ExtraType>? = null
4240
private set
4341
var interceptors: MutableList<Class<out RouteInterceptor>>? = null
4442
private set
4543

4644
private var encodedQuery: String? = null
4745

48-
// constructor(routeInfo: RouteInfo) : this(routeInfo.target) {
49-
// setRouterInfo(routeInfo)
50-
// }
5146

5247
override fun putExtra(map: Bundle?): B {
5348
extras.putAll(map)
@@ -210,7 +205,6 @@ abstract class RouteBuilder<B : RouteBuilder<B, R>?, R : Router<*>?>(val rawUrl:
210205
return this as B
211206
}
212207

213-
214208
private fun putUriAllParams() {
215209
//返回的所有的地址参数与查询参数值
216210
val resultMap = uriAllParams
@@ -226,11 +220,11 @@ abstract class RouteBuilder<B : RouteBuilder<B, R>?, R : Router<*>?>(val rawUrl:
226220
TypeUtils.getObject(null, key, value, type, this)
227221
}
228222
} else {
223+
//未用 @Extra 注解声明的参数类型,
229224
extras.putString(key, value)
230225
}
231226
}
232227

233-
234228
init {
235229
RLog.i(TAG, "rawUrl=${rawUrl}")
236230

@@ -250,7 +244,7 @@ abstract class RouteBuilder<B : RouteBuilder<B, R>?, R : Router<*>?>(val rawUrl:
250244
val info = getRouter(rawUrl)
251245
if (info != null) {
252246
setRouterInfo(info)
253-
}else{
247+
} else {
254248
RLog.e(TAG, "没有路由信息,rawUrl=${rawUrl}")
255249
}
256250
putUriAllParams()
@@ -295,7 +289,7 @@ abstract class RouteBuilder<B : RouteBuilder<B, R>?, R : Router<*>?>(val rawUrl:
295289
try {
296290
val name = URLDecoder.decode(kv[0], "utf-8")
297291
val value = URLDecoder.decode(kv[1], "utf-8")
298-
params[name] = value
292+
params[name] = value.ifEmpty { null }
299293
} catch (e: Exception) {
300294
RLog.e(TAG, "getUriAllParams failed!", e)
301295
}

rudolph/src/main/java/cn/wzbos/android/rudolph/router/Router.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class Router<T> internal constructor(builder: RouteBuilder<*, *>) {
1717
var routePath: MutableList<String>? = builder.path
1818
var routeType: RouteType? = builder.routeType
1919
var routeTag: String? = builder.routeTag
20-
var extraTypes: MutableMap<String, Type>? = builder.extraTypes
20+
var extraTypes: MutableMap<String, ExtraType>? = builder.extraTypes
2121
var interceptors: MutableList<Class<out RouteInterceptor>>? = builder.interceptors
2222

2323
val uriData: Uri

0 commit comments

Comments
 (0)