Skip to content

Commit e4bd334

Browse files
committed
Merge pull request #64 from domaframework/issue-63
イミュータブルなエンティティの取得でNullPointerException が発生するバグを修正しました fix #63
2 parents f958fd7 + 0a9aec3 commit e4bd334

File tree

6 files changed

+325
-7
lines changed

6 files changed

+325
-7
lines changed

src/main/java/org/seasar/doma/internal/apt/EntityTypeGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ protected void printNewEntityMethod() {
610610
.getAllPropertyMetasInCtorArgsOrder().iterator(); it
611611
.hasNext();) {
612612
EntityPropertyMeta propertyMeta = it.next();
613-
iprint(" (%1$s)__args.get(\"%2$s\").get()",
613+
iprint(" (%1$s)(__args.containsKey(\"%2$s\") ? __args.get(\"%2$s\").get() : null)",
614614
TypeMirrorUtil.boxIfPrimitive(
615615
propertyMeta.getType(), env),
616616
propertyMeta.getName());
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2004-2010 the Seasar Foundation and the Others.
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,
13+
* either express or implied. See the License for the specific language
14+
* governing permissions and limitations under the License.
15+
*/
16+
package example.entity;
17+
18+
import java.io.Serializable;
19+
import java.math.BigDecimal;
20+
21+
import org.seasar.doma.Entity;
22+
import org.seasar.doma.Id;
23+
import org.seasar.doma.Version;
24+
import org.seasar.doma.jdbc.entity.NamingType;
25+
26+
@Entity(naming = NamingType.SNAKE_UPPER_CASE, immutable = true)
27+
public class ImmutableEmp implements Serializable {
28+
29+
private static final long serialVersionUID = 1L;
30+
31+
@Id
32+
final Integer id;
33+
34+
final String name;
35+
36+
final BigDecimal salary;
37+
38+
@Version
39+
final Integer version;
40+
41+
public ImmutableEmp(Integer id, String name, BigDecimal salary,
42+
Integer version) {
43+
super();
44+
this.id = id;
45+
this.name = name;
46+
this.salary = salary;
47+
this.version = version;
48+
}
49+
50+
public Integer getId() {
51+
return id;
52+
}
53+
54+
public String getName() {
55+
return name;
56+
}
57+
58+
public BigDecimal getSalary() {
59+
return salary;
60+
}
61+
62+
public Integer getVersion() {
63+
return version;
64+
}
65+
66+
}
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
* Copyright 2004-2010 the Seasar Foundation and the Others.
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,
13+
* either express or implied. See the License for the specific language
14+
* governing permissions and limitations under the License.
15+
*/
16+
package example.entity;
17+
18+
import java.math.BigDecimal;
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import javax.annotation.Generated;
26+
27+
import org.seasar.doma.jdbc.entity.AbstractEntityType;
28+
import org.seasar.doma.jdbc.entity.AssignedIdPropertyType;
29+
import org.seasar.doma.jdbc.entity.DefaultPropertyType;
30+
import org.seasar.doma.jdbc.entity.EntityPropertyType;
31+
import org.seasar.doma.jdbc.entity.GeneratedIdPropertyType;
32+
import org.seasar.doma.jdbc.entity.NamingType;
33+
import org.seasar.doma.jdbc.entity.PostDeleteContext;
34+
import org.seasar.doma.jdbc.entity.PostInsertContext;
35+
import org.seasar.doma.jdbc.entity.PostUpdateContext;
36+
import org.seasar.doma.jdbc.entity.PreDeleteContext;
37+
import org.seasar.doma.jdbc.entity.PreInsertContext;
38+
import org.seasar.doma.jdbc.entity.PreUpdateContext;
39+
import org.seasar.doma.jdbc.entity.Property;
40+
import org.seasar.doma.jdbc.entity.VersionPropertyType;
41+
42+
@Generated("")
43+
public class _ImmutableEmp extends AbstractEntityType<ImmutableEmp> {
44+
45+
private static _ImmutableEmp singleton = new _ImmutableEmp();
46+
47+
public final AssignedIdPropertyType<Object, ImmutableEmp, Integer, Object> id = new AssignedIdPropertyType<>(
48+
ImmutableEmp.class, Integer.class, Integer.class,
49+
() -> new org.seasar.doma.wrapper.IntegerWrapper(), null, null,
50+
"id", "ID", false);
51+
52+
public final DefaultPropertyType<Object, ImmutableEmp, String, Object> name = new DefaultPropertyType<>(
53+
ImmutableEmp.class, String.class, String.class,
54+
() -> new org.seasar.doma.wrapper.StringWrapper(), null, null,
55+
"name", "NAME", true, true, false);
56+
57+
public final DefaultPropertyType<Object, ImmutableEmp, BigDecimal, BigDecimal> salary = new DefaultPropertyType<>(
58+
ImmutableEmp.class, BigDecimal.class, BigDecimal.class,
59+
() -> new org.seasar.doma.wrapper.BigDecimalWrapper(), null, null,
60+
"salary", "SALARY", true, true, false);
61+
62+
public final VersionPropertyType<Object, ImmutableEmp, Integer, Integer> version = new VersionPropertyType<>(
63+
ImmutableEmp.class, Integer.class, Integer.class,
64+
() -> new org.seasar.doma.wrapper.IntegerWrapper(), null, null,
65+
"version", "VERSION", false);
66+
67+
private final String __name = "Emp";
68+
69+
private final String __catalogName = null;
70+
71+
private final String __schemaName = null;
72+
73+
private final String __tableName = "EMP";
74+
75+
private final NamingType __namingType = NamingType.UPPER_CASE;
76+
77+
private final List<EntityPropertyType<ImmutableEmp, ?>> __idPropertyTypes;
78+
79+
private final List<EntityPropertyType<ImmutableEmp, ?>> __entityPropertyTypes;
80+
81+
private final Map<String, EntityPropertyType<ImmutableEmp, ?>> __entityPropertyTypeMap;
82+
83+
private _ImmutableEmp() {
84+
List<EntityPropertyType<ImmutableEmp, ?>> __idList = new ArrayList<>();
85+
__idList.add(id);
86+
__idPropertyTypes = Collections.unmodifiableList(__idList);
87+
List<EntityPropertyType<ImmutableEmp, ?>> __list = new ArrayList<>();
88+
__list.add(id);
89+
__list.add(name);
90+
__list.add(salary);
91+
__list.add(version);
92+
__entityPropertyTypes = Collections.unmodifiableList(__list);
93+
Map<String, EntityPropertyType<ImmutableEmp, ?>> __map = new HashMap<>();
94+
__map.put("id", id);
95+
__map.put("name", name);
96+
__map.put("salary", salary);
97+
__map.put("version", version);
98+
__entityPropertyTypeMap = Collections.unmodifiableMap(__map);
99+
}
100+
101+
@Override
102+
public boolean isImmutable() {
103+
return false;
104+
}
105+
106+
@Override
107+
public ImmutableEmp newEntity(Map<String, Property<ImmutableEmp, ?>> args) {
108+
return new ImmutableEmp((Integer) (args.containsKey("id") ? args.get(
109+
"id").get() : null), (String) (args.containsKey("name") ? args
110+
.get("name").get() : null),
111+
(BigDecimal) (args.containsKey("salary") ? args.get("salary")
112+
.get() : null),
113+
(Integer) (args.containsKey("version") ? args.get("version")
114+
.get() : null));
115+
}
116+
117+
@Override
118+
public Class<ImmutableEmp> getEntityClass() {
119+
return ImmutableEmp.class;
120+
}
121+
122+
@Override
123+
public String getName() {
124+
return __name;
125+
}
126+
127+
@Override
128+
public List<EntityPropertyType<ImmutableEmp, ?>> getEntityPropertyTypes() {
129+
return __entityPropertyTypes;
130+
}
131+
132+
@Override
133+
public EntityPropertyType<ImmutableEmp, ?> getEntityPropertyType(
134+
String propertyName) {
135+
return __entityPropertyTypeMap.get(propertyName);
136+
}
137+
138+
@Override
139+
public void saveCurrentStates(ImmutableEmp __entity) {
140+
}
141+
142+
@Override
143+
public ImmutableEmp getOriginalStates(ImmutableEmp entity) {
144+
return null;
145+
}
146+
147+
@Override
148+
public GeneratedIdPropertyType<Object, ImmutableEmp, ?, ?> getGeneratedIdPropertyType() {
149+
return null;
150+
}
151+
152+
@Override
153+
public VersionPropertyType<Object, ImmutableEmp, ?, ?> getVersionPropertyType() {
154+
return version;
155+
}
156+
157+
@Override
158+
public List<EntityPropertyType<ImmutableEmp, ?>> getIdPropertyTypes() {
159+
return __idPropertyTypes;
160+
}
161+
162+
@Override
163+
public void preInsert(ImmutableEmp entity,
164+
PreInsertContext<ImmutableEmp> context) {
165+
}
166+
167+
@Override
168+
public void preUpdate(ImmutableEmp entity,
169+
PreUpdateContext<ImmutableEmp> context) {
170+
}
171+
172+
@Override
173+
public void preDelete(ImmutableEmp entity,
174+
PreDeleteContext<ImmutableEmp> context) {
175+
}
176+
177+
@Override
178+
public void postInsert(ImmutableEmp entity,
179+
PostInsertContext<ImmutableEmp> context) {
180+
}
181+
182+
@Override
183+
public void postUpdate(ImmutableEmp entity,
184+
PostUpdateContext<ImmutableEmp> context) {
185+
}
186+
187+
@Override
188+
public void postDelete(ImmutableEmp entity,
189+
PostDeleteContext<ImmutableEmp> context) {
190+
}
191+
192+
@Override
193+
public String getCatalogName() {
194+
return __catalogName;
195+
}
196+
197+
@Override
198+
public String getSchemaName() {
199+
return __schemaName;
200+
}
201+
202+
@Override
203+
public String getTableName() {
204+
return __tableName;
205+
}
206+
207+
@Override
208+
public NamingType getNamingType() {
209+
return __namingType;
210+
}
211+
212+
@Override
213+
public boolean isQuoteRequired() {
214+
return false;
215+
}
216+
217+
public static _ImmutableEmp getSingletonInternal() {
218+
return singleton;
219+
}
220+
}

src/test/java/org/seasar/doma/jdbc/entity/EntityTypeTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515
*/
1616
package org.seasar.doma.jdbc.entity;
1717

18+
import java.math.BigDecimal;
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
1822
import junit.framework.TestCase;
1923
import example.entity.Emp;
24+
import example.entity.ImmutableEmp;
2025
import example.entity._Emp;
26+
import example.entity._ImmutableEmp;
2127

2228
/**
2329
* @author nakamura-to
@@ -29,4 +35,30 @@ public void test() throws Exception {
2935
EntityType<Emp> entityType = _Emp.getSingletonInternal();
3036
entityType.getQualifiedTableName();
3137
}
38+
39+
public void testImmutable_newEntity() throws Exception {
40+
ImmutableEmp emp = new ImmutableEmp(99, "hoge", BigDecimal.ONE, 1);
41+
EntityType<ImmutableEmp> entityType = _ImmutableEmp
42+
.getSingletonInternal();
43+
Map<String, Property<ImmutableEmp, ?>> args = new HashMap<>();
44+
45+
EntityPropertyType<ImmutableEmp, ?> idType = entityType
46+
.getEntityPropertyType("id");
47+
Property<ImmutableEmp, ?> id = idType.createProperty();
48+
id.load(emp);
49+
args.put(idType.getName(), id);
50+
51+
EntityPropertyType<ImmutableEmp, ?> salaryType = entityType
52+
.getEntityPropertyType("salary");
53+
Property<ImmutableEmp, ?> salary = salaryType.createProperty();
54+
salary.load(emp);
55+
args.put(salaryType.getName(), salary);
56+
57+
ImmutableEmp newEmp = entityType.newEntity(args);
58+
59+
assertEquals(Integer.valueOf(99), newEmp.getId());
60+
assertNull(newEmp.getName());
61+
assertEquals(BigDecimal.ONE, newEmp.getSalary());
62+
assertNull(newEmp.getVersion());
63+
}
3264
}

src/test/resources/org/seasar/doma/internal/apt/entity/EntityProcessorTest_ImmutableChildEntity.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ public final class _ImmutableChildEntity extends org.seasar.doma.jdbc.entity.Abs
175175
@Override
176176
public org.seasar.doma.internal.apt.entity.ImmutableChildEntity newEntity(java.util.Map<String, org.seasar.doma.jdbc.entity.Property<org.seasar.doma.internal.apt.entity.ImmutableChildEntity, ?>> __args) {
177177
return new org.seasar.doma.internal.apt.entity.ImmutableChildEntity(
178-
(java.lang.Integer)__args.get("aaa").get(),
179-
(java.lang.Integer)__args.get("bbb").get(),
180-
(java.lang.String)__args.get("ccc").get());
178+
(java.lang.Integer)(__args.containsKey("aaa") ? __args.get("aaa").get() : null),
179+
(java.lang.Integer)(__args.containsKey("bbb") ? __args.get("bbb").get() : null),
180+
(java.lang.String)(__args.containsKey("ccc") ? __args.get("ccc").get() : null));
181181
}
182182

183183
@Override

src/test/resources/org/seasar/doma/internal/apt/entity/EntityProcessorTest_ImmutableEntity.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ public final class _ImmutableEntity extends org.seasar.doma.jdbc.entity.Abstract
175175
@Override
176176
public org.seasar.doma.internal.apt.entity.ImmutableEntity newEntity(java.util.Map<String, org.seasar.doma.jdbc.entity.Property<org.seasar.doma.internal.apt.entity.ImmutableEntity, ?>> __args) {
177177
return new org.seasar.doma.internal.apt.entity.ImmutableEntity(
178-
(java.lang.String)__args.get("aaa").get(),
179-
(java.lang.Integer)__args.get("bbb").get(),
180-
(java.lang.Integer)__args.get("ccc").get());
178+
(java.lang.String)(__args.containsKey("aaa") ? __args.get("aaa").get() : null),
179+
(java.lang.Integer)(__args.containsKey("bbb") ? __args.get("bbb").get() : null),
180+
(java.lang.Integer)(__args.containsKey("ccc") ? __args.get("ccc").get() : null));
181181
}
182182

183183
@Override

0 commit comments

Comments
 (0)