Skip to content

Commit

Permalink
feature: update annotation processor
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqi committed Nov 16, 2024
1 parent f829e06 commit d30970e
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,52 @@

package cn.sliew.carp.support.annotation.processor;

import cn.sliew.carp.support.annotation.processor.plugins.jackson.javapoet.JacksonJavapoetPlugin;
import cn.sliew.carp.support.annotation.processor.plugins.web.WebPlugin;
import com.google.auto.common.BasicAnnotationProcessor;
import com.google.auto.service.AutoService;
import com.google.common.collect.Lists;
import com.palantir.javapoet.JavaFile;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* 参考 lombok.core.AnnotationProcessor
*
* @see BasicAnnotationProcessor
*/
@SupportedAnnotationTypes({"cn.sliew.carp.support.annotation.processor.CarpProcessor"})
@SupportedSourceVersion(SourceVersion.RELEASE_17)
@SupportedAnnotationTypes("*")
@AutoService(Processor.class)
public class CarpAnnotationProcessor extends AbstractProcessor {

private List<CarpProcessorPlugin> plugins = Lists.newArrayList();
private List<CarpProcessorPlugin> registered = Lists.newArrayList();
private List<CarpProcessorPlugin> active = Lists.newArrayList();

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
plugins.add(new WebPlugin());
plugins.add(new JacksonJavapoetPlugin());
plugins.forEach(plugin -> plugin.init(processingEnv));
registered.add(new WebPlugin());
active = registered.stream().filter(plugin -> plugin.support(processingEnv)).collect(Collectors.toList());
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
try {
processInternal(roundEnv);
processInternal(annotations, roundEnv);
} catch (Exception e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Exception occurred %s".formatted(e));
}
return false;
}

protected void processInternal(RoundEnvironment roundEnv) {
plugins.stream().map(plugin -> plugin.process(roundEnv.getElementsAnnotatedWith(plugin.supported())))
protected void processInternal(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
active.stream().map(plugin -> plugin.process(roundEnv.getElementsAnnotatedWith(plugin.supported()), roundEnv))
.flatMap(Collection::stream)
.forEach(this::writeFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@
import com.palantir.javapoet.JavaFile;

import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Set;

/**
* 参考 lombok.core.AnnotationProcessor.ProcessorDescriptor
* @see BasicAnnotationProcessor.Step
*/
public interface CarpProcessorPlugin {

void init(ProcessingEnvironment processingEnv);
boolean support(ProcessingEnvironment processingEnv);

Class<? extends Annotation> supported();

Collection<JavaFile> process(Set<? extends Element> annotated);
Collection<JavaFile> process(Set<? extends Element> annotated, RoundEnvironment roundEnv);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,30 @@

package cn.sliew.carp.support.annotation.processor.plugins;

import cn.sliew.carp.support.annotation.processor.util.ProcessingEnvUtils;
import com.palantir.javapoet.JavaFile;
import com.palantir.javapoet.TypeSpec;

import javax.lang.model.element.ExecutableElement;

/**
* 生成一个新的类,实现特定接口
* 生成一个新的类,实现特定接口。
* Annotation Processor 只能实现编译时获取特定的注解标记的类,
* 获取到之后生成新的类和修改已有的类已经超出了 Annotation Processor 的功能。
* 当需要新生成类时可以使用 javapoet 等工具,生成新的 .java 文件
* 修改已有的类则需要使用 javac,这是 java 的内部类。目前没有相关的库可以辅助生成,
* 做的比较好的也就是 lombok。参考:https://stackoverflow.com/a/70008734
*/
public abstract class AbstractClassGeneratePlugin extends AbstractProcessorPlugin {

@Override
protected JavaFile getJavaFile(ElementMethod elementMethod, IndexedValue<ExecutableElement> indexedValue) {
var annotedMethod = indexedValue.value();
var typeSpec = createType(elementMethod, indexedValue, annotedMethod);
String packageName = ProcessingEnvUtils.getPackageName(processingEnv, annotedMethod);
return JavaFile.builder(packageName, typeSpec).build();
}

protected abstract TypeSpec createType(ElementMethod elementMethod, IndexedValue<ExecutableElement> indexedValue, ExecutableElement handlerMethod);

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@
package cn.sliew.carp.support.annotation.processor.plugins;

import cn.sliew.carp.support.annotation.processor.CarpProcessorPlugin;
import cn.sliew.carp.support.annotation.processor.util.ProcessingEnvUtils;
import com.palantir.javapoet.JavaFile;
import com.palantir.javapoet.TypeSpec;

import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.util.ElementFilter;
import javax.tools.Diagnostic;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand All @@ -46,12 +46,15 @@ public abstract class AbstractProcessorPlugin implements CarpProcessorPlugin {
protected ProcessingEnvironment processingEnv;

@Override
public void init(ProcessingEnvironment processingEnv) {
public boolean support(ProcessingEnvironment processingEnv) {
this.processingEnv = processingEnv;
return false;
}

public abstract Class<? extends Annotation> supported();

@Override
public Collection<JavaFile> process(Set<? extends Element> annotated) {
public Collection<JavaFile> process(Set<? extends Element> annotated, RoundEnvironment roundEnv) {
Map<ElementMethod, List<ExecutableElement>> nameDataListMap = ElementFilter
.methodsIn(annotated)
.stream()
Expand Down Expand Up @@ -80,14 +83,7 @@ private JavaFile handle(ElementMethod elementMethod, IndexedValue<ExecutableElem
}
}

private JavaFile getJavaFile(ElementMethod elementMethod, IndexedValue<ExecutableElement> indexedValue) {
var handlerMethod = indexedValue.value();
var typeSpec = createType(elementMethod, indexedValue, handlerMethod);
String packageName = ProcessingEnvUtils.getPackageName(processingEnv, handlerMethod);
return JavaFile.builder(packageName, typeSpec).build();
}

protected abstract TypeSpec createType(ElementMethod elementMethod, IndexedValue<ExecutableElement> indexedValue, ExecutableElement handlerMethod);
protected abstract JavaFile getJavaFile(ElementMethod elementMethod, IndexedValue<ExecutableElement> indexedValue);

protected void log(String msg) {
if (processingEnv.getOptions().containsKey("debug")) {
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit d30970e

Please sign in to comment.