Skip to content

Commit

Permalink
fix:初始化顺序和lazy默认值
Browse files Browse the repository at this point in the history
  • Loading branch information
aleyn97 committed Sep 6, 2023
1 parent 386add0 commit bb228d0
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion annotation/src/main/java/com/aleyn/annotation/Singleton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ import kotlin.reflect.KClass
@Target(AnnotationTarget.CLASS)
annotation class Singleton(
val bind: KClass<*> = Unit::class,
val lazy: Boolean = false
val lazy: Boolean = true
)

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract class ICar {
}

@Qualifier("car2")
@Singleton(lazy = true)
@Singleton(lazy = false)
class Car2 : ICar() {
override val name = "Car2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,17 @@ class FindHandleClass(private val outModel: ArrayList<HandleModel>) : ClassNode(
}

if (!initAnn?.desc.isNullOrBlank() && !routerInitializer.isNullOrBlank()) {
var priority = 0
var priority: Byte = 0
var async = false
initAnn!!.values?.forEachIndexed { index, any ->
if (index % 2 == 1) return@forEachIndexed
if (any == "priority") {
priority = (initAnn.values.getOrNull(index + 1) as? Int) ?: 0
}
if (any == "async") {
priority = (initAnn.values.getOrNull(index + 1) as? Byte) ?: 0
} else if (any == "async") {
async = (initAnn.values.getOrNull(index + 1) as? Boolean) ?: false
}
}
outModel.add(HandleModel.Initializer(priority, if (async) 1 else 0, name))
outModel.add(HandleModel.Initializer(priority.toInt(), if (async) 1 else 0, name))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class InitializerInstructAdapter(

override fun visitCode() {
initializerClass?.forEach {
mv.visitIntInsn(Opcodes.BIPUSH, it.priority)
mv.visitIntInsn(Opcodes.T_BOOLEAN, it.async)
iconst(it.priority)
mv.visitIntInsn(Opcodes.BIPUSH, it.async)
mv.visitTypeInsn(Opcodes.NEW, it.className)
dup()
invokespecial(it.className, "<init>", "()V", false)
Expand Down
Binary file modified plugins/plugin.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fun RouterMeta.RouterAutowired.generatorClass(
val simpleName = routerAutowired.simpleName
val list = routerAutowired.list

val parentDeclaration = routerAutowired.list.firstOrNull()?.second?.parent as? KSClassDeclaration
val parentDeclaration =
routerAutowired.list.firstOrNull()?.second?.parent as? KSClassDeclaration

val className = simpleName + AUTOWIRED_SUFFIX

Expand Down Expand Up @@ -166,8 +167,8 @@ private fun RouterMeta.Module.genDefinition(fileBuilder: FileSpec.Builder): FunS
fileBuilder.addImport("com.aleyn.router.inject.qualifier", "sq")
"sq(\"${it.qualifier}\")"
}
val lazy = if (it.isType(SINGLETON) && it.lazy == true) {
(if (qualifier == "") "" else ", ") + "lazy = true"
val lazy = if (it.isType(SINGLETON)) {
(if (qualifier.isBlank()) "" else ", ") + "lazy = ${it.lazy}"
} else ""

fileBuilder.addImport(it.packageName, it.label)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ClassInjectScanner(private val logger: KSPLogger) {
when (annotation.shortName.asString()) {
Singleton::class.simpleName -> {
keyword = SINGLETON
lazy = annotation.arguments.getArgValue("lazy") ?: false
lazy = annotation.arguments.getArgValue("lazy") ?: true
declarationBind = annotation.arguments.getArgValue<KSType>("bind")
}

Expand Down
6 changes: 5 additions & 1 deletion router/src/main/java/com/aleyn/router/LRouter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,15 @@ object LRouter {
return Core.getOrNull(clazz, qualifier)
}

inline fun <reified T> getByType(qualifier: Qualifier? = null): T? {
return Core.getOrNull(T::class, qualifier)
}

/**
* 获取注入类
*/
@JvmStatic
fun <T> getByJava(clazz: Class<*>, qualifier: Qualifier? = null): T? {
fun <T> getByJava(clazz: Class<*>, qualifier: Qualifier?): T? {
return Core.getOrNull(clazz.kotlin, qualifier)
}

Expand Down
15 changes: 8 additions & 7 deletions router/src/main/java/com/aleyn/router/core/RouterController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Looper
import com.aleyn.router.data.InitializerData
import com.aleyn.router.data.InterceptorData
import com.aleyn.router.executor.DefaultPoolExecutor
import com.aleyn.router.inject.Core
import com.aleyn.router.inject.initModuleRouter
import com.aleyn.router.inject.registerAllInitializer
import com.aleyn.router.inject.registerIntercept
Expand Down Expand Up @@ -60,19 +61,19 @@ internal object RouterController {
// 注册所有初始化器
registerAllInitializer()
// 执行各模块初始化器
async {
initializerModule.forEach {
if (it.async) {
it.routerInitializer.create(context)
} else {
main { it.routerInitializer.create(context) }
}
initializerModule.forEach {
if (it.async) {
async { it.routerInitializer.create(context) }
} else {
it.routerInitializer.create(context)
}
}
initializerModule.clear()
//注册 路由 拦截器 信息
async {
initModuleRouter()
registerIntercept()
Core.createEagerInstances()
}
}

Expand Down
6 changes: 6 additions & 0 deletions router/src/main/java/com/aleyn/router/data/InitializerData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ internal class InitializerData(
else -> -1
}
}

override fun toString(): String {
return """
priority=${priority},async=${async}
""".trimIndent()
}
}
2 changes: 1 addition & 1 deletion router/src/main/java/com/aleyn/router/inject/Tools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fun paramOf(vararg params: Any?) = Parameters(params.toMutableList())
*/
inline fun <reified T> lRouterSingle(
qualifier: Qualifier? = null,
lazy: Boolean = false,
lazy: Boolean = true,
noinline definition: Def<T>
) {
val def = createDefinition(Kind.SINGLETON, qualifier, definition)
Expand Down

0 comments on commit bb228d0

Please sign in to comment.