[jdom-interest] Bug in ListIterator.add() method

Christian Gruber Christian.Gruber at biomax.com
Fri Apr 22 02:21:00 PDT 2005


I think I have found two bugs in the ListIterator implementation of
Jdom 1.0. To be precise, it is the add() method implementation. I have
checked whether they are still there in the nightly build, and I still
found them there, too.

I have attached an example where I compare the behaviour of the Jdom
ListIterator implementation with the standard Java 1.4 ListIterator

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class TestJdom {

    public static void main(String[] args) {
        try {
            // Do something with java.util.ListIterator
            List l = new LinkedList();
            ListIterator i;
            for (i = l.listIterator(); i.hasNext();) {
                String s = (String) i.next();
                i.add(s + "_x");
                i.add(s + "_y");

            // Do the same with the Jdom ListIterator
            Document d =
               new Document().setRootElement(new Element("root"));
            Element r = d.getRootElement();
            r.addContent(new Element("element").setText("1"));
            r.addContent(new Element("element").setText("2"));
            r.addContent(new Element("element").setText("3"));
            XMLOutputter o = new XMLOutputter();
            for (i = r.getChildren("element").listIterator();
                 i.hasNext();) {
                Element e = (Element) i.next();
                i.add(new Element("element").setText(e.getText() +
                //i.add(new Element("element").setText(e.getText() +
                //    "_y")); // bug1
            //i.add(new Element("element").setText("xxx")); // bug2
            o.output(d, System.out);
        } catch (Exception e) {

When the comment is removed from the line marked with "bug1", after a
few seconds a java.lang.OutOfMemoryError is thrown.

When the comment is removed from the line marked with "bug2" instead,
the following exception is thrown:
java.lang.IndexOutOfBoundsException: Index: 7 Size: 6
    at org.jdom.ContentList.add(ContentList.java:237)
    at org.jdom.ContentList.add(ContentList.java:140)
    at org.jdom.ContentList$FilterListIterator.add(ContentList.java:897)
    at TestJdom.main(TestJdom.java:38)

I suppose that this is not the desired behaviour, since doing the same
thing with the ListIterator of java.util.LinkedList behaves as

As I suppose, Jdoms ListIterator.add() implementation has a problem
when it is called more than once and when it is called at the end of
the list.

Christian Gruber

More information about the jdom-interest mailing list