• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / generator / 1945

14 Jan 2026 02:23PM UTC coverage: 88.838% (+0.04%) from 88.799%
1945

Pull #1414

github

web-flow
Merge 5eec5583d into 5d3363986
Pull Request #1414: Major Refactoring - Context is now configuration only, Plugins Potentially Impacted

2347 of 3184 branches covered (73.71%)

490 of 582 new or added lines in 133 files covered. (84.19%)

2 existing lines in 1 file now uncovered.

11517 of 12964 relevant lines covered (88.84%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.18
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/plugins/SerializablePlugin.java
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.IntrospectedTable.TargetRuntime;
23
import org.mybatis.generator.api.PluginAdapter;
24
import org.mybatis.generator.api.dom.java.Field;
25
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
26
import org.mybatis.generator.api.dom.java.JavaVisibility;
27
import org.mybatis.generator.api.dom.java.TopLevelClass;
28
import org.mybatis.generator.api.dom.kotlin.KotlinFile;
29
import org.mybatis.generator.api.dom.kotlin.KotlinType;
30

31
/**
32
 * This plugin adds the java.io.Serializable marker interface to all generated
33
 * model objects.
34
 *
35
 * <p>This plugin demonstrates adding capabilities to generated Java artifacts, and
36
 * shows the proper way to add imports to a compilation unit.
37
 *
38
 * <p>Important: This is a simplistic implementation of serializable and does not
39
 * attempt to do any versioning of classes.
40
 *
41
 * @author Jeff Butler
42
 */
43
public class SerializablePlugin extends PluginAdapter {
44

45
    private final FullyQualifiedJavaType serializable;
46
    private final FullyQualifiedJavaType gwtSerializable;
47
    private boolean addGWTInterface;
48
    private boolean suppressJavaInterface;
49

50
    public SerializablePlugin() {
51
        super();
1✔
52
        serializable = new FullyQualifiedJavaType("java.io.Serializable"); //$NON-NLS-1$
1✔
53
        gwtSerializable = new FullyQualifiedJavaType("com.google.gwt.user.client.rpc.IsSerializable"); //$NON-NLS-1$
1✔
54
    }
1✔
55

56
    @Override
57
    public boolean validate(List<String> warnings) {
58
        // this plugin is always valid
59
        return true;
1✔
60
    }
61

62
    @Override
63
    public void setProperties(Properties properties) {
64
        super.setProperties(properties);
1✔
65
        addGWTInterface = Boolean.parseBoolean(properties.getProperty("addGWTInterface")); //$NON-NLS-1$
1✔
66
        suppressJavaInterface = Boolean.parseBoolean(properties.getProperty("suppressJavaInterface")); //$NON-NLS-1$
1✔
67
    }
1✔
68

69
    @Override
70
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
71
            IntrospectedTable introspectedTable) {
72
        makeSerializable(topLevelClass, introspectedTable);
1✔
73
        return true;
1✔
74
    }
75

76
    @Override
77
    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
78
            IntrospectedTable introspectedTable) {
79
        makeSerializable(topLevelClass, introspectedTable);
1✔
80
        return true;
1✔
81
    }
82

83
    @Override
84
    public boolean modelRecordWithBLOBsClassGenerated(
85
            TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
86
        makeSerializable(topLevelClass, introspectedTable);
1✔
87
        return true;
1✔
88
    }
89

90
    protected void makeSerializable(TopLevelClass topLevelClass,
91
            IntrospectedTable introspectedTable) {
92
        if (addGWTInterface) {
1!
93
            topLevelClass.addImportedType(gwtSerializable);
×
94
            topLevelClass.addSuperInterface(gwtSerializable);
×
95
        }
96

97
        if (!suppressJavaInterface) {
1!
98
            topLevelClass.addImportedType(serializable);
1✔
99
            topLevelClass.addSuperInterface(serializable);
1✔
100

101
            Field field = new Field("serialVersionUID", //$NON-NLS-1$
1✔
102
                    new FullyQualifiedJavaType("long")); //$NON-NLS-1$
103
            field.setFinal(true);
1✔
104
            field.setInitializationString("1L"); //$NON-NLS-1$
1✔
105
            field.setStatic(true);
1✔
106
            field.setVisibility(JavaVisibility.PRIVATE);
1✔
107

108
            if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3_DSQL) {
1!
NEW
109
                commentGenerator.addFieldAnnotation(field, introspectedTable, topLevelClass.getImportedTypes());
×
110
            } else {
111
                commentGenerator.addFieldComment(field, introspectedTable);
1✔
112
            }
113

114
            topLevelClass.addField(field);
1✔
115
        }
116
    }
1✔
117

118
    @Override
119
    public boolean kotlinDataClassGenerated(KotlinFile kotlinFile, KotlinType dataClass,
120
            IntrospectedTable introspectedTable) {
121
        kotlinFile.addImport("java.io.Serializable"); //$NON-NLS-1$
1✔
122
        dataClass.addSuperType("Serializable"); //$NON-NLS-1$
1✔
123
        return true;
1✔
124
    }
125
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc