AbstractProcessor使用

.class

import com.lianyu.framework.logs.integration.annotion.AutoLog;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@SupportedAnnotationTypes("com.lianyu.framework.logs.integration.annotion.AutoLog") // Replace with your package name
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class LogMethodDataProcessor extends AbstractProcessor {
    private Messager messager;
    private Elements elementUtils;
    private Types typeUtils;
    List<String> validAnnotations = new ArrayList<>();

    LogMethodDataProcessor() {
        validAnnotations.add("org.springframework.web.bind.annotation.RestController");
        validAnnotations.add("org.springframework.stereotype.Controller");
        validAnnotations.add("org.springframework.web.bind.annotation.RestController");
        validAnnotations.add("org.springframework.web.bind.annotation.RestController");
    }


    @Override
    public synchronized void init(ProcessingEnvironment processingEnvironment) {
        super.init(processingEnvironment);
        messager = processingEnv.getMessager();
        elementUtils = processingEnv.getElementUtils();
        typeUtils = processingEnv.getTypeUtils();
        messager.printMessage(Diagnostic.Kind.WARNING, "@AutoLog annotation init!");
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment rEnv) {
        Iterator<? extends Element> iterator = rEnv.getRootElements().iterator();
        while (iterator.hasNext()){
            Element element = iterator.next();
            List<? extends AnnotationMirror> ams = element.getAnnotationMirrors();
            List<String> annotationNames = ams.stream()
                    .map(am -> am.getAnnotationType().toString())
                    .collect(Collectors.toList());
        }
        messager.printMessage(Diagnostic.Kind.WARNING, "@AutoLog annotation init!");
        if (annotations == null || annotations.isEmpty()) {
            messager.printMessage(Diagnostic.Kind.WARNING, "" + annotations.size());
            return true;
        }
        Set<? extends Element> elements = rEnv.getElementsAnnotatedWith(AutoLog.class);
        for (Element element : elements) {
            messager.printMessage(Diagnostic.Kind.WARNING, "" + (element.getKind() != ElementKind.METHOD));
            // @AutoLog annotation must be used by Method
            if (element.getKind() != ElementKind.METHOD) {
                messager.printMessage(
                        Diagnostic.Kind.ERROR,
                        "@AutoLog annotation is only support methods!",
                        element);
                return false;
            }
        }
        return true;
    }
}

javax.annotation.processing.Processor

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactGroup>com.test</artifactGroup>
    <artifactId>processor</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <configuration>
                            <compilerArgument>-proc:none</compilerArgument>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容