A kotlin compiler plugin to make easy to write visitor pattern.
MIT License
A kotlin compiler plugin to make easy to write visitor pattern.
This plugin is going to provides two code generator shown below:
accept
with visitor anonymous object from when
expr with metadata by annotation.accept
method and visitor abstract class from annotations.First, you need to apply this gradle plugin
plugins {
id("org.jetbrains.kotlin.jvm") version "<kotlin version>"
id("com.anatawa12.auto-visitor") version "<version>"
}
To generate visitor class and accept function, add @GenerateAccept
, @HasVisitor
, and @HasAccept
to the parent
class, add @GenerateVisitor
to the visitor abstract class, and add @HasVisitor
to each child class.
TODO: add example code and link to it.
To generate calling accept function, surround when expr with autoVisitor
like shown below:
autoVisitor(some_expr) { variable ->
when (variable) {
is SomeClass -> {
statements
}
else -> {
statements
}
}
}
The compiler plugin of Kotlin.
The gradle plugin. This includes applying Kotlin compiler plugin, applying Annotation Processor.
The pluggable annotation processor to verify annotation usages from java.
A pluggable annotation processor for the compiler plugin. See readme in it for more details
Because the generated code of it is linear search,
when
expr with is
checking is much slower than visitor pattern
(see benchmarks), so It's better to use visitor pattern.
However, The visitor pattern needs much boilerplate code,
so I want not to write visitor pattern myself, want to generate it.