Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

generics typecast

Thread view: 
Tomba - 09 Jan 2006 01:19 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.