[ https://issues.apache.org/jira/browse/SLING-3351?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13886614#comment-13886614 ]
Alexei Krainiouk commented on SLING-3351: ----------------------------------------- Sorry for not submitting proper patch, - I was having troubles checking out your project yesterday. > 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 > Affects Versions: JCR Resource 2.2.8 > Reporter: Alexei Krainiouk > Assignee: Carsten Ziegeler > Fix For: JCR Resource 2.3.0 > > > 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 ) { > 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())); > } > } -- This message was sent by Atlassian JIRA (v6.1.5#6160)