This commit is contained in:
yulichang 2024-10-04 11:19:33 +08:00
parent 5269f3a535
commit 5c587a3bfe
4 changed files with 69 additions and 28 deletions

View File

@ -109,25 +109,6 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>4.0.0-M14</version>
<configuration>
<locales>default,es,ja,fr,zh_CN,ko</locales>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<configuration>
<excludes>
<exclude>com.github.yulichang.processor.ognl.*</exclude>
<exclude>com.github.yulichang.processor.javassist.*</exclude>
</excludes>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -56,13 +56,35 @@ public class EntityProcessor extends AbstractProcessor {
@Override @Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (!roundEnv.processingOver()) { if (!roundEnv.processingOver()) {
TypeElement table = annotations.stream().filter(i -> i.toString().equals(TABLE)).findFirst().orElse(null); Set<? extends Element> tables = roundEnv.getRootElements().stream().filter(i -> {
if (table != null) { List<? extends AnnotationMirror> mirrors = i.getAnnotationMirrors();
if (mirrors != null && !mirrors.isEmpty()) {
if (mirrors.stream().anyMatch(m -> m.getAnnotationType().toString().equals(TABLE))) {
return true;
}
if (StringUtil.isNotEmpty(globalConf.getScanAnno())) {
if (mirrors.stream().anyMatch(m -> m.getAnnotationType().toString().equals(globalConf.getScanAnno()))) {
return true;
}
}
}
if (StringUtil.isNotEmpty(globalConf.getScanPackage())) {
if (i.getKind() != ElementKind.CLASS) {
return false;
}
if (i.getModifiers().contains(Modifier.ABSTRACT)) {
return false;
}
String pkg = elementUtils.getPackageOf(i).getQualifiedName().toString();
String[] scanPackages = globalConf.getScanPackage().split(",");
return Arrays.stream(scanPackages).anyMatch(s -> StringUtil.matches(pkg, s));
}
return false;
}).collect(Collectors.toSet());
if (!tables.isEmpty()) {
note("mybatis plus join processor start"); note("mybatis plus join processor start");
Set<? extends Element> tables = roundEnv.getElementsAnnotatedWith(table);
tables.stream().filter(f -> f instanceof TypeElement) tables.stream().filter(f -> f instanceof TypeElement)
.map(f -> (TypeElement) f).map(this::createColumn) .map(f -> (TypeElement) f).map(this::createColumn).filter(TableInfo::isGenTables)
.filter(Objects::nonNull).filter(TableInfo::isGenTables)
.collect(Collectors.groupingBy(TableInfo::getTagTablesPackageName)) .collect(Collectors.groupingBy(TableInfo::getTagTablesPackageName))
.forEach(this::createTables); .forEach(this::createTables);
} }
@ -75,6 +97,9 @@ public class EntityProcessor extends AbstractProcessor {
public Set<String> getSupportedAnnotationTypes() { public Set<String> getSupportedAnnotationTypes() {
Set<String> supportedAnnotationTypes = new HashSet<>(); Set<String> supportedAnnotationTypes = new HashSet<>();
supportedAnnotationTypes.add(TABLE); supportedAnnotationTypes.add(TABLE);
if (StringUtil.isNotEmpty(globalConf.getScanAnno())) {
supportedAnnotationTypes.add(globalConf.getScanAnno());
}
return supportedAnnotationTypes; return supportedAnnotationTypes;
} }
@ -89,10 +114,7 @@ public class EntityProcessor extends AbstractProcessor {
private TableInfo createColumn(TypeElement element) { private TableInfo createColumn(TypeElement element) {
AnnotationMirror tb = element.getAnnotationMirrors().stream().filter(a -> AnnotationMirror tb = element.getAnnotationMirrors().stream().filter(a ->
a.getAnnotationType().asElement().toString().equals(TABLE)).findFirst().orElse(null); a.getAnnotationType().asElement().toString().equals(TABLE)).findFirst().orElse(null);
if (tb == null) { Conf conf = Optional.ofNullable(tb).map(t -> Conf.getConf(globalConf, t.getElementValues())).orElse(globalConf);
return null;
}
Conf conf = Conf.getConf(globalConf, tb.getElementValues());
TableInfo tableInfo = new TableInfo(conf, element.toString(), element.getSimpleName().toString()); TableInfo tableInfo = new TableInfo(conf, element.toString(), element.getSimpleName().toString());
tableInfo.setClassPackage(elementUtils.getPackageOf(element).getQualifiedName().toString()); tableInfo.setClassPackage(elementUtils.getPackageOf(element).getQualifiedName().toString());
tableInfo.setClassComment(elementUtils.getDocComment(element)); tableInfo.setClassComment(elementUtils.getDocComment(element));

View File

@ -23,6 +23,9 @@ public class Conf {
private String tablesClassName = "%S"; private String tablesClassName = "%S";
private boolean cache = true; private boolean cache = true;
private String scanAnno = "";
private String scanPackage = "";
private boolean initFlag = false; private boolean initFlag = false;
private Conf(Conf conf) { private Conf(Conf conf) {
@ -33,6 +36,9 @@ public class Conf {
this.tablesClassName = conf.tablesClassName; this.tablesClassName = conf.tablesClassName;
this.initFlag = conf.initFlag; this.initFlag = conf.initFlag;
this.cache = conf.cache; this.cache = conf.cache;
this.scanAnno = conf.scanAnno;
this.scanPackage = conf.scanPackage;
} }
@ -75,6 +81,8 @@ public class Conf {
this.tablasClassPackage = properties.getOrDefault("tablasClassPackage", this.tablasClassPackage).toString(); this.tablasClassPackage = properties.getOrDefault("tablasClassPackage", this.tablasClassPackage).toString();
this.tablesClassName = properties.getOrDefault("tablesClassName", this.tablesClassName).toString(); this.tablesClassName = properties.getOrDefault("tablesClassName", this.tablesClassName).toString();
this.cache = Boolean.parseBoolean(properties.getOrDefault("cache", this.cache).toString()); this.cache = Boolean.parseBoolean(properties.getOrDefault("cache", this.cache).toString());
this.scanAnno = properties.getOrDefault("scanAnno", this.scanAnno).toString();
this.scanPackage = properties.getOrDefault("scanPackage", this.scanPackage).toString();
} }
public static Conf getConf(Conf globalConf, Map<? extends ExecutableElement, ? extends AnnotationValue> elementMap) { public static Conf getConf(Conf globalConf, Map<? extends ExecutableElement, ? extends AnnotationValue> elementMap) {
@ -134,6 +142,14 @@ public class Conf {
this.cache = cache; this.cache = cache;
} }
public String getScanAnno() {
return scanAnno;
}
public String getScanPackage() {
return scanPackage;
}
public enum ConfItem { public enum ConfItem {
className("value", (c, v) -> c.setClassName(v.toString())), className("value", (c, v) -> c.setClassName(v.toString())),
packageName("classPackage", (c, v) -> c.setClassPackage(v.toString())), packageName("classPackage", (c, v) -> c.setClassPackage(v.toString())),
@ -165,6 +181,9 @@ public class Conf {
", genTables=" + genTables + ", genTables=" + genTables +
", tablasClassPackage='" + tablasClassPackage + '\'' + ", tablasClassPackage='" + tablasClassPackage + '\'' +
", tablesClassName='" + tablesClassName + '\'' + ", tablesClassName='" + tablesClassName + '\'' +
", cache=" + cache +
", scanAnno='" + scanAnno + '\'' +
", scanPackage='" + scanPackage + '\'' +
", initFlag=" + initFlag + ", initFlag=" + initFlag +
'}'; '}';
} }

View File

@ -1,5 +1,8 @@
package com.github.yulichang.processor.utils; package com.github.yulichang.processor.utils;
import java.util.Arrays;
import java.util.stream.Collectors;
public final class StringUtil { public final class StringUtil {
public static boolean isEmpty(String str) { public static boolean isEmpty(String str) {
@ -16,4 +19,20 @@ public final class StringUtil {
} }
return fullName.substring(fullName.lastIndexOf(".") + 1); return fullName.substring(fullName.lastIndexOf(".") + 1);
} }
public static boolean matches(String packageName, String packageRegex) {
if (packageRegex.lastIndexOf("*") != -1) {
String regex = Arrays.stream(packageRegex.split("\\.")).map(r -> {
if (r.equals("**")) {
return "[A-Za-z0-9_.]*";
} else if (r.equals("*")) {
return "\\w*";
} else {
return r;
}
}).collect(Collectors.joining("\\."));
return packageName.matches(regex);
}
return packageRegex.equals(packageName);
}
} }