Skip to content

Commit cc2c974

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

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

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

Lines changed: 6 additions & 2 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,8 +72,8 @@ 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

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ 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());
6463
}
6564
assertTrue(metaObj.hasSetter("bean2List[0]"));
6665

@@ -77,6 +76,13 @@ void assertBasicOperations() {
7776

7877
metaObj.setValue("attrVal", "value");
7978
assertEquals("value", bean.getAttrVal());
79+
try {
80+
metaObj.getValue("attrVal[0]");
81+
fail();
82+
} catch (ReflectionException e) {
83+
assertEquals("Cannot get the value 'attrVal[0]' because the property 'attrVal' is not Map, List or Array.",
84+
e.getMessage());
85+
}
8086

8187
metaObj.setValue("bean2List[1].name", "new name 1");
8288
assertEquals("new name 1", bean.getBean2List().get(1).getName());
@@ -144,7 +150,6 @@ void assertBasicOperations() {
144150
} catch (UnsupportedOperationException e) {
145151
// pass
146152
}
147-
148153
}
149154

150155
static class Bean1 {

0 commit comments

Comments
 (0)