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