tbroyer / gradle-incap-helper

Helper library and annotation processor for building incremental annotation processors
Apache License 2.0
148 stars 12 forks source link

gradle-incap-helper

Helper library and annotation processor for building incremental annotation processors

Gradle 4.7 came (in April 2018) with some level of incremental annotation processing support. Gradle 4.8 went farther by making it possibly dynamic.

This library and annotation processor helps you generate the META-INF descriptor, and return the appropriate value from your processor's getSupportedOptions() if it's dynamic.

Usage

  1. Add the incap library to your compile-time dependencies, and the incap-processor to your annotation processor path:

    with Gradle ```gradle dependencies { // you can use compileOnlyApi (or even compileApi) if you're only using isolating or aggregating processors (i.e. no dynamic processor) implementation("net.ltgt.gradle.incap:incap:${incap.version}") annotationProcessor("net.ltgt.gradle.incap:incap-processor:${incap.version}") } ```
    with Maven ```xml net.ltgt.gradle.incap incap ${incap.version} org.apache.maven.plugins maven-compiler-plugin net.ltgt.gradle.incap incap-processor ${incap.version} ```
  2. Annotate your annotation processor with @IncrementalAnnotationProcessor

    @IncrementalAnnotationProcessor(ISOLATING)
    public class MyProcessor extends AbstractProcessor {
  3. If the choice of incrementality support is dynamic (i.e. you used the DYNAMIC value above), use the IncrementalAnnotationProcessorType enumeration's getProcessorOption() from your getSupportedOptions() to get the appropriate constant.

    @Override
    public Set<String> getSupportedOptions() {
       if (someCondition) {
           return Collections.singleton(ISOLATING.getProcessorOption());
       } else {
           // Non-incremental
           return Collections.emptySet();
       }
    }

Acknowledgements

This processor works great with @AutoService, which also inspired some of the code here.

Shout-out Gradle, Inc. (and Groupon) who built this feature.