atlantis210
01/07/2022, 2:12 PMannotation class Dummy(val integer: Int = 0, val string: String = "")
but cannot seem to retrieve arguments when passed in @Dummy(integer = 1, string = "hello")
. I thought using visitAnnotation
will do the trick but somehow the code doesn't get inside...
I think that this is the `process()`method that is not done right but not sure how to do it...
override fun process(resolver: Resolver): List<KSAnnotated> {
val symbols = resolver.getSymbolsWithAnnotation("com.example.annotation.Dummy")
print("symbol + $symbols")
val list = symbols.filter { !it.validate() }.toList()
symbols
.filter { (it is KSAnnotation || it is KSClassDeclaration) && it.validate() }
.forEach { it.accept(BuilderVisitor(), Unit) }
return list
}
Jiaxiang
01/07/2022, 8:06 PMprint
will not work since it is running in a gradle daemon, you need to log with the KSP logger in your processor, and you should be able to see the log if you have correct log level enabled with your gradle argument.BuilderVisitor
?atlantis210
01/08/2022, 11:03 AMlogger.loggin
, the level was not high enough... It now works, thanks !
For the second response, I just override visitAnnotation
and visitClassDeclaration
override fun visitAnnotation(annotation: KSAnnotation, data: Unit) {
annotation.arguments.onEach { argument ->
when (argument.name?.asString()) {
"string" -> stringDefaultValue = argument.value as String
"integer" -> integerDefaultValue = argument.value as Int
null -> Unit
}
}
}
and
override fun visitClassDeclaration(classDeclaration: KSClassDeclaration, data: Unit) {
val packageName = classDeclaration.containingFile!!.packageName.asString()
val className = classDeclaration.simpleName.asString()
val file = codeGenerator.createNewFile(
dependencies = Dependencies(
aggregating = false,
),
packageName = packageName,
fileName = "Dummy$className",
)
file.appendText("package $packageName\n\n")
file.appendText("val dummy$className = $className(\n")
classDeclaration.primaryConstructor?.parameters?.forEach {
val name = it.name!!.asString()
val value = when (StringBuilder(
it.type.resolve().declaration.qualifiedName?.asString() ?: "<ERROR>"
).toString()) {
"kotlin.String" -> stringDefaultValue
"<http://kotlin.Int|kotlin.Int>" -> integerDefaultValue
else -> 0
}
// TODO get value by default given in Annotation
file.appendText("\t$name = $value,\n")
}
file.appendText(")")
file.close()
}
Jiaxiang
01/12/2022, 9:46 AMatlantis210
01/12/2022, 9:47 AMJiaxiang
01/12/2022, 9:49 AMatlantis210
01/24/2022, 9:43 AMJiaxiang
01/24/2022, 10:20 AM(it is KSAnnotation || it is KSClassDeclaration)
check is not needed, since you are not going to get KSAnnotation
from getSymbolsWithAnnotation
, as you can’t annotate an annotation. What you can do here is to add classDeclaration.annotations.forEach{ it.accept(this, Unit) }
so that your visitor can recursively visit into the annotations on the classes you found with KSP.atlantis210
01/24/2022, 2:56 PM