views:

906

answers:

2

I have the following code

String innerText = null;
innerText = this.getException(detail.getChildElements());

causing this warning

Type safety: The expression of type Iterator needs unchecked conversion to conform to Iterator

The referenced method is

private String getException(Iterator<OMElementImpl> iterator) { ... }

The other method, getChildElements(), is in a JAR file that I can't touch. There are no other warnings or errors.

From Googling, it seems like the usual way to get rid of this sort of warning is

@SuppressWarnings("unchecked")
String innerText = this.getException(detail.getChildElements());

because the compiler can't guarantee safety ahead of time, but I'd prefer to avoid using SuppressWarnings if possible... is there a better way?

EDIT: getChildElements() is documented here

A: 

As an alternative you can explicitly cast the argument to the desired type, but SupressWarnings in that case is OK to my view.

String innerText = null;
innerText = this.getException((Iterator<OMElementImpl>)(detail.getChildElements()));

EDIT: As the comments to my answer point out the above do not solve the issue. I wrote as my first intuition, but that was wrong.

quosoo
Explicit cast won't remove the warning.
Tadeusz Kopec
I can confirm tkopec's comment; this was the first thing I tried.
Lord Torgamus
+8  A: 
erickson
Verifying contents of a collection returned by a third party library like this is good advice.
matt b
As an alternative to creating a new collection see `Iterables.transform` in Google Collections http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Iterables.html#transform(java.lang.Iterable,%20com.google.common.base.Function)
Hemal Pandya
Well, I'm a new junior developer and have been assured that it's okay to make the assumption in this particular case; I (and my tech lead, for that matter) just hate seeing the little warning icon. +1/accepted for giving good general advice, though.
Lord Torgamus