0

I'm trying to deal with annotation processors. I followed tutorials. Here is my code:

@ExampleAnnotation
private void someMethod(){
    System.out.println("hi");
}
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.METHOD)
public @interface ExampleAnnotation {

}
@SupportedAnnotationTypes("org.example.ExampleAnnotation")
public class Processor extends AbstractProcessor {
    @Override
    public boolean process(Set<? extends TypeElement> anots, RoundEnvironment roundEnvironment) {
        anots.forEach(System.out::println);
        return true;
    }
}

I created META-INF/SERVICES/javax.annotation.processing.Processor and registered my processor: org.example.Processor. It seems like everything is OK, but block of code in the processor just dont start. I have no idea what is wrong. P.S.: I use Gradle and Java 11.

Bluecross
  • 25
  • 5
  • The content of the file should be the name of _your_ processor, not `javax.annotation.processing.Processor`. That should be the name of the file, but you already have that. – Rob Spoor Nov 25 '22 at 10:19
  • What tutorial? What do you expect? What do is actual happening? And how do you execute your code above to get the result you expect? – jmizv Nov 25 '22 at 10:27
  • jmizv, all tutorials are almost same i tried this: https://www.baeldung.com/java-annotation-processing-builder. result i excpect is to print every annotated method to system.out – Bluecross Nov 25 '22 at 10:47
  • Looking at that link, it seems you are at least missing the `@SupportedSourceVersion` annotation. Also, please provide a [mre]. – Mark Rotteveel Nov 25 '22 at 11:38
  • 1
    `System.out` doesn't always behave as you might expect, instead use the Messager class. – Colin Alworth Nov 25 '22 at 23:22
  • Mark, here is example: https://github.com/Blu3cr0ss/JAPIssue @SupportedSourceVersion dont help – Bluecross Nov 28 '22 at 07:17
  • Colin, how should i use it? Its an interface – Bluecross Nov 28 '22 at 07:17
  • This question lacks too much information to be adequately answered. "It seems like everything is OK, but it isn't" — very poor problem statement. Have you tried running your processor with javac (e.g without Gradle)? Did you use `annotationProcessor` dependency to add it to one of your projects? What is the actual issue you are facing? Can you attach logs? Btw, using `System.out` does not guarantee that you will see output in Gradle console, at least use System.err for that: https://stackoverflow.com/q/29470860/1643723 – user1643723 Nov 29 '22 at 04:31
  • user1643723, i edited my question, i think now its better.I also tried Messager(as Colin said) but its dont work too.also, can you tell me more about `annotationProcessor` in gradle? – Bluecross Nov 29 '22 at 05:40
  • By default java compiler runs all processors specified in the jar files on compilation classpath. This used to be very convenient. Unfortunately, Gradle developers, in their quest for faster incremental compilation, diverged from that behavior. When you build a project with Gradle, you need to specify all processors in special way: https://docs.gradle.org/4.6/release-notes.html#convenient-declaration-of-annotation-processor-dependencies – user1643723 Nov 29 '22 at 06:45

1 Answers1

0

i fixed my issue and decided to make a little step-by-step tutorial to create simple JAP:

Java-Annotation-Processor-guide

This is guide for annotation processing using gradle

I spent 3 days trying to deal with it, but with this little tutorial you will made JAP in 5 minutes.

sorry for bad english :)

So, first you should do is create gradle subproject for your project:

  1. create project itself with gradle init or intellij
  2. add to your main project's settings.gradle file this line include "*your subproject name, lets say:*annotation-processor"

Congrats, now let go to the processor itself

  1. here is so much tutorials on the web about this part, so you can read something like this https://www.baeldung.com/java-annotation-processing-builde,
    or this (Если ты русский): https://habr.com/ru/company/e-legion/blog/206208/

Final step is very easy - you will add your annotation processor to main project.

!!! instead of annotation-processor you should use your subproject name !!!

kotlin (or java + kotlin) -> {
add this plugin to your plugins: id "org.jetbrains.kotlin.kapt" version "1.7.21"
add this to your dependencies:
kapt project('annotation-processor')
compileOnly project('annotation-processor')
}
java -> {
add this to your dependencies:
annotationProcessor project('annotation-processor')
compileOnly project('annotation-processor')
}
(or you can read this on github: https://github.com/Blu3cr0ss/Java-Annotation-Processor-guide)

Bluecross
  • 25
  • 5