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

mybatis / generator / 2138

02 Apr 2026 07:24PM UTC coverage: 91.775% (+1.4%) from 90.382%
2138

push

github

web-flow
Merge pull request #1484 from mybatis/renovate/org.apache.ant-ant-1.x

Update dependency org.apache.ant:ant to v1.10.16

2425 of 3124 branches covered (77.62%)

11884 of 12949 relevant lines covered (91.78%)

0.92 hits per line

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

93.1
/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.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();
1✔
51
        serializable = new FullyQualifiedJavaType("java.io.Serializable"); //$NON-NLS-1$
1✔
52
        gwtSerializable = new FullyQualifiedJavaType("com.google.gwt.user.client.rpc.IsSerializable"); //$NON-NLS-1$
1✔
53
    }
1✔
54

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

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

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

74
    @Override
75
    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
76
            IntrospectedTable introspectedTable) {
77
        return makeSerializable(topLevelClass, introspectedTable);
1✔
78
    }
79

80
    @Override
81
    public boolean modelRecordWithBLOBsClassGenerated(
82
            TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
83
        return makeSerializable(topLevelClass, introspectedTable);
1✔
84
    }
85

86
    protected boolean makeSerializable(TopLevelClass topLevelClass,
87
            IntrospectedTable introspectedTable) {
88
        if (addGWTInterface) {
1!
89
            topLevelClass.addImportedType(gwtSerializable);
×
90
            topLevelClass.addSuperInterface(gwtSerializable);
×
91
        }
92

93
        if (!suppressJavaInterface) {
1!
94
            topLevelClass.addImportedType(serializable);
1✔
95
            topLevelClass.addSuperInterface(serializable);
1✔
96

97
            Field field = new Field("serialVersionUID", //$NON-NLS-1$
1✔
98
                    new FullyQualifiedJavaType("long")); //$NON-NLS-1$
99
            field.setFinal(true);
1✔
100
            field.setInitializationString("1L"); //$NON-NLS-1$
1✔
101
            field.setStatic(true);
1✔
102
            field.setVisibility(JavaVisibility.PRIVATE);
1✔
103

104
            commentGenerator.addFieldAnnotation(field, introspectedTable, topLevelClass.getImportedTypes());
1✔
105

106
            topLevelClass.addField(field);
1✔
107
        }
108

109
        return true;
1✔
110
    }
111

112
    @Override
113
    public boolean kotlinDataClassGenerated(KotlinFile kotlinFile, KotlinType dataClass,
114
            IntrospectedTable introspectedTable) {
115
        kotlinFile.addImport("java.io.Serializable"); //$NON-NLS-1$
1✔
116
        dataClass.addSuperType("Serializable"); //$NON-NLS-1$
1✔
117
        return true;
1✔
118
    }
119
}
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