update(REQ-2516) - 移除 workflow-auto-gen 功能
This commit is contained in:
parent
ada8eaa5b8
commit
48dad4b55a
@ -1,29 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<parent>
|
||||
<groupId>cn.axzo.workflow</groupId>
|
||||
<artifactId>workflow-engine</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>workflow-auto-gen</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>Workflow Auto Gen</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.auto.service</groupId>
|
||||
<artifactId>auto-service</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup</groupId>
|
||||
<artifactId>javapoet</artifactId>
|
||||
<version>1.13.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -1,16 +0,0 @@
|
||||
package cn.axzo.workflow.generate;
|
||||
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
|
||||
public abstract class BaseCodeGenProcessor implements CodeGenProcessor {
|
||||
|
||||
public static final String PREFIX = "Base";
|
||||
|
||||
@Override
|
||||
public GenCodeInfo generate(TypeElement typeElement, RoundEnvironment roundEnvironment)
|
||||
throws Exception {
|
||||
//添加其他逻辑扩展
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -1,31 +0,0 @@
|
||||
package cn.axzo.workflow.generate;
|
||||
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
public interface CodeGenProcessor {
|
||||
/**
|
||||
* 需要解析的类上的注解
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Class<? extends Annotation> getAnnotation();
|
||||
|
||||
/**
|
||||
* 获取生成的包名
|
||||
*
|
||||
* @param typeElement
|
||||
* @return
|
||||
*/
|
||||
String generatePackage(TypeElement typeElement);
|
||||
|
||||
/**
|
||||
* 代码生成逻辑.
|
||||
*
|
||||
* @param typeElement
|
||||
* @param roundEnvironment
|
||||
* @throws Exception
|
||||
*/
|
||||
GenCodeInfo generate(TypeElement typeElement, RoundEnvironment roundEnvironment) throws Exception;
|
||||
}
|
||||
@ -1,43 +0,0 @@
|
||||
package cn.axzo.workflow.generate;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.Set;
|
||||
|
||||
public class CodeGenProcessorRegistry {
|
||||
private static Map<String, ? extends CodeGenProcessor> PROCESSORS;
|
||||
|
||||
private CodeGenProcessorRegistry() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* 注解处理器要处理的注解集合
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Set<String> getSupportedAnnotations() {
|
||||
return PROCESSORS.keySet();
|
||||
}
|
||||
|
||||
public static CodeGenProcessor find(String annotationClassName) {
|
||||
return PROCESSORS.get(annotationClassName);
|
||||
}
|
||||
|
||||
/**
|
||||
* spi 加载所有的processor
|
||||
*
|
||||
*/
|
||||
public static void initProcessors() {
|
||||
final Map<String, CodeGenProcessor> map = Maps.newLinkedHashMap();
|
||||
ServiceLoader<CodeGenProcessor> processors = ServiceLoader.load(CodeGenProcessor.class, CodeGenProcessor.class.getClassLoader());
|
||||
for (CodeGenProcessor next : processors) {
|
||||
Class<? extends Annotation> annotation = next.getAnnotation();
|
||||
map.put(annotation.getName(), next);
|
||||
}
|
||||
PROCESSORS = map;
|
||||
}
|
||||
}
|
||||
@ -1,68 +0,0 @@
|
||||
package cn.axzo.workflow.generate;
|
||||
|
||||
import com.squareup.javapoet.TypeSpec;
|
||||
|
||||
public class GenCodeInfo {
|
||||
private final TypeSpec typeSpec;
|
||||
private final String sourcePath;
|
||||
private final String packageName;
|
||||
private final String className;
|
||||
|
||||
public static GenCodeInfoBuilder builder() {
|
||||
return new GenCodeInfoBuilder();
|
||||
}
|
||||
|
||||
public TypeSpec getTypeSpec() {
|
||||
return typeSpec;
|
||||
}
|
||||
|
||||
public String getSourcePath() {
|
||||
return sourcePath;
|
||||
}
|
||||
|
||||
public String getPackageName() {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
public String getClassName() {
|
||||
return className;
|
||||
}
|
||||
|
||||
private GenCodeInfo(GenCodeInfoBuilder builder) {
|
||||
this.typeSpec = builder.typeSpec;
|
||||
this.sourcePath = builder.sourcePath;
|
||||
this.packageName = builder.packageName;
|
||||
this.className = builder.className;
|
||||
}
|
||||
|
||||
public static class GenCodeInfoBuilder {
|
||||
private TypeSpec typeSpec;
|
||||
private String sourcePath;
|
||||
private String packageName;
|
||||
private String className;
|
||||
|
||||
public GenCodeInfoBuilder typeSpec(TypeSpec typeSpec) {
|
||||
this.typeSpec = typeSpec;
|
||||
return this;
|
||||
}
|
||||
|
||||
public GenCodeInfoBuilder sourcePath(String sourcePath) {
|
||||
this.sourcePath = sourcePath;
|
||||
return this;
|
||||
}
|
||||
|
||||
public GenCodeInfoBuilder packageName(String packageName) {
|
||||
this.packageName = packageName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public GenCodeInfoBuilder className(String className) {
|
||||
this.className = className;
|
||||
return this;
|
||||
}
|
||||
|
||||
public GenCodeInfo build() {
|
||||
return new GenCodeInfo(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,45 +0,0 @@
|
||||
package cn.axzo.workflow.generate;
|
||||
|
||||
import com.squareup.javapoet.JavaFile;
|
||||
import com.squareup.javapoet.MethodSpec;
|
||||
import com.squareup.javapoet.TypeSpec;
|
||||
|
||||
import javax.lang.model.element.Modifier;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
|
||||
public class GenJavaSourceFileUtils {
|
||||
|
||||
public static void genJavaSourceFile(String pathStr, String packageName, String className, List<MethodSpec> methodSpecs) {
|
||||
TypeSpec typeSpec = TypeSpec
|
||||
.interfaceBuilder(className)
|
||||
.addModifiers(Modifier.PUBLIC)
|
||||
.addMethods(methodSpecs)
|
||||
.build();
|
||||
JavaFile javaFile = JavaFile.builder(packageName, typeSpec)
|
||||
.addFileComment("--auto generated by workflow auto-gen plugin--")
|
||||
.build();
|
||||
String javaFileName = typeSpec.name + ".java";
|
||||
try {
|
||||
Path path = Paths.get(pathStr);
|
||||
File packageFolder = new File(path.toFile().getAbsolutePath());
|
||||
if (!packageFolder.exists()) {
|
||||
System.out.println("package not exists:" + path);
|
||||
return;
|
||||
}
|
||||
String javaFilePath = path.toFile().getAbsolutePath() + File.separator + javaFileName;
|
||||
System.out.println("name:" + javaFilePath);
|
||||
File sourceFile = new File(javaFilePath);
|
||||
if (!sourceFile.exists()) {
|
||||
javaFile.writeTo(packageFolder);
|
||||
} else {
|
||||
javaFile.writeTo(packageFolder);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,96 +0,0 @@
|
||||
package cn.axzo.workflow.generate;
|
||||
|
||||
import cn.axzo.workflow.generate.annotition.GenService;
|
||||
import cn.axzo.workflow.generate.annotition.Management;
|
||||
import com.google.auto.service.AutoService;
|
||||
import com.squareup.javapoet.AnnotationSpec;
|
||||
import com.squareup.javapoet.MethodSpec;
|
||||
import com.squareup.javapoet.ParameterSpec;
|
||||
import com.squareup.javapoet.TypeName;
|
||||
import com.squareup.javapoet.TypeSpec;
|
||||
|
||||
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.element.Modifier;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@AutoService(CodeGenProcessor.class)
|
||||
public class GenServiceProcessor extends BaseCodeGenProcessor {
|
||||
|
||||
@Override
|
||||
public Class<? extends Annotation> getAnnotation() {
|
||||
return GenService.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String generatePackage(TypeElement typeElement) {
|
||||
return typeElement.getAnnotation(GenService.class).genPkgName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GenCodeInfo generate(TypeElement typeElement, RoundEnvironment roundEnvironment) throws Exception {
|
||||
String genPkgName = typeElement.getAnnotation(GenService.class).genPkgName();
|
||||
String sourcePath = typeElement.getAnnotation(GenService.class).genSourcePath();
|
||||
String genClassName = typeElement.getAnnotation(GenService.class).genClassName();
|
||||
List<? extends Element> enclosedElements = typeElement.getEnclosedElements();
|
||||
TypeSpec.Builder targetTypeSpec = TypeSpec.interfaceBuilder(genClassName)
|
||||
.addModifiers(typeElement.getModifiers().toArray(new Modifier[]{}));
|
||||
for (Element enclosedElement : enclosedElements) {
|
||||
if (enclosedElement instanceof ExecutableElement) {
|
||||
ExecutableElement executableElement = (ExecutableElement) enclosedElement;
|
||||
List<? extends AnnotationMirror> annotationMirrors = executableElement.getAnnotationMirrors();
|
||||
boolean add = true;
|
||||
for (AnnotationMirror annotationMirror : annotationMirrors) {
|
||||
if (annotationMirror.getAnnotationType().toString().equals(Management.class.getName())) {
|
||||
add = false;
|
||||
}
|
||||
}
|
||||
if (add) {
|
||||
List<ParameterSpec> parameterSpecs = new ArrayList<>();
|
||||
for (VariableElement variableElement : executableElement.getParameters()) {
|
||||
ParameterSpec parameterSpec = ParameterSpec.get(variableElement);
|
||||
List<? extends AnnotationMirror> paramAnnotations = variableElement.getAnnotationMirrors();
|
||||
if (paramAnnotations != null && !paramAnnotations.isEmpty()) {
|
||||
List<AnnotationSpec> annotationSpecs = new ArrayList<>();
|
||||
for (AnnotationMirror annotation : paramAnnotations) {
|
||||
annotationSpecs.add(AnnotationSpec.get(annotation));
|
||||
}
|
||||
parameterSpec = parameterSpec.toBuilder().addAnnotations(annotationSpecs).build();
|
||||
}
|
||||
parameterSpecs.add(parameterSpec);
|
||||
}
|
||||
List<AnnotationSpec> annotationSpecs = new ArrayList<>();
|
||||
for (AnnotationMirror annotationMirror : annotationMirrors) {
|
||||
annotationSpecs.add(AnnotationSpec.get(annotationMirror));
|
||||
}
|
||||
List<TypeName> exceptions = new ArrayList<>();
|
||||
List<? extends TypeMirror> thrownTypes = executableElement.getThrownTypes();
|
||||
for (TypeMirror typeMirror : thrownTypes) {
|
||||
exceptions.add(TypeName.get(typeMirror));
|
||||
}
|
||||
MethodSpec main = MethodSpec.methodBuilder(enclosedElement.getSimpleName().toString())
|
||||
.addModifiers(enclosedElement.getModifiers())
|
||||
.returns(TypeName.get(executableElement.getReturnType()))
|
||||
.addParameters(parameterSpecs)
|
||||
.addAnnotations(annotationSpecs)
|
||||
.addExceptions(exceptions)
|
||||
.build();
|
||||
targetTypeSpec.addMethod(main);
|
||||
}
|
||||
}
|
||||
}
|
||||
return GenCodeInfo.builder()
|
||||
.className(genClassName)
|
||||
.packageName(genPkgName)
|
||||
.sourcePath(sourcePath)
|
||||
.typeSpec(targetTypeSpec.build())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
package cn.axzo.workflow.generate;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
|
||||
public final class ProcessingEnvironmentHolder {
|
||||
|
||||
public static final ThreadLocal<ProcessingEnvironment> environment = new ThreadLocal<>();
|
||||
|
||||
|
||||
public static void setEnvironment(ProcessingEnvironment pe) {
|
||||
environment.set(pe);
|
||||
}
|
||||
|
||||
public static ProcessingEnvironment getEnvironment() {
|
||||
return environment.get();
|
||||
}
|
||||
}
|
||||
@ -1,128 +0,0 @@
|
||||
package cn.axzo.workflow.generate;
|
||||
|
||||
import com.google.auto.service.AutoService;
|
||||
import com.squareup.javapoet.MethodSpec;
|
||||
|
||||
import javax.annotation.processing.AbstractProcessor;
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.annotation.processing.Processor;
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.util.ElementFilter;
|
||||
import javax.tools.Diagnostic;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@AutoService(Processor.class)
|
||||
public class WorkflowExposeApiGenProcessor extends AbstractProcessor {
|
||||
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
List<GenCodeInfo> genCodeInfos = new ArrayList<>();
|
||||
annotations.forEach(an -> {
|
||||
Set<? extends Element> typeElements = roundEnv.getElementsAnnotatedWith(an);
|
||||
Set<TypeElement> types = ElementFilter.typesIn(typeElements);
|
||||
for (TypeElement typeElement : types) {
|
||||
CodeGenProcessor codeGenProcessor = CodeGenProcessorRegistry.find(an.getQualifiedName().toString());
|
||||
try {
|
||||
GenCodeInfo generate = codeGenProcessor.generate(typeElement, roundEnv);
|
||||
genCodeInfos.add(generate);
|
||||
} catch (Exception e) {
|
||||
ProcessingEnvironmentHolder.getEnvironment().getMessager().printMessage(Diagnostic.Kind.ERROR, "代码生成异常:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
if (!genCodeInfos.isEmpty()) {
|
||||
Map<Triplet<String, String, String>, List<GenCodeInfo>> apiGenMap = new HashMap<>();
|
||||
//进行分组
|
||||
for (GenCodeInfo genCodeInfo : genCodeInfos) {
|
||||
Triplet<String, String, String> triplet = Triplet.of(genCodeInfo.getSourcePath(), genCodeInfo.getPackageName(), genCodeInfo.getClassName());
|
||||
List<GenCodeInfo> codeInfos = apiGenMap.computeIfAbsent(triplet, k -> new ArrayList<>());
|
||||
codeInfos.add(genCodeInfo);
|
||||
}
|
||||
for (Map.Entry<Triplet<String, String, String>, List<GenCodeInfo>> entry : apiGenMap.entrySet()) {
|
||||
Triplet<String, String, String> triplet = entry.getKey();
|
||||
List<MethodSpec> methodSpecs = new ArrayList<>();
|
||||
List<GenCodeInfo> infos = entry.getValue();
|
||||
for (GenCodeInfo genCodeInfo : infos) {
|
||||
methodSpecs.addAll(genCodeInfo.getTypeSpec().methodSpecs);
|
||||
}
|
||||
GenJavaSourceFileUtils.genJavaSourceFile(triplet.getFirst(), triplet.getSecond(), triplet.getThird(), methodSpecs);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void init(ProcessingEnvironment processingEnv) {
|
||||
super.init(processingEnv);
|
||||
ProcessingEnvironmentHolder.setEnvironment(processingEnv);
|
||||
CodeGenProcessorRegistry.initProcessors();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getSupportedAnnotationTypes() {
|
||||
return CodeGenProcessorRegistry.getSupportedAnnotations();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SourceVersion getSupportedSourceVersion() {
|
||||
return SourceVersion.latestSupported();
|
||||
}
|
||||
|
||||
public static final class Triplet<T, U, V> {
|
||||
private final T first;
|
||||
private final U second;
|
||||
private final V third;
|
||||
|
||||
public Triplet(T first, U second, V third) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
this.third = third;
|
||||
}
|
||||
|
||||
public T getFirst() {
|
||||
return first;
|
||||
}
|
||||
|
||||
public U getSecond() {
|
||||
return second;
|
||||
}
|
||||
|
||||
public V getThird() {
|
||||
return third;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Triplet{" +
|
||||
"first=" + first +
|
||||
", second=" + second +
|
||||
", third=" + third +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Triplet<?, ?, ?> triplet = (Triplet<?, ?, ?>) o;
|
||||
return Objects.equals(first, triplet.first) && Objects.equals(second, triplet.second) && Objects.equals(third, triplet.third);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(first, second, third);
|
||||
}
|
||||
|
||||
public static <T, U, V> Triplet<T, U, V> of(T first, U second, V third) {
|
||||
return new Triplet<>(first, second, third);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
package cn.axzo.workflow.generate.annotition;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface GenService {
|
||||
|
||||
String genPkgName();
|
||||
|
||||
String genSourcePath() default "workflow-engine-spring-boot-starter/src/main/java";
|
||||
|
||||
String genClassName() default "WorkflowCoreService_Gen";
|
||||
|
||||
boolean overrideSource() default false;
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
package cn.axzo.workflow.generate.annotition;
|
||||
|
||||
/**
|
||||
* 控制接口是否调用受限,标记了注解的方法,表示受控,不允许暴露给客户端使用
|
||||
*
|
||||
* @author wangli
|
||||
* @since 2024/5/22 10:43
|
||||
*/
|
||||
public @interface Management {
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
cn.axzo.workflow.generate.WorkflowExposeApiGenProcessor
|
||||
Loading…
Reference in New Issue
Block a user