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 / December 2005

Tip: Looking for answers? Try searching our database.

[Java Generics] T.class from <T>

Thread view: 
_v01 - 27 Dec 2005 18:23 GMT
Hi,

I have:
/////////////////////////
class Model<T> {
    public List<T> getAll() {
        // NameOfClass == T.class <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        return (List<T>) DB.getAll(NameOfClass.class);
    }
}

class DB {
   
    //
    // ...
    // smt methods   
    // ...
    //

    public static List getAll(Class c) {
        return Hibernate.findAll(c);
    }
}
///////////////////

NameOfClass == T.class
How can I get T.class from <T>??
Chris Smith - 27 Dec 2005 18:38 GMT
> class Model<T> {
>     public List<T> getAll() {
>         // NameOfClass == T.class <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>         return (List<T>) DB.getAll(NameOfClass.class);
>     }
> }

> NameOfClass == T.class
> How can I get T.class from <T>??

You can't.  It's prevented by type erasure.  Here's what you do.  You
modify the signature for the contructor to require a single parameter,
which is of type "Class<T>".  There will be only one possible value for
that parameter, that being the Class object for the type T... so there's
no chance of someone passing in the wrong values, and you have the
necessary value which you can store in a field for later use.

Yes, it's kludgy.  Yes, type erasure was a dumb idea.  Nevertheless,
that's what you do.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

_v01 - 27 Dec 2005 23:06 GMT
Chris Smith napisał(a):

>>class Model<T> {
>>    public List<T> getAll() {
[quoted text clipped - 15 lines]
> Yes, it's kludgy.  Yes, type erasure was a dumb idea.  Nevertheless,
> that's what you do.

Thank you for req.

There is solution:
http://www.hibernate.org/328.html


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.