Avatar of dleehanson
dleehansonFlag for United States of America

asked on 

Little Help with jdom and Generics

Eclipse has been complaining about the type safety of a line in my code that I can't seem to fix. According to the jdom javadoc(http://www.jdom.org/docs/apidocs/), it's method signature is: public java.util.List getChildren().  So since it is a collection of Elements I figured I had it covered...but no.

Can another set of eyes spot what I am missing, or is it just a bug of some kind?

Thanks in advance.

--Charly
List<Element> lines = xmlDoc.getRootElement().getChild("productionLines")
			.getChildren("productionLine");

Open in new window

Java

Avatar of undefined
Last Comment
dleehanson
Avatar of Mick Barry
Mick Barry
Flag of Australia image

try:

List<?> lines = xmlDoc.getRootElement().getChild("productionLines")
                        .getChildren("productionLine");

or just:

List lines
Avatar of dleehanson
dleehanson
Flag of United States of America image

ASKER

When I don't have anything at all, just List lines, it complains that it is a raw type and needs to be parameterized.  When I tried List<?> lines the warning went away, but my next line of code where I loop through the List of lines broke.  What is the ? in the parameter field?  Some kind of wildcard?
List lines = xmlDoc.getRootElement().getChild("productionLines")
			.getChildren();
		
for(Element someLine : lines)
{
}

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

it says its a list of objects of unknown type. So you'll need to cast in your loop

If a method doesn't define explicitly what it reurns then you cannot make assumptions (as the compiler just goes on how things are defined)

Avatar of dleehanson
dleehanson
Flag of United States of America image

ASKER

So if I leave it as just List, and cast it as an Element in my loop (which I can't get to work), won't that still give me the generics raw type warning I am trying to avoid? Am I missing something?

In the documentation it says that it returns a List of Element objects, but it must not be explicitly defined that way eh...

Thanks for the help and the quick replies!
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of dleehanson
dleehanson
Flag of United States of America image

ASKER

Ok.  Thanks!
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo