Skip to content

Commit 7f95164

Browse files
committed
Add the useMappedSuperclass option
1 parent 090bbb5 commit 7f95164

File tree

16 files changed

+372
-11
lines changed

16 files changed

+372
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ domaCodeGen {
151151
| useAccessor | whether to use accessors or not | | `true` |
152152
| useListener | whether to use listeners or not | | `true` |
153153
| useMetamodel | whether to use metamodels or not | | `true` |
154+
| useMappedSuperclass | whether to use mapped superclasses or not | | `true` |
154155
| originalStatesPropertyName | property to be annotated with `@OriginalStates` | | |
155156
| entityPropertyClassNamesFile | file used to resolve entity property classes | | |
156157
| prefix | prefix for entity classes | | |

codegen/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
group=org.seasar.doma
2-
version=1.0.0
2+
version=1.1.0-SNAPSHOT

codegen/src/main/java/org/seasar/doma/gradle/codegen/GlobalFactory.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.seasar.doma.gradle.codegen.desc.EntityPropertyClassNameResolver;
1111
import org.seasar.doma.gradle.codegen.desc.EntityPropertyDescFactory;
1212
import org.seasar.doma.gradle.codegen.desc.GenerationType;
13+
import org.seasar.doma.gradle.codegen.desc.MappedSuperclassDescFactory;
1314
import org.seasar.doma.gradle.codegen.desc.NamingType;
1415
import org.seasar.doma.gradle.codegen.desc.SqlDescFactory;
1516
import org.seasar.doma.gradle.codegen.desc.SqlTestDescFactory;
@@ -77,7 +78,8 @@ public EntityDescFactory createEntityDescFactory(
7778
boolean showDbComment,
7879
boolean useAccessor,
7980
boolean useListener,
80-
boolean useMetamodel) {
81+
boolean useMetamodel,
82+
boolean useMappedSuperclass) {
8183
return new EntityDescFactory(
8284
packageName,
8385
superclass,
@@ -90,14 +92,20 @@ public EntityDescFactory createEntityDescFactory(
9092
showDbComment,
9193
useAccessor,
9294
useListener,
93-
useMetamodel);
95+
useMetamodel,
96+
useMappedSuperclass);
9497
}
9598

9699
public EntityListenerDescFactory createEntityListenerDescFactory(
97100
String packageName, String superclassName) {
98101
return new EntityListenerDescFactory(packageName, superclassName);
99102
}
100103

104+
public MappedSuperclassDescFactory createMappedSuperclassDescFactory(
105+
String packageName, String entitySuperclassName) {
106+
return new MappedSuperclassDescFactory(packageName, entitySuperclassName);
107+
}
108+
101109
public DaoDescFactory createDaoDescFactory(
102110
String packageName, String suffix, String configClassName) {
103111
return new DaoDescFactory(packageName, suffix, configClassName);

codegen/src/main/java/org/seasar/doma/gradle/codegen/desc/Constants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ public class Constants {
44

55
public static final String ENTITY_LISTENER_SUFFIX = "Listener";
66

7+
public static final String MAPPED_SUPER_CLASS_PREFIX = "Abstract";
8+
79
public static final String ENTITY_TEMPLATE = "entity.ftl";
810

911
public static final String ENTITY_LISTENER_TEMPLATE = "entityListener.ftl";
1012

13+
public static final String MAPPED_SUPERCLASS_TEMPLATE = "mappedSuperclass.ftl";
14+
1115
public static final String DAO_TEMPLATE = "dao.ftl";
1216

1317
public static final String SQL_TEST_CASE_TEMPLATE = "sqlTest.ftl";

codegen/src/main/java/org/seasar/doma/gradle/codegen/desc/EntityDesc.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class EntityDesc extends ClassDesc {
4040

4141
protected boolean useMetamodel;
4242

43+
protected boolean useMappedSuperclass;
44+
4345
protected boolean showDbComment;
4446

4547
protected String templateName;
@@ -191,6 +193,14 @@ public void setUseMetamodel(boolean useMetamodel) {
191193
this.useMetamodel = useMetamodel;
192194
}
193195

196+
public boolean isUseMappedSuperclass() {
197+
return useMappedSuperclass;
198+
}
199+
200+
public void setUseMappedSuperclass(boolean useMappedSuperclass) {
201+
this.useMappedSuperclass = useMappedSuperclass;
202+
}
203+
194204
public boolean isShowDbComment() {
195205
return showDbComment;
196206
}

codegen/src/main/java/org/seasar/doma/gradle/codegen/desc/EntityDescFactory.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class EntityDescFactory {
3232

3333
protected final boolean useMetamodel;
3434

35+
protected final boolean useMappedSuperclass;
36+
3537
protected final Class<?> superclass;
3638

3739
protected final EntityPropertyDescMerger entityPropertyDescMerger;
@@ -50,7 +52,8 @@ public EntityDescFactory(
5052
boolean showDbComment,
5153
boolean useAccessor,
5254
boolean useListener,
53-
boolean useMetamodel) {
55+
boolean useMetamodel,
56+
boolean useMappedSuperclass) {
5457
if (entityPropertyDescFactory == null) {
5558
throw new CodeGenNullPointerException("entityPropertyDescFactory");
5659
}
@@ -69,6 +72,7 @@ public EntityDescFactory(
6972
this.useAccessor = useAccessor;
7073
this.useListener = useListener;
7174
this.useMetamodel = useMetamodel;
75+
this.useMappedSuperclass = useMappedSuperclass;
7276
this.entityPropertyDescMerger = new EntityPropertyDescMerger(superclass);
7377
}
7478

@@ -93,10 +97,15 @@ public EntityDesc createEntityDesc(
9397
entityDesc.setTableName(tableMeta.getName());
9498
entityDesc.setQualifiedTableName(tableMeta.getQualifiedTableName());
9599
entityDesc.setPackageName(packageName);
96-
entityDesc.setEntityPrefix(StringUtil.defaultString(entityPrefix, ""));
97-
entityDesc.setEntitySuffix(StringUtil.defaultString(entitySuffix, ""));
100+
String prefix = StringUtil.defaultString(entityPrefix, "");
101+
entityDesc.setEntityPrefix(prefix);
102+
String suffix = StringUtil.defaultString(entitySuffix, "");
103+
entityDesc.setEntitySuffix(suffix);
98104
entityDesc.setSimpleName(simpleName);
99-
if (superclass != null) {
105+
if (useMappedSuperclass) {
106+
entityDesc.setSuperclassSimpleName(
107+
Constants.MAPPED_SUPER_CLASS_PREFIX + prefix + simpleName + suffix);
108+
} else if (superclass != null) {
100109
entityDesc.setSuperclassSimpleName(superclass.getSimpleName());
101110
}
102111
entityDesc.setListenerClassSimpleName(
@@ -113,6 +122,7 @@ public EntityDesc createEntityDesc(
113122
entityDesc.setUseAccessor(useAccessor);
114123
entityDesc.setUseListener(useListener);
115124
entityDesc.setUseMetamodel(useMetamodel);
125+
entityDesc.setUseMappedSuperclass(useMappedSuperclass);
116126
entityDesc.setTemplateName(Constants.ENTITY_TEMPLATE);
117127
handleShowTableName(entityDesc, tableMeta);
118128
handleEntityPropertyDesc(entityDesc, tableMeta);
@@ -158,7 +168,7 @@ protected void handleImportName(EntityDesc entityDesc, TableMeta tableMeta) {
158168
|| entityDesc.getTableName() != null) {
159169
classDescSupport.addImportName(entityDesc, ClassConstants.Table);
160170
}
161-
if (superclass != null) {
171+
if (!useMappedSuperclass && superclass != null) {
162172
classDescSupport.addImportName(entityDesc, superclass.getName());
163173
}
164174
if (namingType != NamingType.NONE) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.seasar.doma.gradle.codegen.desc;
2+
3+
public class MappedSuperclassDesc extends ClassDesc {
4+
5+
protected String entityClassSimpleName;
6+
7+
protected String superclassSimpleName;
8+
9+
protected String mappedSuperclassSimpleName;
10+
11+
protected EntityDesc entityDesc;
12+
13+
protected String templateName;
14+
15+
public void setTemplateName(String templateName) {
16+
this.templateName = templateName;
17+
}
18+
19+
public String getTemplateName() {
20+
return templateName;
21+
}
22+
23+
public String getEntityClassSimpleName() {
24+
return entityClassSimpleName;
25+
}
26+
27+
public void setEntityClassSimpleName(String entityClassSimpleName) {
28+
this.entityClassSimpleName = entityClassSimpleName;
29+
}
30+
31+
public String getSuperclassSimpleName() {
32+
return superclassSimpleName;
33+
}
34+
35+
public void setSuperclassSimpleName(String superclassSimpleName) {
36+
this.superclassSimpleName = superclassSimpleName;
37+
}
38+
39+
public String getMappedSuperclassSimpleName() {
40+
return mappedSuperclassSimpleName;
41+
}
42+
43+
public void setMappedSuperclassSimpleName(String mappedSuperclassSimpleName) {
44+
this.mappedSuperclassSimpleName = mappedSuperclassSimpleName;
45+
}
46+
47+
public EntityDesc getEntityDesc() {
48+
return entityDesc;
49+
}
50+
51+
public void setEntityDesc(EntityDesc entityDesc) {
52+
this.entityDesc = entityDesc;
53+
}
54+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.seasar.doma.gradle.codegen.desc;
2+
3+
import org.seasar.doma.gradle.codegen.util.ClassUtil;
4+
import org.seasar.doma.gradle.codegen.util.StringUtil;
5+
6+
public class MappedSuperclassDescFactory {
7+
8+
protected final String packageName;
9+
10+
protected final String entitySuperclassName;
11+
12+
protected final ClassDescSupport classDescSupport = new ClassDescSupport();
13+
14+
public MappedSuperclassDescFactory(String packageName, String entitySuperclassName) {
15+
this.packageName = packageName;
16+
this.entitySuperclassName = entitySuperclassName;
17+
}
18+
19+
public MappedSuperclassDesc createMappedSuperclassDesc(EntityDesc entityDesc) {
20+
MappedSuperclassDesc mappedSuperclassDesc = new MappedSuperclassDesc();
21+
mappedSuperclassDesc.setEntityDesc(entityDesc);
22+
mappedSuperclassDesc.setPackageName(entityDesc.getPackageName());
23+
String entityPrefix = StringUtil.defaultString(entityDesc.getEntityPrefix(), "");
24+
String entitySuffix = StringUtil.defaultString(entityDesc.getEntitySuffix(), "");
25+
String entityName = entityPrefix + entityDesc.getSimpleName() + entitySuffix;
26+
mappedSuperclassDesc.setSimpleName(Constants.MAPPED_SUPER_CLASS_PREFIX + entityName);
27+
mappedSuperclassDesc.setEntityClassSimpleName(entityDesc.getSimpleName());
28+
if (entitySuperclassName != null) {
29+
String simpleName = ClassUtil.getSimpleName(entitySuperclassName);
30+
mappedSuperclassDesc.setSuperclassSimpleName(simpleName);
31+
}
32+
mappedSuperclassDesc.setTemplateName(Constants.MAPPED_SUPERCLASS_TEMPLATE);
33+
handleImportName(mappedSuperclassDesc, entityDesc);
34+
return mappedSuperclassDesc;
35+
}
36+
37+
protected void handleImportName(
38+
MappedSuperclassDesc mappedSuperclassDesc, EntityDesc entityDesc) {
39+
classDescSupport.addImportName(mappedSuperclassDesc, entityDesc.getQualifiedName());
40+
if (entitySuperclassName != null) {
41+
classDescSupport.addImportName(mappedSuperclassDesc, entitySuperclassName);
42+
}
43+
}
44+
}

codegen/src/main/java/org/seasar/doma/gradle/codegen/extension/EntityConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public class EntityConfig {
4545

4646
protected Property<Boolean> useMetamodel;
4747

48+
protected Property<Boolean> useMappedSuperclass;
49+
4850
protected Property<String> originalStatesPropertyName;
4951

5052
protected RegularFileProperty entityPropertyClassNamesFile;
@@ -72,6 +74,7 @@ public EntityConfig(ObjectFactory objects) {
7274
useAccessor = objects.property(Boolean.class);
7375
useListener = objects.property(Boolean.class);
7476
useMetamodel = objects.property(Boolean.class);
77+
useMappedSuperclass = objects.property(Boolean.class);
7578
originalStatesPropertyName = objects.property(String.class);
7679
entityPropertyClassNamesFile = objects.fileProperty();
7780
prefix = objects.property(String.class);
@@ -88,6 +91,7 @@ public EntityConfig(ObjectFactory objects) {
8891
useAccessor.set(true);
8992
useListener.set(true);
9093
useMetamodel.set(true);
94+
useMappedSuperclass.set(true);
9195
}
9296

9397
@Internal
@@ -243,6 +247,15 @@ public void setUseMetamodel(boolean useMetamodel) {
243247
this.useMetamodel.set(useMetamodel);
244248
}
245249

250+
@Internal
251+
public Property<Boolean> getUseMappedSuperclass() {
252+
return useMappedSuperclass;
253+
}
254+
255+
public void setUseMappedSuperclass(boolean useMappedSuperclass) {
256+
this.useMappedSuperclass.set(useMappedSuperclass);
257+
}
258+
246259
@Internal
247260
public Property<String> getOriginalStatesPropertyName() {
248261
return originalStatesPropertyName;

codegen/src/main/java/org/seasar/doma/gradle/codegen/task/CodeGenDtoTask.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ private EntityDescFactory createEntityDescFactory(
207207
entityConfig.getShowDbComment().get(),
208208
entityConfig.getUseAccessor().get(),
209209
entityConfig.getUseListener().get(),
210-
entityConfig.getUseMetamodel().get());
210+
entityConfig.getUseMetamodel().get(),
211+
entityConfig.getUseMappedSuperclass().get());
211212
}
212213

213214
protected void generateDto(EntityDesc entityDesc) {

0 commit comments

Comments
 (0)