[
https://issues.apache.org/jira/browse/SLING-600?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12620063#action_12620063
]
Felix Meschberger commented on SLING-600:
-----------------------------------------
The problem is that the JcrPropertyMap.get(String, T) method uses the runtime
type of the object to select the type for the conversion in the convertToType
method. This results in the convertToType method in your example to be called
with the GregorianCalendar class which does not equal any of the checked
classes.
The fix is to special case some types supported by the convertToType method:
Calendar and Value. Both are interfaces of which the default value may be an
implementation.
> ValueMap#get(String s, T obj) does not work with java.util.Calendar
> -------------------------------------------------------------------
>
> Key: SLING-600
> URL: https://issues.apache.org/jira/browse/SLING-600
> Project: Sling
> Issue Type: Bug
> Components: JCR Resource
> Environment: Windows XP, Sling Version 2.0.3-incubator-SNAPSHOT
> (running on Communiqué5)
> Reporter: Andreas Vogl
> Assignee: Felix Meschberger
>
> I am using the sling api (Version 2.0.3-incubator-SNAPSHOT) to access
> properties from a JCR repository and have noticed something strange in the
> class
> org.apache.sling.api.resource.ValueMap
> The method "<T> T get(java.lang.String name, T defaultValue)" allows to pass
> a default value. It will return the default value if the property "name" does
> not exist in the map. This works fine for many types such as String, Long and
> Boolean - but apparently it does not work for Calendar properties.
> The problem is that when i pass a default value of type java.util.Calendar,
> the get method always returns the default value regardless wether there is a
> property "name" or not. Wether the property "name" is of type "Date" (in the
> JCR repository) or of type "String" does not seem to make a difference.
> The following example code can be used to reproduce the problem:
> ValueMap map = resource.adaptTo(ValueMap.class);
> // this does not work
> Calendar defaultCalendar = new GregorianCalendar();
> Calendar dateValue = map.get("dateProperty", defaultCalendar); // <- always
> returns defaultCalendar
> log.debug("dateProperty with default = "+(dateValue == null ? "null" :
> dateValue.get(Calendar.MONTH)+"."+dateValue.get(Calendar.YEAR)));
>
> // without default value - works fine
> dateValue = map.get("dateProperty", Calendar.class); // <- correctly
> returns the dateProperty value
> log.debug("dateProperty no default = "+(dateValue == null ? "null" :
> dateValue.get(Calendar.MONTH)+"."+dateValue.get(Calendar.YEAR)));
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.