https://github.com/anatawa12/auto-visitor
A kotlin compiler plugin to make easy to write visitor pattern.
https://github.com/anatawa12/auto-visitor
annotation-processor gradle-plugin kotlin visitor-pattern
Last synced: about 2 months ago
JSON representation
A kotlin compiler plugin to make easy to write visitor pattern.
- Host: GitHub
- URL: https://github.com/anatawa12/auto-visitor
- Owner: anatawa12
- License: mit
- Created: 2021-01-30T09:42:24.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-16T20:02:12.000Z (about 3 years ago)
- Last Synced: 2025-04-07T17:34:21.045Z (11 months ago)
- Topics: annotation-processor, gradle-plugin, kotlin, visitor-pattern
- Language: Kotlin
- Homepage:
- Size: 391 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
Auto Visitor Kotlin Compiler Plugin
====
[](https://api.anatawa12.com/short/a12-slowly-doc)
[](https://plugins.gradle.org/plugin/com.anatawa12.auto-visitor)
A kotlin compiler plugin to make easy to write visitor pattern.
This plugin is going to provides two code generator shown below:
1. Generate calling `accept` with visitor anonymous object from `when` expr with metadata by annotation.
1. Generate `accept` method and visitor abstract class from annotations.
## How to use
First, you need to apply this gradle plugin
```kotlin
plugins {
id("org.jetbrains.kotlin.jvm") version ""
id("com.anatawa12.auto-visitor") 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:
```kotlin
autoVisitor(some_expr) { variable ->
when (variable) {
is SomeClass -> {
statements
}
else -> {
statements
}
}
}
```
## Status of implementation
- [x] Automatically include this library to classpath in gradle plugin
- [x] Generating visitor and accept method
- [x] Generating visitor abstract class
- [x] Generating accept method
- [x] Provide Compilation Error
- [x] Generating calling accept from when
- [x] Generating calling accept from when
- [x] Provide Compilation Error
## Structure of this project
- [compiler-plugin](./compiler-plugin)
The compiler plugin of Kotlin.
- [gradle-plugin](./gradle-plugin)
The gradle plugin. This includes applying Kotlin compiler plugin, applying Annotation Processor.
- [annotation-processor](./annotation-processor)
The pluggable annotation processor to verify annotation usages from java.
- [annotation-value-gen](./annotation-value-gen)
A pluggable annotation processor for the compiler plugin. See readme in it for more details
## Motivation
Because the generated code of it is linear search,
`when` expr with `is` checking is much slower than visitor pattern
(see [benchmarks](./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.