[jdom-interest] How will JDOM be updated for Java Generics (JDK 1.5)?

Geoff Rimmer geoff.rimmer at sillyfish.com
Fri Jul 19 05:29:24 PDT 2002


Generics are due to be added to the Java language in JDK 1.5 to
provide better compile-time type safety.

For example if you had a list of Element objects, then instead of
writing code like this:

    Element rootElement = new SAXBuilder().build( ... ).getRootElement();

    List elementList = rootElement.getChildren( "person" );
    for ( Iterator iter1 = elementList.iterator(); iter1.hasNext(); )
    {
        Element e = (Element)iter1.next();

        List attributes = e.getAttributes();
        for ( Iterator iter2 = attributes.iterator(); iter2.hasNext(); )
        {
            Attribute attr = (Attribute)iter2.next();
            System.out.println( attr.getName() + " = " + attr.getValue() );
        }
    }

you could write:

    Element rootElement = new SAXBuilder().build( ... ).getRootElement();

    List<Element> elementList = rootElement.getChildren( "person" );
    for ( Iterator<Element> iter1 = elementList.iterator(); iter1.hasNext(); )
    {
        Element e = iter1.next();

        List<Attribute> attributes = e.getAttributes();
        for ( Iterator<Attribute> iter2 = attributes.iterator();
                  iter2.hasNext(); )
        {
            Attribute attr = iter2.next();
            System.out.println( attr.getName() + " = " + attr.getValue() );
        }
    }

Some JDOM changes would be trivial (for example a few changes from
List to List<Attribute>), but others would require a bit of work.

For example, Element.getChildren() currently returns a List.  As this
is always a List of Element objects, this should ideally be changed to
List<Element>.  But the way the method is implemented (in b8) would
not allow this:

    public List getChildren()
    {
        return content.getView(new ElementFilter());
    }

since content.getView() can, depending on what filter is passed to it,
return a List of other types (e.g. Text) as well.

Also, Element.getContent() returns a List whose entries can be of any
of the following types: Text, Element, Comment, ProcessingInstruction,
CDATA, and EntityRef.

There's not a lot of point having Element.getContent() returning a
List<Object>, so I would suggest that this would be the ideal excuse
to introduce an interface Node which all of the above types would
implement.

Then, Element.getContent() can look like this:

    public List<Node> getContent()
    {
        ....
    }

It is not necessary to wait for JDK 1.5 before making any JDOM
changes: they could be made now by using the Sun prototype Generics
compiler:

    http://developer.java.sun.com/developer/earlyAccess/adding_generics/

and what's more, the .class files generated by this compiler are
compatible with current JDK 1.3 and 1.4 JVMs.

-- 
Geoff Rimmer <> geoff.rimmer at sillyfish.com <> www.sillyfish.com
www.sillyfish.com/phone - Make savings on your BT and Telewest phone calls
UPDATED 07/06/2002: 521 destinations, 14 schemes (incl. 10p/min to mobiles)



More information about the jdom-interest mailing list