Skip to content

Commit

Permalink
增加日志的开关, 在项目中检查生成为空的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojinzi123 committed Dec 31, 2023
1 parent e3ef606 commit cbd6a0f
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import com.xiaojinzi.component.support.AttrAutoWireMode

class AutowireProcessor(
override val environment: SymbolProcessorEnvironment,
val logger: KSPLogger = environment.logger,
val codeGenerator: CodeGenerator = environment.codeGenerator,
) : BaseHostProcessor(
environment = environment,
) {
Expand Down Expand Up @@ -87,6 +85,7 @@ class AutowireProcessor(
isSubActivity -> {
it.addStatement("this.injectAttrValue(target = target, bundle = target.intent?.extras?: Bundle())")
}

isSubFragment -> {
it.addStatement("this.injectAttrValue(target = target, bundle = target.arguments?: Bundle())")
}
Expand Down Expand Up @@ -130,18 +129,22 @@ class AutowireProcessor(
).firstOrNull()

if (uriAutoWireAnno != null) {
logger.info(
message = "uriAutoWireAnno = $uriAutoWireAnno"
)
if (logEnable) {
logger.info(
message = "uriAutoWireAnno = $uriAutoWireAnno"
)
}
}

val attrAutoWireAnno = ksPropertyDeclaration.getAnnotationsByType(
annotationKClass = AttrValueAutowiredAnno::class
).firstOrNull()

logger.info(
message = "attrAutoWireAnno = $attrAutoWireAnno"
)
if (logEnable) {
logger.info(
message = "attrAutoWireAnno = $attrAutoWireAnno"
)
}

val serviceAutoWireAnno = ksPropertyDeclaration.getAnnotationsByType(
annotationKClass = ServiceAutowiredAnno::class
Expand Down Expand Up @@ -259,11 +262,13 @@ class AutowireProcessor(
}
funSpec.endControlFlow()
}

AttrAutoWireMode.Default -> {
defaultModel.invoke(
funSpec, attrAutoWireAnnoItemName,
)
}

AttrAutoWireMode.Override -> {
overrideModel.invoke(
funSpec,
Expand Down Expand Up @@ -295,7 +300,7 @@ class AutowireProcessor(
format = "target.%N = %T.%N(tClass = %T::class)",
propertyName,
mClassNameServiceManager,
if(isPropertyNullable) {
if (isPropertyNullable) {
"get"
} else {
"requiredGet"
Expand Down Expand Up @@ -330,9 +335,11 @@ class AutowireProcessor(
.build()

try {
logger.info(
message = "classDeclarationKsType1 = $classDeclarationKsType, isSubFragmentActivity = $isSubActivity, isSubFragment = $isSubFragment",
)
if (logEnable) {
logger.info(
message = "classDeclarationKsType1 = $classDeclarationKsType, isSubFragmentActivity = $isSubActivity, isSubFragment = $isSubFragment",
)
}
codeGenerator.createNewFile(
dependencies = Dependencies.ALL_FILES,
packageName = fileSpec.packageName,
Expand All @@ -342,9 +349,11 @@ class AutowireProcessor(
fileSpec.toString().toByteArray()
)
}
logger.info(
message = "classDeclarationKsType2 = $classDeclarationKsType, isSubFragmentActivity = $isSubActivity, isSubFragment = $isSubFragment",
)
if (logEnable) {
logger.info(
message = "classDeclarationKsType2 = $classDeclarationKsType, isSubFragmentActivity = $isSubActivity, isSubFragment = $isSubFragment",
)
}
} catch (e: Exception) {
// ignore
}
Expand Down Expand Up @@ -387,17 +396,22 @@ class AutowireProcessor(
)
}

return uriAutoWireAnnotatedList + attrAutoWireAnnotatedList + serviceAutoWireAnnotatedList
return emptyList()

}

override fun finish() {
super.finish()
logger.info("$TAG finish")
if (logEnable) {
logger.info("$TAG finish")
}
}

override fun onError() {
super.onError()
logger.info("$TAG onError")
if (logEnable) {
logger.info("$TAG onError")
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package com.xiaojinzi.component.compiler.kt

import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessorEnvironment
import com.google.devtools.ksp.symbol.KSAnnotated

abstract class BaseHostProcessor(
open val environment: SymbolProcessorEnvironment,
private val logger: KSPLogger = environment.logger,
val logger: KSPLogger = environment.logger,
val codeGenerator: CodeGenerator = environment.codeGenerator,
val componentModuleName: String = (environment.options["ModuleName"]
?: environment.options["HOST"]) ?: throw NULLHOSTEXCEPTION
?: environment.options["HOST"]) ?: throw NULL_HOST_EXCEPTION,
val logEnable: Boolean = environment.options["LogEnable"]?.toBoolean() ?: false,
) : BaseProcessor() {

companion object {

val NULLHOSTEXCEPTION = RuntimeException(
val NULL_HOST_EXCEPTION = RuntimeException(
"""the host must not be null,you must define host in build.gradle file,such as:
defaultConfig {
minSdkVersion 14
Expand All @@ -36,9 +39,11 @@ abstract class BaseHostProcessor(

final override fun process(resolver: Resolver): List<KSAnnotated> {
round++
logger.warn(
message = "BaseHostProcessor, round = $round, object is ${this.javaClass.simpleName}, componentModuleName is $componentModuleName",
)
if (logEnable) {
logger.warn(
message = "BaseHostProcessor, round = $round, object is ${this.javaClass.simpleName}, componentModuleName is $componentModuleName",
)
}
if (round == 1) {
doProcess(resolver = resolver)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ import kotlin.reflect.KClass
*/
class ModuleProcessor(
override val environment: SymbolProcessorEnvironment,
val logger: KSPLogger = environment.logger,
val codeGenerator: CodeGenerator = environment.codeGenerator,
) : BaseHostProcessor(
environment = environment,
) {

val TAG = "ModuleProcessor"
private val TAG = "ModuleProcessor"

private val componentClassName = ClassName(
packageName = ComponentConstants.COMPONENT_CLASS_NAME.packageName(),
Expand Down Expand Up @@ -578,9 +576,11 @@ class ModuleProcessor(
.also {
when (item) {
is KSFunctionDeclaration -> {
logger.info(
message = "fragment KSFunctionDeclaration = ${item.qualifiedName?.asString()}"
)
if (logEnable) {
logger.info(
message = "fragment KSFunctionDeclaration = ${item.qualifiedName?.asString()}"
)
}
if (item.parameters.size != 1) {
throw RuntimeException(
"FragmentAnno 注解的方法必须只有一个参数, ${item.qualifiedName}"
Expand Down Expand Up @@ -771,9 +771,13 @@ class ModuleProcessor(
path = ComponentConstants.SEPARATOR + path
}

logger.info("routerAnno.hostAndPath = ${element.location}")
if (logEnable) {
logger.info("routerAnno.hostAndPath = ${element.location}")
}
routerAnno.interceptorNames.forEach { item ->
logger.info("routerAnno.interceptorName = $item")
if (logEnable) {
logger.info("routerAnno.interceptorName = $item")
}
}

return RouterAnnoBean(
Expand Down Expand Up @@ -921,7 +925,9 @@ class ModuleProcessor(
}

is KSFunctionDeclaration -> {
logger.info("element.qualifiedName = ${element.qualifiedName?.asString()}")
if (logEnable) {
logger.info("element.qualifiedName = ${element.qualifiedName?.asString()}")
}
listOf(
customerIntentCallClassName,
mClassNameIntent,
Expand Down Expand Up @@ -1053,27 +1059,52 @@ class ModuleProcessor(
.mapNotNull { it as? KSClassDeclaration }
.filterNot { it.qualifiedName == null }
.toList()
if (logEnable) {
logger.info(
"moduleAppAnnotatedList = $moduleAppAnnotatedList"
)
}
if (logEnable) {
logger.info(
" $TAG, moduleAppAnnotatedList.size = ${moduleAppAnnotatedList.size}"
)
}

// Service 的
val serviceAnnotatedList: List<KSAnnotated> = resolver
.getSymbolsWithAnnotation(
annotationName = ServiceAnno::class.qualifiedName!!
)
.toList()
if (logEnable) {
logger.info(
" $TAG, serviceAnnotatedList.size = ${serviceAnnotatedList.size}"
)
}

// ServiceDecorator 的
val serviceDecoratorAnnotatedList: List<KSAnnotated> = resolver
.getSymbolsWithAnnotation(
annotationName = ServiceDecoratorAnno::class.qualifiedName!!
)
.toList()
if (logEnable) {
logger.info(
" $TAG, serviceDecoratorAnnotatedList.size = ${serviceDecoratorAnnotatedList.size}"
)
}

// Fragment 的
val fragmentAnnotatedList: List<KSAnnotated> = resolver
.getSymbolsWithAnnotation(
annotationName = FragmentAnno::class.qualifiedName!!
)
.toList()
if (logEnable) {
logger.info(
" $TAG, fragmentAnnotatedList.size = ${fragmentAnnotatedList.size}"
)
}

// 全局拦截器的
val globalInterceptorAnnotatedList: List<KSClassDeclaration> = resolver
Expand All @@ -1082,6 +1113,11 @@ class ModuleProcessor(
)
.mapNotNull { it as? KSClassDeclaration }
.toList()
if (logEnable) {
logger.info(
" $TAG, globalInterceptorAnnotatedList.size = ${globalInterceptorAnnotatedList.size}"
)
}

// 拦截器
val interceptorAnnotatedList: List<KSClassDeclaration> = resolver
Expand All @@ -1090,30 +1126,41 @@ class ModuleProcessor(
)
.mapNotNull { it as? KSClassDeclaration }
.toList()
if (logEnable) {
logger.info(
" $TAG, interceptorAnnotatedList.size = ${interceptorAnnotatedList.size}"
)
}

// 路由的
val routerAnnotatedList: List<KSAnnotated> = resolver
.getSymbolsWithAnnotation(
annotationName = RouterAnno::class.qualifiedName!!
)
.toList()
if (logEnable) {
logger.info(
" $TAG, routerAnnotatedList.size = ${routerAnnotatedList.size}"
)
}

// 路由降级的
val routerDegradeAnnotatedList: List<KSAnnotated> = resolver
.getSymbolsWithAnnotation(
annotationName = RouterDegradeAnno::class.qualifiedName!!
)
.toList()
if (logEnable) {
logger.info(
" $TAG, routerDegradeAnnotatedList.size = ${routerDegradeAnnotatedList.size}"
)
}

val packageNameStr = "com.xiaojinzi.component.impl"
val classNameStr = ComponentUtil.transformHostForClass(
componentModuleName
) + ComponentUtil.MODULE

logger.info(
"moduleAppAnnotatedList = $moduleAppAnnotatedList"
)

val typeSpec = TypeSpec
.classBuilder(
name = classNameStr
Expand Down Expand Up @@ -1188,6 +1235,15 @@ class ModuleProcessor(
.build()

try {
codeGenerator.generatedFile.apply {
if (logEnable) {
logger.info("$TAG 一共有 ${this.size} 个生成的文件")
}
}.forEachIndexed { index, file ->
if (logEnable) {
logger.info("$TAG${index + 1}个文件:${file.path}")
}
}
codeGenerator.createNewFile(
dependencies = Dependencies.ALL_FILES,
packageName = fileSpec.packageName,
Expand All @@ -1198,23 +1254,28 @@ class ModuleProcessor(
)
}
} catch (e: Exception) {
// ignore
if (logEnable) {
logger.info("$TAG 生成文件异常啦~~~")
logger.exception(e)
}
}

return moduleAppAnnotatedList + serviceAnnotatedList + serviceDecoratorAnnotatedList +
fragmentAnnotatedList + globalInterceptorAnnotatedList + interceptorAnnotatedList +
routerAnnotatedList + routerDegradeAnnotatedList
return emptyList()

}

override fun finish() {
super.finish()
logger.info("$TAG finish")
if (logEnable) {
logger.info("$TAG finish")
}
}

override fun onError() {
super.onError()
logger.info("$TAG onError")
if (logEnable) {
logger.info("$TAG onError")
}
}

}
Expand Down
Loading

0 comments on commit cbd6a0f

Please sign in to comment.