hi,
simple and straightforward:
how can I write a type cast so that the following line does not produce
any warnings about unsafe typecast etc...
Set<String> shapes = (Set<String>)this.settings.getAppSetting("shapeTypes");
Thanks a lot!
deng.yin@gmail.com - 09 Jan 2006 01:44 GMT
put this "@SuppressWarnings("unchecked")" on the top of you method.
deng.yin@gmail.com - 09 Jan 2006 01:45 GMT
put this @SuppressWarnings("unchecked") on the top of your method
John C. Bollinger - 09 Jan 2006 02:23 GMT
> simple and straightforward:
> how can I write a type cast so that the following line does not produce
> any warnings about unsafe typecast etc...
>
> Set<String> shapes =
> (Set<String>)this.settings.getAppSetting("shapeTypes");
There is no way to do what you ask. That is, if a cast is required in
the first place to make the code compile in a 1.5+ compiler, then it is
necessarily a "possibly unsafe" cast. You cannot rewrite the cast to
make it safe because the problem is not with the cast itself, but rather
with the expression being typecast and the variable to which you are
assigning it.
You do not provide the signature of the getAppSetting() method, but it
is probably this:
Set getAppSetting(String s);
What you appear to want instead is this:
Set<String> getAppSetting(String s);
If the method signature were the latter then no cast would be required
at all, and certainly not an unsafe one. (Some compilers would in fact
emit an "unnecessary cast" warning if you used a cast with it anyway.)

Signature
John Bollinger
jobollin@indiana.edu
Tony Morris - 09 Jan 2006 04:10 GMT
> hi,
>
[quoted text clipped - 5 lines]
>
> Thanks a lot!
It's not so simple and straightfoward at all, since you have oversimplified
details.
One can only speculate at what your getAppSetting() method returns - a Set
or Set<?> is my guess.
You cannot perform the cast without a compile-time warning for reasons which
are covered explicitly in the generics tutorial.
You might decide to change your reference to type Set<?> or Set<? super
String> or even change the return type of the method.
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

Signature
Tony Morris
http://tmorris.net/
Chris Smith - 09 Jan 2006 07:04 GMT
> You cannot perform the cast without a compile-time warning for reasons which
> are covered explicitly in the generics tutorial.
> You might decide to change your reference to type Set<?> or Set<? super
> String> or even change the return type of the method.
Of course, you probably can't perform a safe conversion to Set<? super
String> either. Set<?> works, but prevents an add operation.
If only it were so simple as changing the return type. How do you
change the return type for javax.servlet.ServletRequest.getAttribute,
for example?

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Roedy Green - 09 Jan 2006 04:12 GMT
>Set<String> shapes = (Set<String>)this.settings.getAppSetting("shapeTypes");
You need to change the signature for getAppSetting to return a
Set<String> or Set<T>. Then the compiler can check that you have not
cheated.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Thomas Hawtin - 09 Jan 2006 14:47 GMT
> how can I write a type cast so that the following line does not produce
> any warnings about unsafe typecast etc...
>
> Set<String> shapes =
> (Set<String>)this.settings.getAppSetting("shapeTypes");
There is not enough runtime information to check the cast. So you should
find a different way.
You can introduce specialist class to represent the set of strings:
class ShapeSettings {
private final Set<String> shapes;
public ShapeSettings(Set<String> shapes) {
this.shapes = Collections.unmodifiableSet(
new HashSet<String>(shapes)
);
}
public Set<Shape> getShapes() {
return shapes;
}
}
...
ShapeSettings shapeSettings = (ShapeSettings)
settings.getAppSettings(SettingKeys.SHAPE_TYPES);
Set<String> shapes = shapeSettings.getShapes();
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
ricky.clarkson@gmail.com - 13 Jan 2006 02:02 GMT
Set set=new HashSet();
set.add("Hello");
set.add("Goodbye");
Set<String> strings=new HashSet<String>();
To get the contents of set into 'strings':
for (final Object object: set)
strings.add((String)object);
Not brilliant, but in some contexts, I can't find a better way, e.g.,
deserialisation.