Skip to content

Commit d166699

Browse files
author
hewei
committed
LombokPlugin 支持 supportSuperBuilderForIdea参数,用于在IDEA替换SuperBuilder
1 parent 886cca0 commit d166699

File tree

8 files changed

+196
-47
lines changed

8 files changed

+196
-47
lines changed

src/main/java/com/itfsw/mybatis/generator/plugins/IncrementsPlugin.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,18 @@ public boolean sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated(XmlElement e
178178
// =============================================== ILombokPluginHook ===================================================
179179

180180
@Override
181-
public boolean modelBaseRecordBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
182-
return this.lombokBuilderClassGenerated(topLevelClass, columns, introspectedTable);
181+
public boolean modelBaseRecordBuilderClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
182+
return this.lombokBuilderClassGenerated(topLevelClass, IntrospectedTableTools.getModelBaseRecordClomns(introspectedTable), introspectedTable);
183183
}
184184

185185
@Override
186-
public boolean modelPrimaryKeyBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
187-
return this.lombokBuilderClassGenerated(topLevelClass, columns, introspectedTable);
186+
public boolean modelPrimaryKeyBuilderClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
187+
return this.lombokBuilderClassGenerated(topLevelClass, introspectedTable.getPrimaryKeyColumns(), introspectedTable);
188188
}
189189

190190
@Override
191-
public boolean modelRecordWithBLOBsBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
192-
return this.lombokBuilderClassGenerated(topLevelClass, columns, introspectedTable);
191+
public boolean modelRecordWithBLOBsBuilderClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
192+
return this.lombokBuilderClassGenerated(topLevelClass, introspectedTable.getBLOBColumns(), introspectedTable);
193193
}
194194

195195
/**

src/main/java/com/itfsw/mybatis/generator/plugins/LombokPlugin.java

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
package com.itfsw.mybatis.generator.plugins;
1818

1919
import com.itfsw.mybatis.generator.plugins.utils.BasePlugin;
20-
import com.itfsw.mybatis.generator.plugins.utils.IntrospectedTableTools;
20+
import com.itfsw.mybatis.generator.plugins.utils.EnumModelType;
2121
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
2222
import com.itfsw.mybatis.generator.plugins.utils.hook.ILombokPluginHook;
2323
import org.mybatis.generator.api.IntrospectedColumn;
2424
import org.mybatis.generator.api.IntrospectedTable;
2525
import org.mybatis.generator.api.dom.java.Method;
2626
import org.mybatis.generator.api.dom.java.TopLevelClass;
27+
import org.mybatis.generator.config.PluginConfiguration;
28+
import org.mybatis.generator.internal.ObjectFactory;
2729
import org.mybatis.generator.internal.util.StringUtility;
2830

2931
import java.util.ArrayList;
@@ -42,6 +44,11 @@
4244
* ---------------------------------------------------------------------------
4345
*/
4446
public class LombokPlugin extends BasePlugin {
47+
/**
48+
* 开启IDEA(老版本) SuperBuilder 支持
49+
*/
50+
public final static String PRO_SUPPORT_SUPER_BUILDER_FOR_IDEA = "supportSuperBuilderForIdea";
51+
4552
private final static List<String> LOMBOK_FEATURES;
4653
private final static List<String> LOMBOK_EXPERIMENTAL_FEATURES;
4754
private final static Pattern LOMBOK_ANNOTATION = Pattern.compile("@([a-zA-z]+)(\\(.*\\))?");
@@ -57,6 +64,7 @@ public class LombokPlugin extends BasePlugin {
5764
}
5865

5966
private List<String> annotations;
67+
private boolean suportSuperBuilderForIdea;
6068

6169
/**
6270
* 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
@@ -68,8 +76,8 @@ public boolean validate(List<String> warnings) {
6876
Properties properties = this.getProperties();
6977
for (Object key : properties.keySet()) {
7078
String annotation = key.toString().trim();
71-
if (!annotation.matches(LOMBOK_ANNOTATION.pattern())) {
72-
this.warnings.add("itfsw:插件" + LombokPlugin.class.getTypeName() + "不能识别的注解(" + annotation + ")!");
79+
if (!(annotation.matches(LOMBOK_ANNOTATION.pattern()) || PRO_SUPPORT_SUPER_BUILDER_FOR_IDEA.equals(annotation))) {
80+
warnings.add("itfsw:插件" + LombokPlugin.class.getTypeName() + "不能识别的注解(" + annotation + ")!");
7381
return false;
7482
}
7583
}
@@ -95,14 +103,17 @@ public void initialized(IntrospectedTable introspectedTable) {
95103
findData = true;
96104
}
97105

98-
if (StringUtility.isTrue(properties.getProperty(key.toString()))) {
106+
if (StringUtility.isTrue(properties.getProperty(key.toString())) && !PRO_SUPPORT_SUPER_BUILDER_FOR_IDEA.equals(annotation)) {
99107
this.annotations.add(annotation);
100108
}
101109
}
102110

103111
if (!findData) {
104112
this.annotations.add(0, "@Data");
105113
}
114+
115+
// 老版本IDEA SuperBuilder支持
116+
this.suportSuperBuilderForIdea = StringUtility.isTrue(properties.getProperty(PRO_SUPPORT_SUPER_BUILDER_FOR_IDEA));
106117
}
107118

108119
/**
@@ -113,7 +124,7 @@ public void initialized(IntrospectedTable introspectedTable) {
113124
*/
114125
@Override
115126
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
116-
this.addAnnotations(topLevelClass, introspectedTable, IntrospectedTableTools.getModelBaseRecordClomns(introspectedTable));
127+
this.addAnnotations(topLevelClass, introspectedTable, EnumModelType.MODEL_BASE_RECORD);
117128
return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
118129
}
119130

@@ -125,7 +136,7 @@ public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, Intros
125136
*/
126137
@Override
127138
public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
128-
this.addAnnotations(topLevelClass, introspectedTable, introspectedTable.getPrimaryKeyColumns());
139+
this.addAnnotations(topLevelClass, introspectedTable, EnumModelType.MODEL_PRIMARY_KEY);
129140
return super.modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
130141
}
131142

@@ -137,7 +148,7 @@ public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, Intros
137148
*/
138149
@Override
139150
public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
140-
this.addAnnotations(topLevelClass, introspectedTable, introspectedTable.getBLOBColumns());
151+
this.addAnnotations(topLevelClass, introspectedTable, EnumModelType.MODEL_RECORD_WITH_BLOBS);
141152
return super.modelRecordWithBLOBsClassGenerated(topLevelClass, introspectedTable);
142153
}
143154

@@ -183,9 +194,9 @@ public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelC
183194
* 添加注解
184195
* @param topLevelClass
185196
* @param introspectedTable
186-
* @param columns
197+
* @param modelType
187198
*/
188-
private void addAnnotations(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, List<IntrospectedColumn> columns) {
199+
private void addAnnotations(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, EnumModelType modelType) {
189200
for (String annotation : this.annotations) {
190201
// @Data
191202
if (annotation.startsWith("@Data")) {
@@ -197,12 +208,18 @@ private void addAnnotations(TopLevelClass topLevelClass, IntrospectedTable intro
197208
} else if (annotation.startsWith("@Builder")) {
198209
// TODO 配合IncrementsPlugin,以后删除
199210
boolean checkIncrementsPlugin = true;
200-
if (introspectedTable.getRules().generatePrimaryKeyClass() && topLevelClass.getType().getFullyQualifiedName().equals(introspectedTable.getPrimaryKeyType())) {
201-
checkIncrementsPlugin = PluginTools.getHook(ILombokPluginHook.class).modelPrimaryKeyBuilderClassGenerated(topLevelClass, columns, introspectedTable);
202-
} else if (introspectedTable.getRules().generateBaseRecordClass() && topLevelClass.getType().getFullyQualifiedName().equals(introspectedTable.getBaseRecordType())) {
203-
checkIncrementsPlugin = PluginTools.getHook(ILombokPluginHook.class).modelBaseRecordBuilderClassGenerated(topLevelClass, columns, introspectedTable);
204-
} else if (introspectedTable.getRules().generateRecordWithBLOBsClass() && topLevelClass.getType().getFullyQualifiedName().equals(introspectedTable.getRecordWithBLOBsType())) {
205-
checkIncrementsPlugin = PluginTools.getHook(ILombokPluginHook.class).modelRecordWithBLOBsBuilderClassGenerated(topLevelClass, columns, introspectedTable);
211+
if (!this.suportSuperBuilderForIdea) {
212+
switch (modelType) {
213+
case MODEL_PRIMARY_KEY:
214+
checkIncrementsPlugin = PluginTools.getHook(ILombokPluginHook.class).modelPrimaryKeyBuilderClassGenerated(topLevelClass, introspectedTable);
215+
break;
216+
case MODEL_BASE_RECORD:
217+
checkIncrementsPlugin = PluginTools.getHook(ILombokPluginHook.class).modelBaseRecordBuilderClassGenerated(topLevelClass, introspectedTable);
218+
break;
219+
case MODEL_RECORD_WITH_BLOBS:
220+
checkIncrementsPlugin = PluginTools.getHook(ILombokPluginHook.class).modelRecordWithBLOBsBuilderClassGenerated(topLevelClass, introspectedTable);
221+
break;
222+
}
206223
}
207224

208225
if (checkIncrementsPlugin) {
@@ -219,7 +236,25 @@ private void addAnnotations(TopLevelClass topLevelClass, IntrospectedTable intro
219236
}
220237

221238
if (topLevelClass.getSuperClass() != null || count >= 2) {
222-
this.addAnnotation(topLevelClass, "@SuperBuilder");
239+
if (this.suportSuperBuilderForIdea) {
240+
// TODO 兼容老版本
241+
PluginConfiguration configuration = new PluginConfiguration();
242+
configuration.setConfigurationType(ModelBuilderPlugin.class.getTypeName());
243+
ModelBuilderPlugin modelBuilderPlugin = (ModelBuilderPlugin) ObjectFactory.createPlugin(this.context, configuration);
244+
switch (modelType) {
245+
case MODEL_PRIMARY_KEY:
246+
modelBuilderPlugin.modelPrimaryKeyBuilderClassGenerated(topLevelClass, introspectedTable);
247+
break;
248+
case MODEL_BASE_RECORD:
249+
modelBuilderPlugin.modelBaseRecordBuilderClassGenerated(topLevelClass, introspectedTable);
250+
break;
251+
case MODEL_RECORD_WITH_BLOBS:
252+
modelBuilderPlugin.modelRecordWithBLOBsBuilderClassGenerated(topLevelClass, introspectedTable);
253+
break;
254+
}
255+
} else {
256+
this.addAnnotation(topLevelClass, "@SuperBuilder");
257+
}
223258
} else {
224259
this.addAnnotation(topLevelClass, annotation);
225260
}

src/main/java/com/itfsw/mybatis/generator/plugins/ModelBuilderPlugin.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@
2121
import com.itfsw.mybatis.generator.plugins.utils.JavaElementGeneratorTools;
2222
import com.itfsw.mybatis.generator.plugins.utils.PluginTools;
2323
import com.itfsw.mybatis.generator.plugins.utils.enhanced.InnerTypeFullyQualifiedJavaType;
24+
import com.itfsw.mybatis.generator.plugins.utils.hook.ILombokPluginHook;
2425
import com.itfsw.mybatis.generator.plugins.utils.hook.IModelBuilderPluginHook;
2526
import org.mybatis.generator.api.IntrospectedColumn;
2627
import org.mybatis.generator.api.IntrospectedTable;
2728
import org.mybatis.generator.api.dom.java.*;
2829
import org.mybatis.generator.internal.util.JavaBeansUtil;
2930

30-
import java.util.HashMap;
3131
import java.util.List;
32-
import java.util.Map;
3332

3433
/**
3534
* ---------------------------------------------------------------------------
@@ -39,9 +38,8 @@
3938
* @time:2016/12/28 14:56
4039
* ---------------------------------------------------------------------------
4140
*/
42-
public class ModelBuilderPlugin extends BasePlugin {
41+
public class ModelBuilderPlugin extends BasePlugin implements ILombokPluginHook {
4342
public static final String BUILDER_CLASS_NAME = "Builder"; // Builder 类名
44-
private Map<IntrospectedTable, InnerTypeFullyQualifiedJavaType> innerClasses = new HashMap<>();
4543

4644
/**
4745
* Model Methods 生成
@@ -86,6 +84,31 @@ public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, Intros
8684
return super.modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
8785
}
8886

87+
// ------------------------------------------------------- LombokPluginHook -------------------------------------------------------
88+
89+
@Override
90+
public boolean modelBaseRecordBuilderClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
91+
// 判断是否有生成Model的WithBLOBs类
92+
List<IntrospectedColumn> columns = introspectedTable.getRules().generateRecordWithBLOBsClass() ? introspectedTable.getNonBLOBColumns() : introspectedTable.getAllColumns();
93+
InnerClass innerClass = this.generateModelBuilder(topLevelClass, introspectedTable, columns);
94+
topLevelClass.addInnerClass(innerClass);
95+
return true;
96+
}
97+
98+
@Override
99+
public boolean modelPrimaryKeyBuilderClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
100+
InnerClass innerClass = this.generateModelBuilder(topLevelClass, introspectedTable, introspectedTable.getPrimaryKeyColumns());
101+
topLevelClass.addInnerClass(innerClass);
102+
return true;
103+
}
104+
105+
@Override
106+
public boolean modelRecordWithBLOBsBuilderClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
107+
InnerClass innerClass = this.generateModelBuilder(topLevelClass, introspectedTable, introspectedTable.getAllColumns());
108+
topLevelClass.addInnerClass(innerClass);
109+
return true;
110+
}
111+
89112
/**
90113
* 生成ModelBuilder
91114
* @param topLevelClass
@@ -98,15 +121,13 @@ private InnerClass generateModelBuilder(TopLevelClass topLevelClass, Introspecte
98121
InnerClass innerClass = new InnerClass(BUILDER_CLASS_NAME);
99122
innerClass.setVisibility(JavaVisibility.PUBLIC);
100123
innerClass.setStatic(true);
124+
if (topLevelClass.getSuperClass() != null) {
125+
innerClass.setSuperClass(topLevelClass.getSuperClass().getShortName() + "." + BUILDER_CLASS_NAME);
126+
}
101127

102128
// 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html
103129
// 顺序为 key base withBLOBs
104130
InnerTypeFullyQualifiedJavaType builderType = new InnerTypeFullyQualifiedJavaType(topLevelClass.getType().getFullyQualifiedName() + "." + BUILDER_CLASS_NAME);
105-
if (innerClasses.get(introspectedTable) != null) {
106-
innerClass.setSuperClass(innerClasses.get(introspectedTable));
107-
innerClasses.remove(introspectedTable);
108-
}
109-
innerClasses.put(introspectedTable, builderType);
110131

111132
// 增加静态builder方法实现和lombok一样
112133
Method builder = JavaElementGeneratorTools.generateMethod(
@@ -175,5 +196,4 @@ private InnerClass generateModelBuilder(TopLevelClass topLevelClass, Introspecte
175196

176197
return innerClass;
177198
}
178-
179199
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2019.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.itfsw.mybatis.generator.plugins.utils;
18+
19+
/**
20+
* ---------------------------------------------------------------------------
21+
*
22+
* ---------------------------------------------------------------------------
23+
* @author: hewei
24+
* @time:2019/7/12 10:13
25+
* ---------------------------------------------------------------------------
26+
*/
27+
public enum EnumModelType {
28+
MODEL_PRIMARY_KEY, MODEL_BASE_RECORD, MODEL_RECORD_WITH_BLOBS
29+
}

src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/HookAggregator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,29 +362,29 @@ public boolean logicalDeleteEnumGenerated(IntrospectedColumn logicalDeleteColumn
362362

363363

364364
@Override
365-
public boolean modelBaseRecordBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
365+
public boolean modelBaseRecordBuilderClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
366366
for (ILombokPluginHook plugin : this.getPlugins(ILombokPluginHook.class)) {
367-
if (!plugin.modelBaseRecordBuilderClassGenerated(topLevelClass, columns, introspectedTable)) {
367+
if (!plugin.modelBaseRecordBuilderClassGenerated(topLevelClass, introspectedTable)) {
368368
return false;
369369
}
370370
}
371371
return true;
372372
}
373373

374374
@Override
375-
public boolean modelPrimaryKeyBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
375+
public boolean modelPrimaryKeyBuilderClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
376376
for (ILombokPluginHook plugin : this.getPlugins(ILombokPluginHook.class)) {
377-
if (!plugin.modelPrimaryKeyBuilderClassGenerated(topLevelClass, columns, introspectedTable)) {
377+
if (!plugin.modelPrimaryKeyBuilderClassGenerated(topLevelClass, introspectedTable)) {
378378
return false;
379379
}
380380
}
381381
return true;
382382
}
383383

384384
@Override
385-
public boolean modelRecordWithBLOBsBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable) {
385+
public boolean modelRecordWithBLOBsBuilderClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
386386
for (ILombokPluginHook plugin : this.getPlugins(ILombokPluginHook.class)) {
387-
if (!plugin.modelRecordWithBLOBsBuilderClassGenerated(topLevelClass, columns, introspectedTable)) {
387+
if (!plugin.modelRecordWithBLOBsBuilderClassGenerated(topLevelClass, introspectedTable)) {
388388
return false;
389389
}
390390
}

src/main/java/com/itfsw/mybatis/generator/plugins/utils/hook/ILombokPluginHook.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616

1717
package com.itfsw.mybatis.generator.plugins.utils.hook;
1818

19-
import org.mybatis.generator.api.IntrospectedColumn;
2019
import org.mybatis.generator.api.IntrospectedTable;
2120
import org.mybatis.generator.api.dom.java.TopLevelClass;
2221

23-
import java.util.List;
24-
2522
/**
2623
* ---------------------------------------------------------------------------
2724
*
@@ -34,27 +31,24 @@ public interface ILombokPluginHook {
3431
/**
3532
* Model builder class 生成
3633
* @param topLevelClass
37-
* @param columns
3834
* @param introspectedTable
3935
* @return
4036
*/
41-
boolean modelBaseRecordBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable);
37+
boolean modelBaseRecordBuilderClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable);
4238

4339
/**
4440
* Model builder class 生成
4541
* @param topLevelClass
46-
* @param columns
4742
* @param introspectedTable
4843
* @return
4944
*/
50-
boolean modelPrimaryKeyBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable);
45+
boolean modelPrimaryKeyBuilderClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable);
5146

5247
/**
5348
* Model builder class 生成
5449
* @param topLevelClass
55-
* @param columns
5650
* @param introspectedTable
5751
* @return
5852
*/
59-
boolean modelRecordWithBLOBsBuilderClassGenerated(TopLevelClass topLevelClass, List<IntrospectedColumn> columns, IntrospectedTable introspectedTable);
53+
boolean modelRecordWithBLOBsBuilderClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable);
6054
}

0 commit comments

Comments
 (0)