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

Rolf Lear rlear at algorithmics.com
Fri Apr 22 12:07:42 PDT 2005

For the record ... here is the output of the Bug testcase with both
bug-lines uncommented:

[one, one_x, one_y, two, two_x, two_y, three, three_x, three_y, xxx]
<?xml version="1.0" encoding="UTF-8"?>


-----Original Message-----
From: jdom-interest-bounces at jdom.org
[mailto:jdom-interest-bounces at jdom.org]On Behalf Of Christian Gruber
Sent: Friday, April 22, 2005 5:21 AM
To: jdom-interest at jdom.org
Subject: [jdom-interest] Bug in ListIterator.add() method


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
To control your jdom-interest membership:

This email and any files transmitted with it are confidential and
proprietary to Algorithmics Incorporated and its affiliates
("Algorithmics").  If received in error, use is prohibited.  Please destroy,
and notify sender.  Sender does not waive confidentiality or privilege.
Internet communications cannot be guaranteed to be timely, secure, error or
virus-free.  Algorithmics does not accept liability for any errors or
omissions.  Any commitment intended to bind Algorithmics must be reduced to
writing and signed by an authorized signatory.

More information about the jdom-interest mailing list