Uploaded image for project: 'Sling'
  1. Sling
  2. SLING-3351

org.apache.sling.jcr.resource.JcrResourceUtil.setProperty method doesn't handle primitive arrays

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • JCR Resource 2.2.8
    • JCR Resource 2.3.0
    • JCR
    • 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 )

      { 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())); }

      }

      Attachments

        Activity

          People

            cziegeler Carsten Ziegeler
            akrainiouk Alexei Krainiouk
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: