1717package com .itfsw .mybatis .generator .plugins ;
1818
1919import 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 ;
2121import com .itfsw .mybatis .generator .plugins .utils .PluginTools ;
2222import com .itfsw .mybatis .generator .plugins .utils .hook .ILombokPluginHook ;
2323import org .mybatis .generator .api .IntrospectedColumn ;
2424import org .mybatis .generator .api .IntrospectedTable ;
2525import org .mybatis .generator .api .dom .java .Method ;
2626import org .mybatis .generator .api .dom .java .TopLevelClass ;
27+ import org .mybatis .generator .config .PluginConfiguration ;
28+ import org .mybatis .generator .internal .ObjectFactory ;
2729import org .mybatis .generator .internal .util .StringUtility ;
2830
2931import java .util .ArrayList ;
4244 * ---------------------------------------------------------------------------
4345 */
4446public 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 }
0 commit comments