Skip to content

Commit 5a26fd1

Browse files
committed
Fix the Npe about use ternary operator
1 parent e30f97b commit 5a26fd1

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/main/java/org/apache/ibatis/parsing/XNode.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,7 @@ public String getStringBody() {
130130
}
131131

132132
public String getStringBody(String def) {
133-
if (body == null) {
134-
return def;
135-
} else {
136-
return body;
137-
}
133+
return body == null ? def : body;
138134
}
139135

140136
public Boolean getBooleanBody() {
@@ -219,6 +215,8 @@ public Boolean getBooleanAttribute(String name) {
219215

220216
public Boolean getBooleanAttribute(String name, Boolean def) {
221217
String value = attributes.getProperty(name);
218+
// Note that ternary operator will throw NPE in some scenarios, just be careful.
219+
// see this explanation[https://stackoverflow.com/questions/5246776/java-weird-nullpointerexception-in-ternary-operator/5246820#5246820]
222220
return value == null ? def : Boolean.valueOf(value);
223221
}
224222

@@ -228,7 +226,7 @@ public Integer getIntAttribute(String name) {
228226

229227
public Integer getIntAttribute(String name, Integer def) {
230228
String value = attributes.getProperty(name);
231-
return value == null ? def : Integer.parseInt(value);
229+
return value == null ? def : Integer.valueOf(value);
232230
}
233231

234232
public Long getLongAttribute(String name) {
@@ -237,7 +235,7 @@ public Long getLongAttribute(String name) {
237235

238236
public Long getLongAttribute(String name, Long def) {
239237
String value = attributes.getProperty(name);
240-
return value == null ? def : Long.parseLong(value);
238+
return value == null ? def : Long.valueOf(value);
241239
}
242240

243241
public Double getDoubleAttribute(String name) {
@@ -246,7 +244,7 @@ public Double getDoubleAttribute(String name) {
246244

247245
public Double getDoubleAttribute(String name, Double def) {
248246
String value = attributes.getProperty(name);
249-
return value == null ? def : Double.parseDouble(value);
247+
return value == null ? def : Double.valueOf(value);
250248
}
251249

252250
public Float getFloatAttribute(String name) {
@@ -255,7 +253,7 @@ public Float getFloatAttribute(String name) {
255253

256254
public Float getFloatAttribute(String name, Float def) {
257255
String value = attributes.getProperty(name);
258-
return value == null ? def : Float.parseFloat(value);
256+
return value == null ? def : Float.valueOf(value);
259257
}
260258

261259
public List<XNode> getChildren() {

0 commit comments

Comments
 (0)