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

Michael Salmon ms at formulae.org
Fri Jul 19 11:58:32 PDT 2002


I do not agree with adding Generics to Java. Let me list many of my reasons.

Java is stable, changes to the syntax and use of the language assumes
that something is wrong with the existing language. Don't fix what aint broke.

Generics dont accomplish anything that you couldnt do with Java already.
It is very easy to subclass the list type you want to allow only particular
types of values for. If JDOM wanted that, then it would be very easy to
add an Elements list which would give you the benefit of compile-time 
error checking. Making a list for homogenous types is also not
a problem, though you dont get the compile-time check. Basically
SafeVector (Class c) { this.cType = c; } 
public boolean add (Object o) { if (!(o.getClass() == cType))return false; ...}

Not ideal, but then at least you dont have to worry about many 
possible classcastexceptions. 

And again. This is not adding anything to java that can't already be done.
Is changing the language really going to make it any easier for everyone?

I strongly disagree to adding generics code to jdom until; its released 
in the jdk, has full backwards compatibility and most people seem to 
understand how they would use it properly and also how they can accomplish 
the same thing without it. I dont want to support Java changing things like
PERL does whenever someone comes up with some "bright idea".

On Fri, Jul 19, 2002 at 12:29:24PM +0000, Geoff Rimmer wrote:
> 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

ms-



More information about the jdom-interest mailing list