View Javadoc
1   /*
2    *    Copyright 2006-2026 the original author or authors.
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    *       https://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  package org.mybatis.generator.plugins;
17  
18  import java.util.List;
19  import java.util.Properties;
20  
21  import org.mybatis.generator.api.IntrospectedTable;
22  import org.mybatis.generator.api.PluginAdapter;
23  import org.mybatis.generator.api.dom.java.Field;
24  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
25  import org.mybatis.generator.api.dom.java.JavaVisibility;
26  import org.mybatis.generator.api.dom.java.TopLevelClass;
27  import org.mybatis.generator.api.dom.kotlin.KotlinFile;
28  import org.mybatis.generator.api.dom.kotlin.KotlinType;
29  
30  /**
31   * This plugin adds the java.io.Serializable marker interface to all generated
32   * model objects.
33   *
34   * <p>This plugin demonstrates adding capabilities to generated Java artifacts, and
35   * shows the proper way to add imports to a compilation unit.
36   *
37   * <p>Important: This is a simplistic implementation of serializable and does not
38   * attempt to do any versioning of classes.
39   *
40   * @author Jeff Butler
41   */
42  public class SerializablePlugin extends PluginAdapter {
43  
44      private final FullyQualifiedJavaType serializable;
45      private final FullyQualifiedJavaType gwtSerializable;
46      private boolean addGWTInterface;
47      private boolean suppressJavaInterface;
48  
49      public SerializablePlugin() {
50          super();
51          serializable = new FullyQualifiedJavaType("java.io.Serializable"); //$NON-NLS-1$
52          gwtSerializable = new FullyQualifiedJavaType("com.google.gwt.user.client.rpc.IsSerializable"); //$NON-NLS-1$
53      }
54  
55      @Override
56      public boolean validate(List<String> warnings) {
57          // this plugin is always valid
58          return true;
59      }
60  
61      @Override
62      public void setProperties(Properties properties) {
63          super.setProperties(properties);
64          addGWTInterface = Boolean.parseBoolean(properties.getProperty("addGWTInterface")); //$NON-NLS-1$
65          suppressJavaInterface = Boolean.parseBoolean(properties.getProperty("suppressJavaInterface")); //$NON-NLS-1$
66      }
67  
68      @Override
69      public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
70              IntrospectedTable introspectedTable) {
71          return makeSerializable(topLevelClass, introspectedTable);
72      }
73  
74      @Override
75      public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
76              IntrospectedTable introspectedTable) {
77          return makeSerializable(topLevelClass, introspectedTable);
78      }
79  
80      @Override
81      public boolean modelRecordWithBLOBsClassGenerated(
82              TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
83          return makeSerializable(topLevelClass, introspectedTable);
84      }
85  
86      protected boolean makeSerializable(TopLevelClass topLevelClass,
87              IntrospectedTable introspectedTable) {
88          if (addGWTInterface) {
89              topLevelClass.addImportedType(gwtSerializable);
90              topLevelClass.addSuperInterface(gwtSerializable);
91          }
92  
93          if (!suppressJavaInterface) {
94              topLevelClass.addImportedType(serializable);
95              topLevelClass.addSuperInterface(serializable);
96  
97              Field field = new Field("serialVersionUID", //$NON-NLS-1$
98                      new FullyQualifiedJavaType("long")); //$NON-NLS-1$
99              field.setFinal(true);
100             field.setInitializationString("1L"); //$NON-NLS-1$
101             field.setStatic(true);
102             field.setVisibility(JavaVisibility.PRIVATE);
103 
104             commentGenerator.addFieldAnnotation(field, introspectedTable, topLevelClass.getImportedTypes());
105 
106             topLevelClass.addField(field);
107         }
108 
109         return true;
110     }
111 
112     @Override
113     public boolean kotlinDataClassGenerated(KotlinFile kotlinFile, KotlinType dataClass,
114             IntrospectedTable introspectedTable) {
115         kotlinFile.addImport("java.io.Serializable"); //$NON-NLS-1$
116         dataClass.addSuperType("Serializable"); //$NON-NLS-1$
117         return true;
118     }
119 }