auto-visitor

A kotlin compiler plugin to make easy to write visitor pattern.

MIT License

Stars
3

Auto Visitor Kotlin Compiler Plugin

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.
  2. Generate accept method and visitor abstract class from annotations.

How to use

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
        }
    }
}

Status of implementation

  • Automatically include this library to classpath in gradle plugin
  • Generating visitor and accept method
    • Generating visitor abstract class
    • Generating accept method
    • Provide Compilation Error
  • Generating calling accept from when
    • Generating calling accept from when
    • Provide Compilation Error

Structure of this project

  • compiler-plugin

    The compiler plugin of Kotlin.

  • gradle-plugin

    The gradle plugin. This includes applying Kotlin compiler plugin, applying Annotation Processor.

  • annotation-processor

    The pluggable annotation processor to verify annotation usages from java.

  • 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), 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.

Package Rankings
Top 32.02% on Repo1.maven.org
Badges
Extracted from project README
a12 maintenance: Slowly Gradle Plugin Portal