我编写了一个简单的注释处理器,它创建了一个hello world终结点:
@SupportedAnnotationTypes("br.com.govbr.service.annotations.Multitenancy")
@AutoService(Processor.class)
public final class MultitenancyAnnotationProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations,RoundEnvironment roundEnv) {
if (!validateAnnotationUsage(roundEnv))
return false;
if(!roundEnv.getElementsAnnotatedWith(Multitenancy.class).isEmpty())
generateCode();
return false;
}
@Override
public Set<String> getSupportedAnnotationTypes() {
Set<String> annotations = new LinkedHashSet<>();
annotations.add(Multitenancy.class.getcanonicalName());
return annotations;
}
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
private boolean validateAnnotationUsage(RoundEnvironment roundEnv) {
return true;
}
private void generateCode() throws FileGenerationException{
MethodSpec getNoticias = MethodSpec.methodBuilder("getHelloWorld")
.addmodifiers(Modifier.PUBLIC)
.returns(String.class)
.addAnnotation(AnnotationSpec.builder(GET.class).build())
.addStatement("return \"Hello World\"")
.build();
TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
.addAnnotation(AnnotationSpec.builder(Path.class).addMember("value","\"hello\"").build())
.addmodifiers(Modifier.PUBLIC)
.addMethod(getNoticias)
.build();
JavaFile javaFile = JavaFile.builder("br.com.govbr.generated",helloWorld).build();
try {
javaFile.writeTo(processingEnv.getFiler());
} catch (IOException e) {
final String errorMessage = "Fail to generate class HelloWorld";
super.processingEnv.getMessager().printMessage(Kind.WARNING,errorMessage);
throw new FileGenerationException(errorMessage);
}
}
}
我在 quarkus 项目中使用了此实现,并通过maven进行了导入,我尝试添加 optional 标志以查看是否有任何更改。在检查是否生成了任何代码之后,我找不到任何证据,有人可以帮助我了解为什么处理器没有运行吗?