我正在尝试学习处理器生成,为此我使用javapoet和googleautoservice。我用maven编译器插件编译代码时没有任何错误,generatedsources文件夹出现了,但我看不到里面有任何生成的文件。
我也没有找到阅读processingenv.getmessager日志的方法,所以我现在完全陷入了困境。请帮忙。
这是我的处理器:
package com.annotationprocessing.demo;
import com.annotationprocessing.demo.annotation.NewLoginApplicable;
import com.google.auto.service.AutoService;
import com.squareup.javapoet.*;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
@SupportedAnnotationTypes("com.annotationprocessing.demo.annotation.NewLoginApplicable.java")
@AutoService(Processor.class)
public class NewLoginInterceptorProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
// The elements which have NewLoginApplicable annotation
Set<? extends Element> annotatedElements = roundEnv
.getElementsAnnotatedWith(NewLoginApplicable.class);
Map<Boolean, List<Element>> annotatedMethods = annotatedElements.stream().collect(
Collectors.partitioningBy(element -> element.getKind() == ElementKind.METHOD));
Set<Element> annotatedCorrectly = new HashSet<>(annotatedMethods.get(true));
List<Element> faulty = annotatedMethods.get(false);
faulty.forEach(element -> processingEnv.getMessager().printMessage(
Diagnostic.Kind.ERROR,
"NewLoginApplicable annotation should only be used on methods ",
element));
String className = "NewLoginApplicableMap";
TypeSpec typeSpec = buildClassTypeSpec(annotatedCorrectly, className);
String packageName = "com.annotationprocessing.demo";
if (typeSpec != null) {
try {
writeToFile(typeSpec, packageName);
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
private static TypeSpec buildClassTypeSpec(Set<? extends Element> elements, String className) {
if (elements.isEmpty())
return null;
ClassName hashmap = ClassName.get("java.util", "HashMap");
String hashmapObjectName = "getEndpointMap";
TypeSpec.Builder typeSpecBuilder = TypeSpec.classBuilder(className)
.addModifiers(Modifier.PUBLIC);
FieldSpec fieldSpec = FieldSpec.builder(HashMap.class, hashmapObjectName,
Modifier.PRIVATE, Modifier.STATIC).initializer("new HashMap()").build();
typeSpecBuilder.addField(fieldSpec);
MethodSpec.Builder getterMethodBuilder = MethodSpec.methodBuilder(hashmapObjectName)
.returns(HashMap.class)
.addModifiers(Modifier.PUBLIC)
.addModifiers(Modifier.STATIC)
.addStatement("return this.endpointMap()");
typeSpecBuilder.addMethod(getterMethodBuilder.build());
return typeSpecBuilder.build();
}
private void writeToFile(TypeSpec typeSpec, String packageName) throws IOException {
JavaFile.builder(packageName, typeSpec).build().writeTo(processingEnv.getFiler());
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Code generation complete!");
}
}
mvn编译后的我的文件夹:
暂无答案!
目前还没有任何答案,快来回答吧!