0

Project 1 (annotation project):

build.gradle

plugins {
    id 'java'
}

apply plugin: 'java'

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    annotationProcessor 'com.google.auto.service:auto-service:1.0.1'
    implementation 'com.google.auto.service:auto-service:1.0.1'

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}

test {
    useJUnitPlatform()
}

Annotation

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
    String value();
    String[] ignoreColumns() default {};
}

Processor

@AutoService(TableProcessor.class)
@SupportedAnnotationTypes("com.github.ahuangJM.annotations.Table")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class TableProcessor extends AbstractProcessor {
    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        System.out.println("init hit!");
        super.init(processingEnv);
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        final Messager messager = processingEnv.getMessager();
        messager.printMessage(Diagnostic.Kind.NOTE, "Processing...");
        return true;
    }
}

\resources\META-INF\javax.annotation.processing.Processor

org.example.processors.TableProcessor

Project 2 (annotation project consumer):

Annotation Consumer

@Table("user-info")
public class UserInfo {
}

build.gradle

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation files("REDACTED\\code-generator-annotation.jar")
    annotationProcessor files("REDACTED\\code-generator-annotation.jar")

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}

test {
    useJUnitPlatform()
}

This should be a very simple annotation processor just printing debugging statements. This all seems correct to me, but process() is not running when I build/run project 2. I have tried overriding getSupportedVersion() and getSupportedAnnotationTypes() as well. That didn't do anything. Also tried without @AutoService, that didn't change anything as well.

EDIT: why am i can getting the print() and/or printMessage() statement?

2
  • Is your question is, why you are not seeing the print statement in init & process methods? Commented Dec 10, 2021 at 15:48
  • Yup! Mostly why am I not getting the printMessage(). Commented Dec 10, 2021 at 16:09

3 Answers 3

1
@AutoService(TableProcessor.class)

should be

@AutoService(Processor.class)
Sign up to request clarification or add additional context in comments.

Comments

0
@AutoService(TableProcessor.class)

should be

@AutoService(AbstractProcessor.class) or @AutoService(Processor.class)

@autoservice(X) means you implement X, but i see a class or interface Processor in your question, so you have to be sure what implement what but your @autoservice first is wrong.

Comments

-1

@Retention(RetentionPolicy.RUNTIME)

should be

@Retention(RetentionPolicy.SOURCE)

3 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
This is won't change anything. This can only break a program rather than fix anything.
Retention is rather like access modifiers, telling someone to make a field public is inconsequential, but telling them to make it private may have consequences. Runtime retention also means source retention, but not vise-versa.

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.