処理するプロセッサと、メタ注釈が付けられた@FooEntity
注釈(この場合)を構成する方法はないと思います。代わりにできることは、任意の注釈()をサポートするプロセッサを用意し、処理する注釈を決定するために、プロセッサ自体にさらにロジックを実装することです。問題の私の理解に基づいたそのような実装は次のとおりです。@FooEntity
@Controller
@SupportedAnnotationTypes("*")
package acme.annotation.processing;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;
import acme.annotation.FooEntity;
@SupportedAnnotationTypes("*")
public class FooEntityExtendedProcessor extends AbstractProcessor {
private void log(String msg) {
System.out.println(msg);
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
log("Initially I was asked to process:" + annotations.toString());
Set<TypeElement> fooAnnotations = new HashSet<>();
for (TypeElement elem : annotations) {
if (isFoo(elem)) fooAnnotations.add(elem);
}
if (fooAnnotations.size() > 0) {
log("... but I am now going to process:" + fooAnnotations.toString());
processInternal(fooAnnotations, roundEnv);
} else {
log("... but none of those was my business!");
}
// always return false so that other processors get a chance to process the annotations not consumed here
return false;
}
private void processInternal(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// TODO: do your foo processing here
}
private boolean isFoo(TypeElement elem) {
if (elem.getQualifiedName().toString().equals("acme.annotation.FooEntity")
|| elem.getAnnotation(FooEntity.class) != null) {
return true;
} else {
return false;
}
}
}
サンプルを実行すると、次のようになります。
Initially I was asked to process:[acme.annotation.FooEntity, skriptor.annotation.ScriptArg, java.lang.Override, skriptor.annotation.ScriptCodeRT, skriptor.annotation.ScriptCode, skriptor.annotation.ScriptObject, javax.ws.rs.Path, javax.ws.rs.GET, acme.annotation.Controller, com.sun.jersey.spi.resource.Singleton, skriptor.annotation.ScriptImport, javax.ws.rs.ext.Provider, javax.ws.rs.Produces, javax.annotation.PostConstruct]
... but I am now going to process:[acme.annotation.Controller, acme.annotation.FooEntity]