Skip to content

Commit

Permalink
Improve error message for indexed property
Browse files Browse the repository at this point in the history
  • Loading branch information
harawata committed Jan 26, 2024
1 parent c2f18b7 commit cc2c974
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Expand Up @@ -43,6 +43,10 @@ protected Object resolveCollection(PropertyTokenizer prop, Object object) {
}

protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
if (collection == null) {
throw new ReflectionException("Cannot get the value '" + prop.getIndexedName() + "' because the property '"
+ prop.getName() + "' is null.");
}
if (collection instanceof Map) {
return ((Map) collection).get(prop.getIndex());
}
Expand All @@ -68,8 +72,8 @@ protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
} else if (collection instanceof short[]) {
return ((short[]) collection)[i];
} else {
throw new ReflectionException(
"The '" + prop.getName() + "' property of " + collection + " is not a List or Array.");
throw new ReflectionException("Cannot get the value '" + prop.getIndexedName() + "' because the property '"
+ prop.getName() + "' is not Map, List or Array.");
}
}

Expand Down
Expand Up @@ -59,8 +59,7 @@ void assertBasicOperations() {
metaObj.getValue("bean2List[0]");
fail();
} catch (ReflectionException e) {
// TODO: more accurate message
assertEquals("The 'bean2List' property of null is not a List or Array.", e.getMessage());
assertEquals("Cannot get the value 'bean2List[0]' because the property 'bean2List' is null.", e.getMessage());
}
assertTrue(metaObj.hasSetter("bean2List[0]"));

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

metaObj.setValue("attrVal", "value");
assertEquals("value", bean.getAttrVal());
try {
metaObj.getValue("attrVal[0]");
fail();
} catch (ReflectionException e) {
assertEquals("Cannot get the value 'attrVal[0]' because the property 'attrVal' is not Map, List or Array.",
e.getMessage());
}

metaObj.setValue("bean2List[1].name", "new name 1");
assertEquals("new name 1", bean.getBean2List().get(1).getName());
Expand Down Expand Up @@ -144,7 +150,6 @@ void assertBasicOperations() {
} catch (UnsupportedOperationException e) {
// pass
}

}

static class Bean1 {
Expand Down

0 comments on commit cc2c974

Please sign in to comment.