mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
Merge branch 'refs/heads/proce'
# Conflicts: # plugin/mybatis-plus-join-processor/pom.xml
This commit is contained in:
commit
5a7416345a
@ -18,12 +18,17 @@ import java.lang.annotation.Target;
|
|||||||
* <li>加APT后缀并且大写 %SAPT</li>
|
* <li>加APT后缀并且大写 %SAPT</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
* <p>
|
* <p>
|
||||||
* 支持Ognl语法字段说明<br/>
|
* 支持Ognl语法,字段说明<br/>
|
||||||
* Ognl上下文
|
* Ognl上下文
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>className 类名</li>
|
* <li>className 类名</li>
|
||||||
* <li>classPackage 包名</li>
|
* <li>classPackage 包名</li>
|
||||||
* <li>util 工具类 stringHelper {@link com.github.yulichang.apt.OgnlRoot.StringHelper}</li>
|
* <li>
|
||||||
|
* util 工具类 OgnlUtil {@link com.github.yulichang.processor.utils.OgnlUtil} ,
|
||||||
|
* <a href="https://github.com/yulichang/mybatis-plus-join/tree/master/plugin/mybatis-plus-join-processor/src/main/java/com/github/yulichang/processor/utils/OgnlUtil.java">
|
||||||
|
* github link
|
||||||
|
* </a>
|
||||||
|
* </li>
|
||||||
* </ul>
|
* </ul>
|
||||||
* 指定开头 Ognl# 这不是ognl语法,这是MPJ规定的 用于区分 ognl还是String.format
|
* 指定开头 Ognl# 这不是ognl语法,这是MPJ规定的 用于区分 ognl还是String.format
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -1,89 +0,0 @@
|
|||||||
package com.github.yulichang.extension.apt.matedata;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* apt ognl表达式上下文
|
|
||||||
*
|
|
||||||
* @author yulichang
|
|
||||||
* @since 1.5.0
|
|
||||||
*/
|
|
||||||
@Getter
|
|
||||||
public class OgnlRoot {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 类名
|
|
||||||
*/
|
|
||||||
private final String className;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 包名
|
|
||||||
*/
|
|
||||||
private final String classPackage;
|
|
||||||
|
|
||||||
private final StringHelper util = new StringHelper();
|
|
||||||
|
|
||||||
public OgnlRoot(String className, String classPackage) {
|
|
||||||
this.className = className;
|
|
||||||
this.classPackage = classPackage;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static final class StringHelper {
|
|
||||||
/**
|
|
||||||
* 移除后缀
|
|
||||||
*
|
|
||||||
* @param str 原字符串
|
|
||||||
* @param suffix 指定后缀
|
|
||||||
*/
|
|
||||||
public String removeSuffix(String str, String suffix) {
|
|
||||||
if (isBlank(str) || isBlank(suffix)) {
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
if (str.endsWith(suffix)) {
|
|
||||||
return str.substring(0, str.length() - suffix.length());
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 替换后缀
|
|
||||||
*
|
|
||||||
* @param str 原字符串
|
|
||||||
* @param suffix 指定后缀
|
|
||||||
* @param replacement 新后缀
|
|
||||||
*/
|
|
||||||
public String replaceSuffix(String str, String suffix, String replacement) {
|
|
||||||
if (isBlank(str)) {
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
String rep = Objects.isNull(replacement) ? "" : replacement;
|
|
||||||
if (isBlank(suffix)) {
|
|
||||||
return str + rep;
|
|
||||||
}
|
|
||||||
if (str.endsWith(suffix)) {
|
|
||||||
return str.substring(0, str.length() - suffix.length()) + rep;
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取上级包名
|
|
||||||
*
|
|
||||||
* @param pk 报名
|
|
||||||
* @return 上级报名
|
|
||||||
*/
|
|
||||||
public String getParentPackage(String pk) {
|
|
||||||
if (pk.lastIndexOf(".") > -1) {
|
|
||||||
return pk;
|
|
||||||
}
|
|
||||||
return pk.substring(0, pk.lastIndexOf('.'));
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isBlank(String str) {
|
|
||||||
return str == null || str.trim().isEmpty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -34,11 +34,29 @@
|
|||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
<artifactId>jackson-core</artifactId>
|
<artifactId>jackson-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.github.yulichang</groupId>
|
|
||||||
<artifactId>mybatis-plus-join-processor</artifactId>
|
|
||||||
<version>${revision}</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.13.0</version>
|
||||||
|
<configuration>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>com.github.yulichang</groupId>
|
||||||
|
<artifactId>mybatis-plus-join-processor</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</path>
|
||||||
|
<path>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.34</version>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
@ -16,14 +16,17 @@
|
|||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.yulichang</groupId>
|
<groupId>ognl</groupId>
|
||||||
<artifactId>mybatis-plus-join-core</artifactId>
|
<artifactId>ognl</artifactId>
|
||||||
<version>${revision}</version>
|
<version>3.4.3</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baomidou</groupId>
|
<groupId>org.javassist</groupId>
|
||||||
<artifactId>mybatis-plus-extension</artifactId>
|
<artifactId>javassist</artifactId>
|
||||||
<version>${mybatis.plus.version}</version>
|
<version>3.30.2-GA</version>
|
||||||
|
<scope>compile</scope>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
@ -48,6 +51,7 @@
|
|||||||
|
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.13.0</version>
|
<version>3.13.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
@ -56,6 +60,55 @@
|
|||||||
<target>1.8</target>
|
<target>1.8</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.6.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<phase>package</phase>
|
||||||
|
<configuration>
|
||||||
|
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||||
|
<createSourcesJar>true</createSourcesJar>
|
||||||
|
<artifactSet>
|
||||||
|
<includes>
|
||||||
|
<include>ognl:ognl</include>
|
||||||
|
<include>org.javassist:javassist</include>
|
||||||
|
</includes>
|
||||||
|
</artifactSet>
|
||||||
|
<filters>
|
||||||
|
<filter>
|
||||||
|
<artifact>ognl:ognl</artifact>
|
||||||
|
<excludes>
|
||||||
|
<exclude>META-INF/MANIFEST.MF</exclude>
|
||||||
|
</excludes>
|
||||||
|
</filter>
|
||||||
|
<filter>
|
||||||
|
<artifact>org.javassist:javassist</artifact>
|
||||||
|
<excludes>
|
||||||
|
<exclude>META-INF/MANIFEST.MF</exclude>
|
||||||
|
</excludes>
|
||||||
|
</filter>
|
||||||
|
</filters>
|
||||||
|
<relocations>
|
||||||
|
<relocation>
|
||||||
|
<pattern>ognl</pattern>
|
||||||
|
<shadedPattern>com.github.yulichang.processor.ognl</shadedPattern>
|
||||||
|
</relocation>
|
||||||
|
<relocation>
|
||||||
|
<pattern>javassist</pattern>
|
||||||
|
<shadedPattern>com.github.yulichang.processor.javassist</shadedPattern>
|
||||||
|
</relocation>
|
||||||
|
</relocations>
|
||||||
|
<shadeSourcesContent>true</shadeSourcesContent>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
@ -1,9 +1,5 @@
|
|||||||
package com.github.yulichang.processor;
|
package com.github.yulichang.processor;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
|
||||||
import com.github.yulichang.annotation.Table;
|
|
||||||
import com.github.yulichang.extension.apt.matedata.BaseColumn;
|
|
||||||
import com.github.yulichang.extension.apt.matedata.Column;
|
|
||||||
import com.github.yulichang.processor.matedata.Conf;
|
import com.github.yulichang.processor.matedata.Conf;
|
||||||
import com.github.yulichang.processor.matedata.FieldInfo;
|
import com.github.yulichang.processor.matedata.FieldInfo;
|
||||||
import com.github.yulichang.processor.matedata.TableInfo;
|
import com.github.yulichang.processor.matedata.TableInfo;
|
||||||
@ -31,6 +27,13 @@ import java.util.stream.Collectors;
|
|||||||
*/
|
*/
|
||||||
public class EntityProcessor extends AbstractProcessor {
|
public class EntityProcessor extends AbstractProcessor {
|
||||||
|
|
||||||
|
private static final String TABLE = "com.github.yulichang.annotation.Table";
|
||||||
|
private static final String TABLE_FIELD = "com.baomidou.mybatisplus.annotation.TableField";
|
||||||
|
private static final String TABLE_FIELD_EXIST = "exist";
|
||||||
|
|
||||||
|
private static final String BASE_COLUMN = "com.github.yulichang.extension.apt.matedata.BaseColumn";
|
||||||
|
private static final String COLUMN = "com.github.yulichang.extension.apt.matedata.Column";
|
||||||
|
|
||||||
private Elements elementUtils;
|
private Elements elementUtils;
|
||||||
private Types typeUtils;
|
private Types typeUtils;
|
||||||
private Messager messager;
|
private Messager messager;
|
||||||
@ -53,13 +56,38 @@ 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.class.getName())).findFirst().orElse(null);
|
if (!globalConf.isEnable()) {
|
||||||
if (table != null) {
|
return false;
|
||||||
|
}
|
||||||
|
Set<? extends Element> tables = roundEnv.getRootElements().stream().filter(i -> {
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
@ -71,7 +99,10 @@ public class EntityProcessor extends AbstractProcessor {
|
|||||||
@Override
|
@Override
|
||||||
public Set<String> getSupportedAnnotationTypes() {
|
public Set<String> getSupportedAnnotationTypes() {
|
||||||
Set<String> supportedAnnotationTypes = new HashSet<>();
|
Set<String> supportedAnnotationTypes = new HashSet<>();
|
||||||
supportedAnnotationTypes.add(Table.class.getCanonicalName());
|
supportedAnnotationTypes.add(TABLE);
|
||||||
|
if (StringUtil.isNotEmpty(globalConf.getScanAnno())) {
|
||||||
|
supportedAnnotationTypes.add(globalConf.getScanAnno());
|
||||||
|
}
|
||||||
return supportedAnnotationTypes;
|
return supportedAnnotationTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,19 +116,13 @@ 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.class.getName())).findFirst().orElse(null);
|
a.getAnnotationType().asElement().toString().equals(TABLE)).findFirst().orElse(null);
|
||||||
Table table = element.getAnnotation(Table.class);
|
Conf conf = Optional.ofNullable(tb).map(t -> Conf.getConf(globalConf, t.getElementValues())).orElse(globalConf);
|
||||||
if (tb == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Set<String> keySet = tb.getElementValues().keySet().stream().map(k ->
|
|
||||||
k.getSimpleName().toString()).collect(Collectors.toSet());
|
|
||||||
Conf conf = Conf.getConf(globalConf, table, keySet);
|
|
||||||
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));
|
||||||
|
|
||||||
Set<FieldInfo> fieldInfos = new HashSet<>();
|
List<FieldInfo> fieldInfos = new ArrayList<>();
|
||||||
|
|
||||||
TypeElement currElement = element;
|
TypeElement currElement = element;
|
||||||
do {
|
do {
|
||||||
@ -106,8 +131,15 @@ public class EntityProcessor extends AbstractProcessor {
|
|||||||
e.getKind() == ElementKind.FIELD && !e.getModifiers().contains(Modifier.STATIC))
|
e.getKind() == ElementKind.FIELD && !e.getModifiers().contains(Modifier.STATIC))
|
||||||
.filter(e -> {
|
.filter(e -> {
|
||||||
// 过滤 exist = false 的字段
|
// 过滤 exist = false 的字段
|
||||||
TableField tableField = e.getAnnotation(TableField.class);
|
AnnotationMirror tableField = e.getAnnotationMirrors().stream().filter(f ->
|
||||||
return tableField == null || tableField.exist();
|
TABLE_FIELD.equals(f.getAnnotationType().toString())).findFirst().orElse(null);
|
||||||
|
if (tableField != null) {
|
||||||
|
Map<String, Object> propMap = tableField.getElementValues().entrySet().stream()
|
||||||
|
.collect(Collectors.toMap(entry -> entry.getKey().getSimpleName().toString(), entry -> entry.getValue().getValue()));
|
||||||
|
Object exist = propMap.get(TABLE_FIELD_EXIST);
|
||||||
|
return exist == null || (boolean) exist;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
})
|
})
|
||||||
.map(e -> new FieldInfo(e.toString(), elementUtils.getDocComment(e))).collect(Collectors.toList()));
|
.map(e -> new FieldInfo(e.toString(), elementUtils.getDocComment(e))).collect(Collectors.toList()));
|
||||||
currElement = (TypeElement) typeUtils.asElement(currElement.getSuperclass());
|
currElement = (TypeElement) typeUtils.asElement(currElement.getSuperclass());
|
||||||
@ -118,8 +150,8 @@ public class EntityProcessor extends AbstractProcessor {
|
|||||||
StringBuilderHelper content = new StringBuilderHelper(tableInfo)
|
StringBuilderHelper content = new StringBuilderHelper(tableInfo)
|
||||||
.addPackage(tableInfo.getTagClassPackage())
|
.addPackage(tableInfo.getTagClassPackage())
|
||||||
.newLine()
|
.newLine()
|
||||||
.addImport(true, BaseColumn.class.getName())
|
.addImport(true, BASE_COLUMN)
|
||||||
.addImport(true, Column.class.getName())
|
.addImport(true, COLUMN)
|
||||||
.addImport(true, tableInfo.getClassName())
|
.addImport(true, tableInfo.getClassName())
|
||||||
.newLine(tableInfo.isCache())
|
.newLine(tableInfo.isCache())
|
||||||
.addImport(tableInfo.isCache(), Map.class.getName())
|
.addImport(tableInfo.isCache(), Map.class.getName())
|
||||||
@ -127,7 +159,7 @@ public class EntityProcessor extends AbstractProcessor {
|
|||||||
.addImport(tableInfo.isCache(), ConcurrentHashMap.class.getName())
|
.addImport(tableInfo.isCache(), ConcurrentHashMap.class.getName())
|
||||||
.newLine()
|
.newLine()
|
||||||
.addClass(tableInfo.getClassComment(), tableInfo.getTagClassName(),
|
.addClass(tableInfo.getClassComment(), tableInfo.getTagClassName(),
|
||||||
BaseColumn.class.getSimpleName() + "<" + tableInfo.getSimpleClassName() + ">",
|
StringUtil.getSimpleName(BASE_COLUMN) + "<" + tableInfo.getSimpleClassName() + ">",
|
||||||
c -> c
|
c -> c
|
||||||
.addConstructor()
|
.addConstructor()
|
||||||
.addFields()
|
.addFields()
|
||||||
|
@ -1,19 +1,18 @@
|
|||||||
package com.github.yulichang.processor.matedata;
|
package com.github.yulichang.processor.matedata;
|
||||||
|
|
||||||
import com.github.yulichang.annotation.Table;
|
|
||||||
|
|
||||||
import javax.annotation.processing.Filer;
|
import javax.annotation.processing.Filer;
|
||||||
|
import javax.lang.model.element.AnnotationValue;
|
||||||
|
import javax.lang.model.element.ExecutableElement;
|
||||||
import javax.tools.FileObject;
|
import javax.tools.FileObject;
|
||||||
import javax.tools.StandardLocation;
|
import javax.tools.StandardLocation;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
public class Conf {
|
public class Conf {
|
||||||
|
|
||||||
@ -24,6 +23,10 @@ public class Conf {
|
|||||||
private String tablesClassName = "%S";
|
private String tablesClassName = "%S";
|
||||||
private boolean cache = true;
|
private boolean cache = true;
|
||||||
|
|
||||||
|
private boolean enable = true;
|
||||||
|
private String scanAnno = "";
|
||||||
|
private String scanPackage = "";
|
||||||
|
|
||||||
private boolean initFlag = false;
|
private boolean initFlag = false;
|
||||||
|
|
||||||
private Conf(Conf conf) {
|
private Conf(Conf conf) {
|
||||||
@ -34,6 +37,10 @@ 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.enable = conf.enable;
|
||||||
|
this.scanAnno = conf.scanAnno;
|
||||||
|
this.scanPackage = conf.scanPackage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -76,14 +83,17 @@ 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.enable = Boolean.parseBoolean(properties.getOrDefault("enable", this.enable).toString());
|
||||||
|
this.scanAnno = properties.getOrDefault("scanAnno", this.scanAnno).toString();
|
||||||
|
this.scanPackage = properties.getOrDefault("scanPackage", this.scanPackage).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Conf getConf(Conf globalConf, Table table, Collection<String> keys) {
|
public static Conf getConf(Conf globalConf, Map<? extends ExecutableElement, ? extends AnnotationValue> elementMap) {
|
||||||
if (keys == null || keys.isEmpty()) {
|
if (elementMap == null || elementMap.isEmpty()) {
|
||||||
return globalConf;
|
return globalConf;
|
||||||
}
|
}
|
||||||
Conf conf = new Conf(globalConf);
|
Conf conf = new Conf(globalConf);
|
||||||
keys.forEach(key -> ConfItem.doIt(conf, key, table));
|
elementMap.forEach((k, v) -> ConfItem.doIt(conf, k.getSimpleName().toString(), v));
|
||||||
return conf;
|
return conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,29 +145,38 @@ public class Conf {
|
|||||||
this.cache = cache;
|
this.cache = cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getScanAnno() {
|
||||||
|
return scanAnno;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getScanPackage() {
|
||||||
|
return scanPackage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEnable() {
|
||||||
|
return enable;
|
||||||
|
}
|
||||||
|
|
||||||
public enum ConfItem {
|
public enum ConfItem {
|
||||||
className("value", Table::value, (c, v) -> c.setClassName(v.toString())),
|
className("value", (c, v) -> c.setClassName(v.toString())),
|
||||||
packageName("classPackage", Table::classPackage, (c, v) -> c.setClassPackage(v.toString())),
|
packageName("classPackage", (c, v) -> c.setClassPackage(v.toString())),
|
||||||
genTables("genTables", Table::genTables, (c, v) -> c.setGenTables((boolean) v)),
|
genTables("genTables", (c, v) -> c.setGenTables((boolean) v)),
|
||||||
tablasPackageName("tablesClassPackage", Table::tablesClassPackage, (c, v) -> c.setTablasClassPackage(v.toString())),
|
tablasPackageName("tablesClassPackage", (c, v) -> c.setTablasClassPackage(v.toString())),
|
||||||
tablesName("tablesClassName", Table::tablesClassName, (c, v) -> c.setTablesClassName(v.toString())),
|
tablesName("tablesClassName", (c, v) -> c.setTablesClassName(v.toString())),
|
||||||
cache("cache", Table::cache, (c, v) -> c.setCache((boolean) v));
|
cache("cache", (c, v) -> c.setCache((boolean) v));
|
||||||
|
|
||||||
private final String action;
|
private final String action;
|
||||||
|
|
||||||
private final Function<Table, Object> annoVal;
|
|
||||||
|
|
||||||
private final BiConsumer<Conf, Object> doIt;
|
private final BiConsumer<Conf, Object> doIt;
|
||||||
|
|
||||||
ConfItem(String action, Function<Table, Object> annoVal, BiConsumer<Conf, Object> doIt) {
|
ConfItem(String action, BiConsumer<Conf, Object> doIt) {
|
||||||
this.action = action;
|
this.action = action;
|
||||||
this.annoVal = annoVal;
|
|
||||||
this.doIt = doIt;
|
this.doIt = doIt;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void doIt(Conf tableConf, String act, Table anno) {
|
public static void doIt(Conf tableConf, String key, AnnotationValue value) {
|
||||||
Arrays.stream(ConfItem.values()).filter(f -> f.action.equals(act)).findFirst()
|
Arrays.stream(ConfItem.values()).filter(f -> f.action.equals(key)).findFirst()
|
||||||
.ifPresent(item -> item.doIt.accept(tableConf, item.annoVal.apply(anno)));
|
.ifPresent(item -> item.doIt.accept(tableConf, value.getValue()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,6 +188,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 +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.github.yulichang.processor.matedata;
|
||||||
|
|
||||||
|
import com.github.yulichang.processor.utils.OgnlUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* apt ognl表达式上下文
|
||||||
|
*
|
||||||
|
* @author yulichang
|
||||||
|
* @since 1.5.0
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class OgnlRoot {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 类名
|
||||||
|
*/
|
||||||
|
private final String className;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 包名
|
||||||
|
*/
|
||||||
|
private final String classPackage;
|
||||||
|
|
||||||
|
private final static OgnlUtil util = new OgnlUtil();
|
||||||
|
|
||||||
|
public OgnlRoot(String className, String classPackage) {
|
||||||
|
this.className = className;
|
||||||
|
this.classPackage = classPackage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClassName() {
|
||||||
|
return className;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClassPackage() {
|
||||||
|
return classPackage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OgnlUtil getUtil() {
|
||||||
|
return util;
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +1,10 @@
|
|||||||
package com.github.yulichang.processor.matedata;
|
package com.github.yulichang.processor.matedata;
|
||||||
|
|
||||||
import com.github.yulichang.extension.apt.matedata.OgnlRoot;
|
import ognl.Ognl;
|
||||||
import org.apache.ibatis.builder.BuilderException;
|
import ognl.OgnlContext;
|
||||||
import org.apache.ibatis.ognl.Ognl;
|
import ognl.OgnlException;
|
||||||
import org.apache.ibatis.ognl.OgnlContext;
|
|
||||||
import org.apache.ibatis.ognl.OgnlException;
|
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author yulichang
|
* @author yulichang
|
||||||
@ -28,7 +26,7 @@ public class TableInfo {
|
|||||||
|
|
||||||
private final Conf conf;
|
private final Conf conf;
|
||||||
|
|
||||||
private Set<FieldInfo> fields;
|
private List<FieldInfo> fields;
|
||||||
|
|
||||||
private String tagClassName;
|
private String tagClassName;
|
||||||
private String tagPackageName;
|
private String tagPackageName;
|
||||||
@ -90,7 +88,7 @@ public class TableInfo {
|
|||||||
try {
|
try {
|
||||||
return Ognl.getValue(ognl, context, context.getRoot()).toString();
|
return Ognl.getValue(ognl, context, context.getRoot()).toString();
|
||||||
} catch (OgnlException e) {
|
} catch (OgnlException e) {
|
||||||
throw new BuilderException("Error evaluating expression '" + ognl + "'. Cause: " + e, e);
|
throw new RuntimeException("Error evaluating expression '" + ognl + "'. Cause: " + e, e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tag = String.format(expression, source);
|
tag = String.format(expression, source);
|
||||||
@ -130,11 +128,11 @@ public class TableInfo {
|
|||||||
return this.conf.isCache();
|
return this.conf.isCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<FieldInfo> getFields() {
|
public List<FieldInfo> getFields() {
|
||||||
return fields;
|
return fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFields(Set<FieldInfo> fields) {
|
public void setFields(List<FieldInfo> fields) {
|
||||||
this.fields = fields;
|
this.fields = fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.github.yulichang.processor.utils;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public final class OgnlUtil {
|
||||||
|
/**
|
||||||
|
* 移除后缀
|
||||||
|
*
|
||||||
|
* @param str 原字符串
|
||||||
|
* @param suffix 指定后缀
|
||||||
|
*/
|
||||||
|
public String removeSuffix(String str, String suffix) {
|
||||||
|
if (isBlank(str) || isBlank(suffix)) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
if (str.endsWith(suffix)) {
|
||||||
|
return str.substring(0, str.length() - suffix.length());
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 替换后缀
|
||||||
|
*
|
||||||
|
* @param str 原字符串
|
||||||
|
* @param suffix 指定后缀
|
||||||
|
* @param replacement 新后缀
|
||||||
|
*/
|
||||||
|
public String replaceSuffix(String str, String suffix, String replacement) {
|
||||||
|
if (isBlank(str)) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
String rep = Objects.isNull(replacement) ? "" : replacement;
|
||||||
|
if (isBlank(suffix)) {
|
||||||
|
return str + rep;
|
||||||
|
}
|
||||||
|
if (str.endsWith(suffix)) {
|
||||||
|
return str.substring(0, str.length() - suffix.length()) + rep;
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取上级包名
|
||||||
|
*
|
||||||
|
* @param pk 报名
|
||||||
|
* @return 上级报名
|
||||||
|
*/
|
||||||
|
public String getParentPackage(String pk) {
|
||||||
|
if (pk.lastIndexOf(".") > -1) {
|
||||||
|
return pk;
|
||||||
|
}
|
||||||
|
return pk.substring(0, pk.lastIndexOf('.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isBlank(String str) {
|
||||||
|
return str == null || str.trim().isEmpty();
|
||||||
|
}
|
||||||
|
}
|
@ -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) {
|
||||||
@ -9,4 +12,27 @@ public final class StringUtil {
|
|||||||
public static boolean isNotEmpty(String str) {
|
public static boolean isNotEmpty(String str) {
|
||||||
return !isEmpty(str);
|
return !isEmpty(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getSimpleName(String fullName) {
|
||||||
|
if (isEmpty(fullName) && fullName.lastIndexOf(".") == -1) {
|
||||||
|
return fullName;
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user