generics typecast

Discussion in 'Java' started by Tomba, Jan 9, 2006.

  1. Tomba

    Tomba Guest

    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!
    Tomba, Jan 9, 2006
    #1
    1. Advertising

  2. Tomba

    Guest

    put this "@SuppressWarnings("unchecked")" on the top of you method.
    , Jan 9, 2006
    #2
    1. Advertising

  3. Tomba

    Guest

    put this @SuppressWarnings("unchecked") on the top of your method
    , Jan 9, 2006
    #3
  4. Tomba wrote:
    > 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.)

    --
    John Bollinger
    John C. Bollinger, Jan 9, 2006
    #4
  5. Tomba

    Tony Morris Guest

    "Tomba" <> wrote in message
    news:43c1ba20$0$32187$...
    > 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!


    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

    --
    Tony Morris
    http://tmorris.net/
    Tony Morris, Jan 9, 2006
    #5
  6. Tomba

    Roedy Green Guest

    On Mon, 09 Jan 2006 02:19:28 +0100, Tomba <>
    wrote, quoted or indirectly quoted someone who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 9, 2006
    #6
  7. Tomba

    Chris Smith Guest

    Tony Morris <> wrote:
    > 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?

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jan 9, 2006
    #7
  8. Tomba wrote:
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Jan 9, 2006
    #8
  9. Tomba

    Guest

    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.
    , Jan 13, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Fitzpatrick

    Re: typecast error!

    Mark Fitzpatrick, Feb 1, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,664
    Lau Lei Cheong
    Feb 8, 2006
  2. Joe Van Meer

    Re: typecast error!

    Joe Van Meer, Feb 1, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    410
    Bob Lehmann
    Feb 2, 2006
  3. Otis Mukinfus

    Re: typecast error!

    Otis Mukinfus, Feb 3, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    362
    Terry Burns
    Feb 4, 2006
  4. Juergen Berchtel
    Replies:
    1
    Views:
    5,975
    John C. Bollinger
    May 20, 2005
  5. Soul
    Replies:
    0
    Views:
    508
Loading...

Share This Page