Generics: how to read actual type parameters

Discussion in 'Java' started by marek.dudek@gmail.com, Sep 30, 2007.

  1. Guest

    For example:

    public class Pair<S> {

    public Pair(S first, S second) {

    this.first = first;
    this.second = second;
    }

    public String toString() {
    Class clas = ??? ;
    return "Pair of " + clas.toString();
    }

    public S first;
    public S second;
    }

    so that after instantiating

    Pair<Double> p = new Pair<Double>( 0.0, 0.0 );

    p.toString() gives "Pair of Double"

    TIA
     
    , Sep 30, 2007
    #1
    1. Advertising

  2. wrote:
    > For example:
    >
    > public class Pair<S> {
    > public String toString() {
    > Class clas = ??? ;
    > return "Pair of " + clas.toString();
    > }
    > public S first;
    > public S second;
    > }
    >

    1. Don't use tabs in Usenet posts.
    2. What you are probably intending to do is impossible as specified.
    There is no possible way at runtime to get the class of S. Java erases
    the types of the parameters at runtime.
    3. Class is generic. Use Class<?> instead.

    The easiest thing you can do is:
    Class<?> clas = first.getClass();

    A potentially tighter bound is:
    Class<?> left = first.getClass();
    Class<?> right = second.getClass();
    Class<?> clas = left;

    while (!clas.isAssignableFrom(right))
    clas = clas.getSuperclass();

    (This returns the last common ancestor of the classes of first and second)
    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Sep 30, 2007
    #2
    1. Advertising

  3. Guest

    Thank You a lot
    Sorry for tabs
     
    , Sep 30, 2007
    #3
  4. Lew Guest

    Joshua Cranmer wrote:
    > wrote:
    >> For example:
    >>
    >> public class Pair<S> {
    >> public String toString() {
    >> Class clas = ??? ;
    >> return "Pair of " + clas.toString();
    >> }
    >> public S first;
    >> public S second;
    >> }
    >>

    > 1. Don't use tabs in Usenet posts.
    > 2. What you are probably intending to do is impossible as specified.
    > There is no possible way at runtime to get the class of S. Java erases
    > the types of the parameters at runtime.
    > 3. Class is generic. Use Class<?> instead.
    >
    > The easiest thing you can do is:
    > Class<?> clas = first.getClass();
    >
    > A potentially tighter bound is:
    > Class<?> left = first.getClass();
    > Class<?> right = second.getClass();
    > Class<?> clas = left;
    >
    > while (!clas.isAssignableFrom(right))
    > clas = clas.getSuperclass();
    >
    > (This returns the last common ancestor of the classes of first and second)


    Another hack in a class you own is to have a Class<?> instance variable to
    provide runtime type information.

    And here's a recent article I just googled up that delves into the issue (GIYF):
    <http://www.artima.com/weblogs/viewpost.jsp?thread=208860>

    --
    Lew
     
    Lew, Sep 30, 2007
    #4
  5. Daniel Pitts Guest

    On Sep 30, 1:24 pm, Lew <> wrote:
    > Joshua Cranmer wrote:
    > > wrote:
    > >> For example:

    >
    > >> public class Pair<S> {
    > >> public String toString() {
    > >> Class clas = ??? ;
    > >> return "Pair of " + clas.toString();
    > >> }
    > >> public S first;
    > >> public S second;
    > >> }

    >
    > > 1. Don't use tabs in Usenet posts.
    > > 2. What you are probably intending to do is impossible as specified.
    > > There is no possible way at runtime to get the class of S. Java erases
    > > the types of the parameters at runtime.
    > > 3. Class is generic. Use Class<?> instead.

    >
    > > The easiest thing you can do is:
    > > Class<?> clas = first.getClass();

    >
    > > A potentially tighter bound is:
    > > Class<?> left = first.getClass();
    > > Class<?> right = second.getClass();
    > > Class<?> clas = left;

    >
    > > while (!clas.isAssignableFrom(right))
    > > clas = clas.getSuperclass();

    >
    > > (This returns the last common ancestor of the classes of first and second)

    >
    > Another hack in a class you own is to have a Class<?> instance variable to
    > provide runtime type information.
    >
    > And here's a recent article I just googled up that delves into the issue (GIYF):
    > <http://www.artima.com/weblogs/viewpost.jsp?thread=208860>
    >
    > --
    > Lew


    Also, since Class is generified, you can do something like this:

    class Pair<E> {
    E first;
    E second;
    Class<E> type;

    public Pair(Class<E> type) {
    this.type = type;
    }

    public String toString() {
    return "A pair of " + type.getName() + " objects: <" + first +
    ", " + second ">";
    }
    }

    Although, I have to say its been my experience that toString is only
    very useful for debug messages, and not for any real textual output
    intended for the end user (in most cases). Especially a toString that
    gathers runtime information "automagically".
     
    Daniel Pitts, Oct 1, 2007
    #5
    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. Juergen Berchtel
    Replies:
    1
    Views:
    6,098
    John C. Bollinger
    May 20, 2005
  2. Amir  Michail
    Replies:
    2
    Views:
    315
    Terry Reedy
    Aug 10, 2004
  3. Todd
    Replies:
    1
    Views:
    457
    Stefan Ram
    Aug 15, 2008
  4. Soul
    Replies:
    0
    Views:
    551
  5. mreister
    Replies:
    1
    Views:
    3,386
    mreister
    May 25, 2010
Loading...

Share This Page