Uploaded image for project: 'Commons JXPath'
  1. Commons JXPath
  2. JXPATH-54

[JXPATH] Pointer invalid after iterating over Pointers referencing atomic values


    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.2 Final
    • Fix Version/s: None
    • Labels:
    • Environment:

      Operating System: Windows XP
      Platform: All

    • Bugzilla Id:


      We encountered a problem with Pointers when iterating over an array or a
      collection that holds atomic values such as Boolean.


      • Create a bean exposing an array of Boolean objects
      • Obtain a Pointer iterator by calling:
      • Iterate over all Pointers and store them in a new Collection
      • Iterate over the new Collection
      • For each Pointer, try to update the Boolean value.
        --> The result is that only the last Boolean value of the original
        array is updated.
        Apparently the Pointers do not keep the index of the referenced
        element withing the array.
        The problem does also exist for Collections.

      We are using java5.

      We consider this to be an error since nowhere is stated that updating
      Pointer values is not possible after iterating over a list pointers.

      The only work around is to wrap the values (see Example)

      Kind regards

      Example Code:

      import java.util.ArrayList;
      import java.util.Iterator;

      import org.apache.commons.jxpath.JXPathContext;
      import org.apache.commons.jxpath.Pointer;

      public class JXPathTest {


      • @param args
        @SuppressWarnings( {"unchecked","unchecked"}

        public static void main(String[] args) {

      ArrayList arrayPointers = new ArrayList();
      ArrayList listPointers = new ArrayList();
      ArrayList listWrappedPointers = new ArrayList();

      AModel model = new AModel();
      System.out.println("start with:");
      // get pointers
      JXPathContext context = JXPathContext.newContext(model);
      // get the array pointers
      Iterator resultPointerIterator = context.iteratePointers
      while (resultPointerIterator.hasNext())

      { Pointer ptr = (Pointer)resultPointerIterator.next(); arrayPointers.add(ptr); }

      // get list pointers
      resultPointerIterator = context.iteratePointers("/booleans/listBool");
      while (resultPointerIterator.hasNext())

      { Pointer ptr = (Pointer)resultPointerIterator.next(); listPointers.add(ptr); }

      // get wrapped boolean pointers
      resultPointerIterator = context.iteratePointers
      while (resultPointerIterator.hasNext())

      { Pointer ptr = (Pointer)resultPointerIterator.next(); listWrappedPointers.add(ptr); }

      // modify pointers: We just invert every values
      // modify array content via iterated pointer (case Boolean[])
      Iterator it = arrayPointers.iterator();
      while (it.hasNext())

      { Pointer ptr = (Pointer)it.next(); ptr.setValue(Boolean.TRUE.equals(ptr.getValue())? Boolean.FALSE:Boolean.TRUE); }

      // modify collection content via iterated pointer (case Boolean List)
      it = listPointers.iterator();
      while (it.hasNext())

      { Pointer ptr = (Pointer)it.next(); ptr.setValue(Boolean.TRUE.equals(ptr.getValue())? Boolean.FALSE:Boolean.TRUE); }

      // modify wrapped boolean collection content via iterated pointer (case
      Wrapped Boolean List)
      it = listWrappedPointers.iterator();
      while (it.hasNext())

      { Pointer ptr = (Pointer)it.next(); ptr.setValue(Boolean.TRUE.equals(ptr.getValue())? Boolean.FALSE:Boolean.TRUE); }

      System.out.println("after invert --> not ok, only the boolean values of
      the wrapped boolean list are inverted");
      System.out.println(" In the Boolean Array and the
      ArrayList only the last value is inverted.");



      public static class AModel{
      SomeBooleans booleans = new SomeBooleans();

      public SomeBooleans getBooleans()

      { return booleans; }

      public void printIt()

      { booleans.printArrBool(); booleans.printListBool(); booleans.printListWrappedBool(); }


      public static class SomeBooleans{
      private Boolean[] arrBool = new Boolean[]

      { Boolean.TRUE, Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, }


      ArrayList listBool = new ArrayList();

      ArrayList listWrappedBool = new ArrayList();

      public SomeBooleans()

      { // populate list with boolean Object references listBool.add(Boolean.TRUE); listBool.add(Boolean.FALSE); listBool.add(Boolean.TRUE); listBool.add(Boolean.FALSE); // populate list with boolean wrapper objects listWrappedBool.add(new BooleanWrapper(Boolean.TRUE)); listWrappedBool.add(new BooleanWrapper(Boolean.FALSE)); listWrappedBool.add(new BooleanWrapper(Boolean.TRUE)); listWrappedBool.add(new BooleanWrapper(Boolean.FALSE)); }


      • @return Returns the arrBool.
        public Boolean[] getArrBool() { return arrBool; }


      • @param arrBool The arrBool to set.
        public void setArrBool(Boolean[] arrBool) { this.arrBool = arrBool; }


      • @return Returns the listBool.
        public ArrayList getListBool() { return listBool; }


      • @param listBool The listBool to set.
        public void setListBool(ArrayList listBool) { this.listBool = listBool; }


      • @return Returns the listWrappedBool.
        public ArrayList getListWrappedBool() { return listWrappedBool; }


      • @param listWrappedBool The listWrappedBool to set.
        public void setListWrappedBool(ArrayList listWrappedBool) { this.listWrappedBool = listWrappedBool; }

      public void printArrBool(){
      System.out.print("printArrBool: [");
      for (int i= 0; i< arrBool.length; ++i)

      { System.out.print(i>0?", "+arrBool[i]:arrBool[i]); }


      public void printListBool()

      { System.out.println("printListBool: " + listBool); }

      public void printListWrappedBool(){
      System.out.print("printListWrappedBool: [");
      for (int i= 0; i< listWrappedBool.size(); ++i)

      { System.out.print(i>0?", "+listWrappedBool.get(i):listWrappedBool.get (i)); }


      public static class BooleanWrapper{
      Boolean booleanValue;

      public BooleanWrapper(Boolean booleanValue)

      { this.booleanValue = booleanValue; }

      * @return Returns the booleanValue.
      public Boolean getBooleanValue() { return booleanValue; }

      * @param booleanValue The booleanValue to set.
      public void setBooleanValue(Boolean booleanValue) { this.booleanValue = booleanValue; }

      public String toString()

      { return booleanValue.toString(); }



        andreas.leitel@iclip.ch Andreas Leitel added a comment -

        The problem did not appear any more on nightly build from 2006/04/25

        andreas.leitel@iclip.ch Andreas Leitel added a comment - The problem did not appear any more on nightly build from 2006/04/25


          • Assignee:
            andreas.leitel@iclip.ch Andreas Leitel
          • Votes:
            0 Vote for this issue
            0 Start watching this issue


            • Created: