Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
JCR Resource 2.2.8
-
None
Description
org.apache.sling.jcr.resource.JcrResourceUtil.setProperty(Node, String, Object) method throws ClassCastException if primitive array is passed as a property value.
The reason is that the method implementation attempts cast propertyValue parameter to Object[] directly after detecting that the property class is an array (see http://svn.apache.org/repos/asf/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/JcrResourceUtil.java lines 176:177):
....
} else if ( propertyValue.getClass().isArray() ) {
final Object[] values = (Object[])propertyValue;
.....
This doesn't take into account that primitive arrays are not subclasses of Object[] thus leading to ClassCastException.
Here is the fixed version of setProperty method:
public static void setProperty(final Node node,
final String propertyName,
final Object propertyValue)
throws RepositoryException {
if ( propertyValue == null )
else if ( propertyValue.getClass().isArray() ) {
final int length = Array.getLength(propertyValue);
final Value[] setValues = new Value[length];
for(int i=0; i<length; i++)
node.setProperty(propertyName, setValues);
} else
}