2

I am currently trying to implement a small fun library, that auto-generates fixture classes on compile time by using anootation processing: https://github.com/floydkretschmar/fixturize/tree/main/fixturize

The project structure is quite a simple mono-repo

|-- fixturize
    |
    |--fixturize-core
    |--fixturize-playground

fixturize-core defines both the relevant annotations as well as the annotation processor:

@SupportedAnnotationTypes("de.floydkretschmar.fixturize.annotations.Fixture")
@SupportedSourceVersion(SourceVersion.RELEASE_21)
@AutoService(AbstractProcessor.class)
public class FixtureProcessor extends AbstractProcessor {
...
}

As you can see I am using auto-service to generate META-INF/services/javax.annotation.processing.Processor which seems to work. At least the file exists as part of the generated classes. This is the build.gradle file I am using for fixturize-core

plugins {
    id "java"
    id "io.freefair.lombok" version "8.6"
}

ext {
    nashornVersion = "15.4"
    guavaVersion = "33.2.1-jre"
    autoServiceVersion = "1.1.1"

    junitVersion = "5.10.0"
    assertJVersion = "3.25.1"
    mockitoVersion = "5.12.0"
    compileTestingVersion = "0.21.0"
    lombokVersion = "1.18.34"
}

dependencies {
    implementation "com.google.auto.service:auto-service:$autoServiceVersion"
    annotationProcessor "com.google.auto.service:auto-service:$autoServiceVersion"

    implementation "com.google.guava:guava:$guavaVersion"
    implementation "org.openjdk.nashorn:nashorn-core:$nashornVersion"

    testImplementation platform("org.junit:junit-bom:$junitVersion")
    testImplementation "org.junit.jupiter:junit-jupiter"
    testImplementation "org.assertj:assertj-core:$assertJVersion"
    testImplementation "org.mockito:mockito-junit-jupiter:$mockitoVersion"
    testImplementation "com.google.testing.compile:compile-testing:$compileTestingVersion"
    testImplementation "org.projectlombok:lombok:$lombokVersion"
}

test {
    useJUnitPlatform()
}

Now in fixturize-playground I want to use the the library in the exact way, I would use it in any other project: import it, register the annotation processor and then get autogenerated fixture classes for all domain objects annotated with @Fixture. So this is what my build.gradle looks like:

plugins {
    id "java"
    id "io.freefair.lombok" version "8.6"
}

tasks.withType(JavaCompile) {
    doFirst {
        println "AnnotationProcessorPath for $name is ${options.getAnnotationProcessorPath().getFiles()}"
    }
}

compileJava {
    options.annotationProcessorPath = configurations.annotationProcessor
}

dependencies {
    implementation project(":fixturize-core")
    annotationProcessor project(":fixturize-core")

    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
    useJUnitPlatform()
}

And my annotated class:

import de.floydkretschmar.fixturize.annotations.Fixture;

@Fixture
public class Test {
    private int field;
}

When running ./gradlew clean build no class gets generated and the log in the compile task shows that my custom annotation processor is not part of the annoation processor path. I am at a bit of a loss, and i was hoping someone has an idea, what i am doing wrong.

2
  • 1
    What happens if you try @AutoService(Processor.class) instead? The Processor interface is the SPI, not AbstractProcessor (a convenience class). Commented Jul 22, 2024 at 3:17
  • Wow, this actually woked. I am a bit confused as to why, because I had used @AutoService(Processor.class) before but it was not working. But I guess at that point i had not yet added annotationProcessor project(":fixturize-core") to the build.gradle of playground. Anyway changing the annotation to @AutoService(Processor.class) did work. Thanks! Commented Jul 22, 2024 at 8:07

1 Answer 1

2

The solution to the problem was as slaw proposed in his comment: @AutoService(AbstractProcessor.class) in the FixtureProcessor class needed to be @AutoService(Processor.class) instead. Then the generation started working.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.