Skip to content

Commit f09520e

Browse files
committed
Improve error message for indexed property
1 parent c2f18b7 commit f09520e

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/main/java/org/apache/ibatis/reflection/wrapper/BaseWrapper.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ protected Object resolveCollection(PropertyTokenizer prop, Object object) {
4343
}
4444

4545
protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
46+
if (collection == null) {
47+
throw new ReflectionException("Cannot get the value '" + prop.getIndexedName() + "' because the property '"
48+
+ prop.getName() + "' is null.");
49+
}
4650
if (collection instanceof Map) {
4751
return ((Map) collection).get(prop.getIndex());
4852
}
@@ -68,12 +72,16 @@ protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
6872
} else if (collection instanceof short[]) {
6973
return ((short[]) collection)[i];
7074
} else {
71-
throw new ReflectionException(
72-
"The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
75+
throw new ReflectionException("Cannot get the value '" + prop.getIndexedName() + "' because the property '"
76+
+ prop.getName() + "' is not Map, List or Array.");
7377
}
7478
}
7579

7680
protected void setCollectionValue(PropertyTokenizer prop, Object collection, Object value) {
81+
if (collection == null) {
82+
throw new ReflectionException("Cannot set the value '" + prop.getIndexedName() + "' because the property '"
83+
+ prop.getName() + "' is null.");
84+
}
7785
if (collection instanceof Map) {
7886
((Map) collection).put(prop.getIndex(), value);
7987
} else {
@@ -99,8 +107,8 @@ protected void setCollectionValue(PropertyTokenizer prop, Object collection, Obj
99107
} else if (collection instanceof short[]) {
100108
((short[]) collection)[i] = (Short) value;
101109
} else {
102-
throw new ReflectionException(
103-
"The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
110+
throw new ReflectionException("Cannot set the value '" + prop.getIndexedName() + "' because the property '"
111+
+ prop.getName() + "' is not Map, List or Array.");
104112
}
105113
}
106114
}

src/test/java/org/apache/ibatis/reflection/wrapper/BeanWrapperTest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ void assertBasicOperations() {
5959
metaObj.getValue("bean2List[0]");
6060
fail();
6161
} catch (ReflectionException e) {
62-
// TODO: more accurate message
63-
assertEquals("The 'bean2List' property of null is not a List or Array.", e.getMessage());
62+
assertEquals("Cannot get the value 'bean2List[0]' because the property 'bean2List' is null.", e.getMessage());
63+
}
64+
try {
65+
metaObj.setValue("bean2List[0]", new Bean2());
66+
fail();
67+
} catch (ReflectionException e) {
68+
assertEquals("Cannot set the value 'bean2List[0]' because the property 'bean2List' is null.", e.getMessage());
6469
}
6570
assertTrue(metaObj.hasSetter("bean2List[0]"));
6671

@@ -77,6 +82,20 @@ void assertBasicOperations() {
7782

7883
metaObj.setValue("attrVal", "value");
7984
assertEquals("value", bean.getAttrVal());
85+
try {
86+
metaObj.getValue("attrVal[0]");
87+
fail();
88+
} catch (ReflectionException e) {
89+
assertEquals("Cannot get the value 'attrVal[0]' because the property 'attrVal' is not Map, List or Array.",
90+
e.getMessage());
91+
}
92+
try {
93+
metaObj.setValue("attrVal[0]", "blur");
94+
fail();
95+
} catch (ReflectionException e) {
96+
assertEquals("Cannot set the value 'attrVal[0]' because the property 'attrVal' is not Map, List or Array.",
97+
e.getMessage());
98+
}
8099

81100
metaObj.setValue("bean2List[1].name", "new name 1");
82101
assertEquals("new name 1", bean.getBean2List().get(1).getName());
@@ -144,7 +163,6 @@ void assertBasicOperations() {
144163
} catch (UnsupportedOperationException e) {
145164
// pass
146165
}
147-
148166
}
149167

150168
static class Bean1 {

0 commit comments

Comments
 (0)