Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

java - Annotation Processor Generation No Files Created (JavaPoet-AutoService)

I am trying to learn processor generation, I use JavaPoet and Google AutoService for that purpose. I compile my code with maven-compiler-plugin without any errors, generated sources folder appears but I can't see any generated file inside of it.

I also failed to find a way to read processingEnv.getMessager logs so I am completely stuck at the moment. Please help.

Here is my Processor:

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!");
    }
}

My folders after mvn compile:

enter image description here

question from:https://stackoverflow.com/questions/65602067/annotation-processor-generation-no-files-created-javapoet-autoservice

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...