Using a generic to cast, and then ? [T s = (T)new Stuff()]

Discussion in 'Java' started by Sébastien de Mapias, Jun 19, 2008.

  1. Hi,
    What are the limitations of using a cast with a generic ?
    How *beautiful* would it be if I could write a generic 'equals()'
    method -for exple-, with the following:

    public class StuffImpl extends GenericTypes<StuffImpl>
    {
    // almost empty class...
    }

    public abstract class GenericTypes<T>
    {
    ...
    private String value;
    ...
    public boolean equals(Object obj) {
    if (this == obj)
    return true;
    if (obj == null)
    return false;
    if (getClass() != obj.getClass())
    return false;
    // final StuffImpl other = (StuffImpl) obj;
    final T other = (T) obj;
    if (value == null) {
    if (other.value != null)
    return false;
    } else if (!value.equals(other.value))
    return false;
    return true;
    }

    This equals is that generated by RAD. Once I do
    final T other = (T) obj;
    the jdk complains on the line
    if (other.value != null)
    saying it "cannot find symbol"...

    Too bad isn't it ?

    Has someone perhaps found a way to implement an "equals()"
    method similar to that above, using a casted generic ?

    What operations are possible on 'other' (I mean using Class.forName()
    etc.) to try to retrieve what 'other' really is, what instanceOf it
    is ??

    The possibilities seem to be much lesser than with C++ don't they ?

    In advance, many thanks.
    Regards,
    Sébastien
    Sébastien de Mapias, Jun 19, 2008
    #1
    1. Advertising

  2. > What operations are possible on 'other' (I mean using Class.forName()
    > etc.) to try to retrieve what 'other' really is, what instanceOf it
    > is ??


    Using reflection maybe ?
    Sébastien de Mapias, Jun 19, 2008
    #2
    1. Advertising

  3. Sébastien de Mapias

    Lew Guest

    Sébastien de Mapias wrote:
    > This equals is that generated by RAD. Once I do
    >   final T other = (T) obj;
    > the jdk complains on the line
    >   if (other.value != null)
    > saying it "cannot find symbol"...
    >
    > Too bad isn't it ?


    Not at all. The problem would be if it accepted that. How should the
    compiler determine that type T has a member named 'value'? You have
    given it no indication that it does. You have declared that the type
    *GenericTypes* has such a member, but not T.

    Also, you really cannot cast on a generic parameter type. You don't
    show an @SuppressWarnings() annotation, so presumably the cast raised
    you a compiler warning.

    --
    Lew
    Lew, Jun 19, 2008
    #3
    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. David Lozzi
    Replies:
    15
    Views:
    6,843
    Steven Cheng[MSFT]
    Dec 20, 2005
  2. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  3. Mufasa
    Replies:
    7
    Views:
    411
    Alexey Smirnov
    Sep 4, 2007
  4. Replies:
    0
    Views:
    270
  5. Replies:
    4
    Views:
    537
    cwdjrxyz
    Jan 17, 2006
Loading...

Share This Page