What I want to do is this:
try { } catch (@AutoLog e: Exception) { //generate a log statement here }
i.e. when the exception variable in catch block is annotated with @AutoLog, generate a log statement automatically in the beginning of catch block during build process.
I tried to do that by creating my own Kotlin compiler plugin. But I just found:
-
‘backend’ extensions works on jvm byte code and jvm byte code does not retain annotations on exception variable (and more generally, all local variables). I watched Kevin Most’s Kotlin Compiler plugin talk at Kotlin Conf 2018, and checked the Kotlin source code(in the path compiler/backend/src/org/jetbrains/kotlin/codegen/extensions). ‘backend’ plugins can add log statement but seem not able to check the existence of @AutoLog on Exception variable.
-
‘frontend’ extensions should do the job, but there is no example to show how to do similar things. The existing extensions in the path compiler/frontend/src/org/jetbrains/kotlin/extensions are all coarse-grained, e.g. DeclarationAttributeAltererExtension , don’t show how to manipulate the AST(Abstract syntax tree), which I suppose I have to work on to achieve my goal.
So, my question is , do I have to use Compiler plugin, particularly frontend entensions to do that, and how? Thanks.