简体   繁体   中英

Java annotation processor ignores annotation in parent class

I've got the classes below. Two annotations ( AnnotA and AnnotB ), a class ' Child .java' (with @AnnotA) and its' parent ' Base .java' (with @AnnotB).

When compiling Child.java, my annotation processor repors AnnotA but it doesn't report the annotations ( AnnotB ) found in Base.java.

AnnotA.java

import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface AnnotA
    {
    }

AnnotB.java

import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface AnnotB
    {
    }

Base.java

@AnnotB
public class Base
    {
    }

Child.java

@AnnotA
public class Child extends Base
    {
    }

MyProc.java

import javax.annotation.processing.*;
import java.lang.annotation.*;
import javax.lang.model.*;
import javax.lang.model.element.*;
import javax.tools.*;
import java.util.*;
import java.util.stream.*;

@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class MyProc  extends AbstractProcessor
    {
    @Override
    public Set<String> getSupportedAnnotationTypes()  {
        final Set<String> set = new HashSet<>();
        set.add("AnnotA");
        set.add("AnnotB");
        return set;
        }
    @Override
    public boolean process(final Set<? extends TypeElement> annotations,
        final RoundEnvironment roundEnv
        ) {
        roundEnv.getElementsAnnotatedWith(AnnotA.class).stream().
            forEach(E->{System.err.println("AnnotA>>" + E + " "+ E.getAnnotation(AnnotA.class));});

        roundEnv.getElementsAnnotatedWith(AnnotB.class).stream().
            forEach(E->{System.err.println("AnnotB>>" + E + " "+ E.getAnnotation(AnnotB.class));});
        return true;
        }
    }

Here is the compilation process and it's output, as your can see, there is no message about AnnotB while Parent.java is a parent of Child.java

rm -rf tmp
mkdir -p tmp/META-INF/services
javac -d tmp MyProc.java
echo "MyProc" > tmp/META-INF/services/javax.annotation.processing.Processor
jar cvf myproc.jar -C tmp .
added manifest
ignoring entry META-INF/
adding: META-INF/services/(in = 0) (out= 0)(stored 0%)
adding: META-INF/services/javax.annotation.processing.Processor(in = 7) (out= 9)(deflated -28%)
adding: AnnotB.class(in = 363) (out= 221)(deflated 39%)
adding: MyProc.class(in = 2512) (out= 1118)(deflated 55%)
adding: AnnotA.class(in = 363) (out= 221)(deflated 39%)
##
mkdir -p tmp
javac -processorpath myproc.jar -d tmp Child.java
AnnotA>>Child @AnnotA()
warning: Implicitly compiled files were not subject to annotation processing.
  Use -implicit to specify a policy for implicit compilation.
1 warning
rm -rf tmp

what's wrong with this code ? Thanks .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM