Hi guys, I have a dumb question: is it possible to...
# ksp
a
Hi guys, I have a dumb question: is it possible to add logs in the BuilderProcessor ? I tried println, print and even using the environment.logger, but I don't know where are going the logs... ? Also, I'm trying to get params from an annotation
annotation 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...
Copy code
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
}
j
print
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.
for your second question: what is inside your
BuilderVisitor
?
a
Hi, thanks for your response. Indeed, I was using
logger.loggin
, the level was not high enough... It now works, thanks ! For the second response, I just override
visitAnnotation
and
visitClassDeclaration
Copy code
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
Copy code
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 do you see what's not working ?
j
sorry I am bound by other works as the moment, will take a look ASAP
a
ok thanks, sorry if I woke you up :o
j
no worries, was not sleeping 😛
🙂 1
a
Still got no bandwidth for my problem ? :s
j
sorry it took too long to respond. I think the problem here is that
(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.
a
aw ok I did not take it that way. Thanks a lot ! I'll try
Forgot to tell you but it worked just fine ! Thanks a lot :)
👍 1