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. Advertisements

  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. Advertisements

  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. Advertisements

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. =?Utf-8?B?TWlrZQ==?=

    greater then / less then

    =?Utf-8?B?TWlrZQ==?=, Nov 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,863
    Kevin Spencer
    Nov 4, 2004
  2. David Lozzi
    Replies:
    15
    Views:
    7,238
    Steven Cheng[MSFT]
    Dec 20, 2005
  3. JosephByrns

    Slow, then quick then slow

    JosephByrns, Jul 10, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    2,745
    codezilla94
    Nov 13, 2007
  4. Schnoffos
    Replies:
    2
    Views:
    1,504
    Martien Verbruggen
    Jun 27, 2003
  5. Mufasa
    Replies:
    7
    Views:
    640
    Alexey Smirnov
    Sep 4, 2007
  6. rote
    Replies:
    0
    Views:
    533
  7. Replies:
    0
    Views:
    590
  8. Replies:
    4
    Views:
    729
    cwdjrxyz
    Jan 17, 2006
Loading...