Skip to content

Easy creation and validation of annotation processors

License

LGPL-2.1, GPL-2.0 licenses found

Licenses found

LGPL-2.1
COPYING.LESSER
GPL-2.0
COPYING
Notifications You must be signed in to change notification settings

nkaaf/AnnotationProcessor

Repository files navigation

Annotation Processor

License

DeepSource Build Status

GitHub all releases

Maven Central

GitHub forks GitHub Repo stars GitHub watchers

Index

Use Case

In case you want to create a new annotation processor, you can use the annotation @AnnotationProcessor, to automatically create the required javax.annotation.processing.Processor file in the META-INF/services/ directory at compile time.

❗ To use this properly, see how to import it into your Build System and integrate it into Java

↑ Back to Index

Build System

Maven

<dependency>
    <groupId>io.github.nkaaf</groupId>
    <artifactId>annotationprocessor</artifactId>
    <version>1.0</version>
</dependency>

↑ Back to Index

Gradle

plugins {
    id 'java-library'
}

dependencies {
    compileOnly 'io.github.nkaaf:annotationprocessor:1.0'
    annotationProcessor 'io.github.nkaaf:annotationprocessor:1.0'
}

↑ Back to Index

Java

Annotate your annotation processor with @AnnotationProcessor. The processor behind this annotation checks if your annotation processor is built conforming (JSP 269). You can either extend your processor with javax.annotation.processing.AbstractProcessor or directly implement it with javax.annotation.processing.Processor.

↑ Back to Index

Non-Modular

You only need to import the dependency with your build system.

Maven Example

Gradle Example

↑ Back to Index

Modular (Java 9+)

You need to add the dependencies' module in the module-info.java of the desired module. In addition, you need to set it up in your build system (Maven/ Gradle).

↑ Back to Index

Maven

Your module only needs to require the io.github.nkaaf.annotationprocessor module. It gets the module java.compiler automatically, so you don't need it additionally for your annotation processor.

module yourModule {
    requires static io.github.nkaaf.annotationprocessor;
}

You need to add the dependency as an annotation path in maven-compiler-plugin.

<pluin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <release>JAVA_VERSION_GREATER_OR_EQUALS_9</release>
        <annotationProcessorPaths>
            <annotationProcessorPath>
                <groupId>io.github.nkaaf</groupId>
                <artifactId>annotationprocessor</artifactId>
                <version>1.0</version>
            </annotationProcessorPath>
        </annotationProcessorPaths>
    </configuration>
</pluin>

Maven Example

↑ Back to Index

Gradle (6.4+)

Your module must require the io.github.nkaaf.annotationprocessor module.

❗ It will NOT automatically get the java.compiler module (different behaviour than in Maven) ❗

module yourModule {
    requires static io.github.nkaaf.annotationprocessor;
}

You also need to turn on module path inference and add the release flag.

❗ You cannot use JDK 9 because the release flag causes an error with Gradle. JDK 10 causes another error with Gradle, but the Java 10 release flag can be used with JDK 12 and later. See the error tickets linked in example. But you can use the release flag 11, and a JDK 11 ❗

java {
    modularity.inferModulePath.set(true)
}

// < Gradle 6.6
compileJava {
    options.compilerArgs.addAll(['--release', '11'])
}

// >= Gradle 6.6
compileJava {
    options.release.set(11)
}

Gradle Example

↑ Back to Index

Developing

To ensure maximum compatibility, this project must be compiled with Java 9.

↑ Back to Index

The Problem with Multi-Release JARs and IDEs

Most IDEs do not support multi-release jars properly. The problem is that the package and class names are identical. The IDEs cannot compile them, even though this mechanism is clearly defined in the Maven POM.

↑ Back to Index

Testing

There is also a problem with the tests. There is no automatic way to change the JDKs, so that both versions of my annotation processor are tested. My solution is a bash script that compiles and tests the classes. It uses SDKMAN! and the Junit Jupiter Engine, which is imported by Maven. You can easily run the test script with bash from anywhere on your computer. If you do not have the required Java Libraries installed in the Maven folder, the script downloads them. This also applies to the JDKs with SDKMAN!. The mechanism for switching JDKs with SDKMAN! is not perfect for my purpose, because it depends on hardcoded Java versions. These can be deleted at any time in the lists of SDKMAN! without me notice it.

↑ Back to Index

Needed Components

↑ Back to Index

Used SDKMAN! JDKs

  • 8.0.282-zulu
  • 11.0.10-zulu
  • 15.0.2-sapmchn
  • 16.0.1-zulu

Also tested with following, at the moment not available, SDKMAN! JDKs:

  • 12.0.2-sapmchn
  • 13.0.2-sapmchn
  • 14.0.2-sapmchn
  • 15.0.2-zulu

↑ Back to Index

Used SDKMAN! Maven

  • 3.8.1

Also tested with following SDKMAN! Maven Versions:

  • 3.6.3

↑ Back to Index

Used Java Libraries

↑ Back to Index

License

This Project is licensed under the GNU Lesser General Public License 2.1 or any later (LGPL 2.1).

Licenses of used Libraries and Tools

This list includes only Libraries and tools that are explicit imported/used in this project.

↑ Back to Index