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 / October 2007

Tip: Looking for answers? Try searching our database.

Generics: how to read actual type parameters

Thread view: 
marek.dudek@gmail.com - 30 Sep 2007 20:50 GMT
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
Joshua Cranmer - 30 Sep 2007 21:07 GMT
> For example:
>
[quoted text clipped - 6 lines]
>     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)
Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

marek.dudek@gmail.com - 30 Sep 2007 21:10 GMT
Thank You a lot
Sorry for tabs
Lew - 30 Sep 2007 21:24 GMT
>> For example:
>>
[quoted text clipped - 25 lines]
>
> (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>

Signature

Lew

Daniel Pitts - 01 Oct 2007 04:48 GMT
> > marek.du...@gmail.com wrote:
> >> For example:
[quoted text clipped - 35 lines]
> --
> 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".


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.