[JAVA] Für SOURCE-Annotationen ist möglicherweise keine Aufbewahrung verfügbar, wenn die Annotationsverarbeitung in der IDE ausgeführt wird

Beim Ausführen der Anmerkungsverarbeitung in der IDE werden die durch "@Retention (RetentionPolicy.SOURCE)" definierten Anmerkungsinformationen unter bestimmten Bedingungen möglicherweise nicht abgerufen.

Umgebung

OS

Windows 10

Java

openjdk 10.0.2

Eclipse

Photon (4.8.0)Pleiades

IntelliJ

IntelliJ IDEA 2018.2.3 (Community Edition)

Gradle

Gradle 4.10

Symptome

Quellcode

Projektstruktur


|-foo/
| `-src/main/java/
|   `-sample/foo/
|     |-Fizz.java
|     `-Buzz.java
|
|-bar/
| |-src/main/java/
| | `-sample/bar/
| |   |-MyRuntimeRetention.java
| |   |-MyClassRetention.java
| |   |-MySourceRetention.java
| |   `-MyProcessor.java
| |
| `-src/main/resources/
|   `-META-INF/services
|     `-javax.annotation.processing.Processor
|
|-build.gradle
`-settings.gradle

--Multi-Projekt von foo, bar

MyRuntimeRetention.java


package sample.bar;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyRuntimeRetention {
}

MyClassRetention.java


package sample.bar;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface MyClassRetention {
}

MySourceRetention.java


package sample.bar;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface MySourceRetention {
}

MyProcessor.java


package sample.bar;

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic.Kind;

public class MyProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        Elements elements = this.processingEnv.getElementUtils();
        
        if (!annotations.isEmpty()) {
            this.printAnnotations(elements.getTypeElement("sample.foo.Fizz"));
            this.printAnnotations(elements.getTypeElement("sample.foo.Buzz"));
        }
        
        return true;
    }
    
    private void printAnnotations(TypeElement element) {
        element.getAnnotationMirrors().forEach(annotation -> {
            this.processingEnv.getMessager().printMessage(Kind.NOTE, element.getSimpleName() + " : " + annotation);
        });
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.RELEASE_10;
    }
    
    @Override
    public Set<String> getSupportedAnnotationTypes() {
        return Set.of(MySourceRetention.class.getCanonicalName());
    }
}

Fizz.java


package sample.foo;

import sample.bar.MyClassRetention;
import sample.bar.MyRuntimeRetention;
import sample.bar.MySourceRetention;

@MyRuntimeRetention
@MyClassRetention
@MySourceRetention
public class Fizz {
}

Buzz.java


package sample.foo;

import sample.bar.MyClassRetention;
import sample.bar.MyRuntimeRetention;
import sample.bar.MySourceRetention;

@MyRuntimeRetention
@MyClassRetention
@MySourceRetention
public class Buzz {
}

Verarbeitung durchgeführt werden

Ändern Sie in diesem Zustand nur die Klasse ** Fizz und führen Sie die ** Kompilierung aus. Das Ergebnis ist dann anders, wenn es in Gradle und in IDE (Eclipse, IntelliJ) ausgeführt wird.

Beim Laufen auf Gradle

> gradle :foo:compileJava

Hinweis:Fizz : @sample.bar.MyRuntimeRetention
Hinweis:Fizz : @sample.bar.MyClassRetention
Hinweis:Fizz : @sample.bar.MySourceRetention
Hinweis:Buzz : @sample.bar.MyRuntimeRetention
Hinweis:Buzz : @sample.bar.MyClassRetention
Hinweis:Buzz : @sample.bar.MySourceRetention

Beim Ausführen in Eclipse

apt.jpg

Beim Ausführen auf IntelliJ

apt.jpg

Sie können es auch in der IDE erhalten, indem Sie Buzz ändern

Eclipse apt.jpg

IntelliJ apt.jpg

――Das Gleiche gilt hier, und Sie können @ MySourceRetention erhalten.

Rate, was passiert ist

--Wenn "Aufbewahrung" auf "QUELLE" gesetzt ist, verbleiben keine Anmerkungsinformationen in der Klassendatei /RetentionPolicy.html#SOURCE)

Schlussfolgerung (Vermutung)

Recommended Posts

Für SOURCE-Annotationen ist möglicherweise keine Aufbewahrung verfügbar, wenn die Annotationsverarbeitung in der IDE ausgeführt wird
Wenn das Projekt nicht in Eclipse angezeigt wird
Verarbeitung, wenn eine ID, die nicht in der Datenbank vorhanden ist, in die URL eingegeben wird
Festlegen, wann in junit "Der Konstruktor Empty () ist nicht sichtbar" auftritt
Wenn ich den Quellbefehl im Makefile von Ubuntu ausführe, erhalte ich "source: Befehl nicht gefunden".