[
http://issues.apache.org/jira/browse/BEANUTILS-252?page=comments#action_12447361
]
Niall Pemberton commented on BEANUTILS-252:
-------------------------------------------
You're right that BeanUtils doesn't support this, though I'm wondering why you
don't simply leave out the brackets all together? You can simply do the
following:
HashMap myMap = new HashMap();
myMap.put("key","value");
Request:
System.out.println(PropertyUtils.getProperty(myMap, "key"));
Another point is that the code you posted from PropertyUtilsBean has changed
since the last release - rather than accesssing the Map from the
getNestedProperty() method it now delegates to a new getPropertyOfMapBean()
method - which means you can override that method and implement your own custom
behaviour.
Theres a number of bugs open in BeanUtils regarding how the "keys" to Maps
should be handled - with opposing points of view on what characters should be
allowed. Therefore I think we should close this as WONT FIX since there is now
a mechanism for people to resovle this easily themselves.
> getNestedProperty doesn't work with maps properly
> -------------------------------------------------
>
> Key: BEANUTILS-252
> URL: http://issues.apache.org/jira/browse/BEANUTILS-252
> Project: Commons BeanUtils
> Issue Type: Bug
> Affects Versions: 1.7.0
> Environment: Any
> Reporter: Masker71
>
> Hello,
> Here is an example of PropertyUtils.getProperty use:
> HashMap myMap = new HashMap();
> myMap.put("key","value");
> Request:
> System.out.println(PropertyUtils.getProperty(myMap, "(key)"));
> Result:
> null
> Workaround:
> HashMap myMap = new HashMap();
> myMap.put("(key)","value");
> Request:
> System.out.println(PropertyUtils.getProperty(myMap, "(key)"));
> Result:
> value
> The reason of this behaviour is that in the implementation of
> PropertyUtilsBean.getNestedProperty function.
> Currently it doesn't extract 'key' from brackets. variable name is equal to
> "(key)" when this method is invoked
> and it is used to extract value from the map:
> indexOfINDEXED_DELIM = name.indexOf(PropertyUtils.INDEXED_DELIM);
> indexOfMAPPED_DELIM = name.indexOf(PropertyUtils.MAPPED_DELIM);
> if (bean instanceof Map) {
> bean = ((Map) bean).get(name);
> } else if (indexOfMAPPED_DELIM >= 0) {
> bean = getMappedProperty(bean, name);
> } else if (indexOfINDEXED_DELIM >= 0) {
> bean = getIndexedProperty(bean, name);
> } else {
> bean = getSimpleProperty(bean, name);
> }
> For the bean methods which returs maps key extraction is performed, why it
> isn't done for the Map? Cannot I use any string as a key in the map, why
> should I use keys enveloped in brackets? I think what is mentioned to do is
> that:
> indexOfINDEXED_DELIM = name.indexOf(PropertyUtils.INDEXED_DELIM);
> indexOfMAPPED_DELIM = name.indexOf(PropertyUtils.MAPPED_DELIM);
> indexOfMAPPED_DELIM2 = name.indexOf(PropertyUtils.MAPPED_DELIM2);
> if (bean instanceof Map) {
> if (indexOfMAPPED_DELIM >= 0 && indexOfMAPPED_DELIM2>=0)
> name = name.substring(indexOfMAPPED_DELIM+1,
> indexOfMAPPED_DELIM2);
> bean = ((Map) bean).get(name);
> }
> Hope description was clear enough and you will approve it as a bug.
> Thank you
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]