Alexei Krainiouk created SLING-3351:
---------------------------------------

             Summary: org.apache.sling.jcr.resource.JcrResourceUtil.setProperty 
method doesn't handle primitive arrays
                 Key: SLING-3351
                 URL: https://issues.apache.org/jira/browse/SLING-3351
             Project: Sling
          Issue Type: Bug
          Components: JCR
            Reporter: Alexei Krainiouk


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 JcrResourceUtil.java lines 176:177):
{noformat}
        } else if ( propertyValue.getClass().isArray() ) {
            final Object[] values = (Object[])propertyValue;
{noformat}
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:
{noformat}
    public static void setProperty(final Node node,
            final String propertyName,
            final Object propertyValue)
            throws RepositoryException {
        if ( propertyValue == null ) {
            node.setProperty(propertyName, (String)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++) {
                Object value = Array.get(propertyValue, i);
                setValues[i] = createValue(value, node.getSession());
            }
            node.setProperty(propertyName, setValues);
        } else {
            node.setProperty(propertyName, createValue(propertyValue, 
node.getSession()));
        }
    }
{noformat}



--
This message was sent by Atlassian JIRA
(v6.1.5#6160)

Reply via email to