You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@geronimo.apache.org by Jason Dillon <ja...@planet57.com> on 2007/10/01 07:48:13 UTC

Anyway to get the generic type from a generic class?

Any of you generics experts out there know if there is any way to get  
the generic type from a generic class... like, say you have:

     Class type = new ArrayList<String>().getClass();

Is there any way to determine that this is an ArrayList containing  
String objects?  I can't seem to figure out how to get this  
information out of the Class instance.  I can figure out what the  
type variable name was, er like T, but that is well, completely  
useless IMO.

Does anyone know if its possible and how to do it?

--jason

Re: Anyway to get the generic type from a generic class?

Posted by Vamsavardhana Reddy <c1...@gmail.com>.
Jason,

As per my understanding, an ArrayList<String> will help generating
compilation errors should one try to add a non String object to the list.
It does not mean that you can not add a non String object to the underlying
list.  For e.g. the following code does not result in an error:

    public static void main(String[] args) {
        ArrayList<String> sl = new ArrayList<String>();
        sl.add("Me");
        ((ArrayList)sl).add(new Integer(10));
        System.out.println(sl);
    }

Vamsi

On 10/1/07, Jason Dillon <ja...@planet57.com> wrote:
>
> Any of you generics experts out there know if there is any way to get
> the generic type from a generic class... like, say you have:
>
>      Class type = new ArrayList<String>().getClass();
>
> Is there any way to determine that this is an ArrayList containing
> String objects?  I can't seem to figure out how to get this
> information out of the Class instance.  I can figure out what the
> type variable name was, er like T, but that is well, completely
> useless IMO.
>
> Does anyone know if its possible and how to do it?
>
> --jason
>

Re: Anyway to get the generic type from a generic class?

Posted by Vamsavardhana Reddy <c1...@gmail.com>.
I guess he wants something like, "I have an object obj which is of type
ArrayList<String>.  From this obj, I want to arrive at "String" which is the
type of the objects the array list holds.

Vamsi

On 10/1/07, Gareth Evans <ga...@msoft.co.uk> wrote:
>
> Hi,
>
> I use this code in a generic dao implementation
>
> public abstract class GenericDaoImpl<T> implements GenericDao<T> {
>
>     private Class<T>        parameterizedClass;
>
>     @SuppressWarnings( "unchecked" )
>     public GenericDaoImpl() {
>         this.parameterizedClass = (Class<T>) ( (ParameterizedType)
> getClass().getGenericSuperclass() ).getActualTypeArguments()[0];
>     }
>
> Not sure if this is the sort of thing your after?
>
> Gareth
>
> Vamsavardhana Reddy wrote:
> >
> >
> > On 10/1/07, *Jason Dillon* <jason@planet57.com
> > <ma...@planet57.com>> wrote:
> >
> >     Alright... thanks, that is what I figured :-(  So lame... I can
> >     figure out that a class as T and E bound, but I can't figure out the
> >     type of those buggers...
> >
> >
> > The underlying object does not impose any restrictions as such (and so
> > there is nothing special about these objects).  It is only for the
> > compiler that will help you prevent using some undesired type
> > unknowingly (for e.g, adding String to a Integer list).  I guess it is
> > evident from the  example in my last post.
> >
> >
> >     so stupid :-(
> >
> >     --jason
> >
> >
> >     On Sep 30, 2007, at 11:08 PM, David Jencks wrote:
> >
> >     >
> >     > On Sep 30, 2007, at 10:48 PM, Jason Dillon wrote:
> >     >
> >     >> Any of you generics experts out there know if there is any way to
> >     >> get the generic type from a generic class... like, say you have:
> >     >>
> >     >>     Class type = new ArrayList<String>().getClass();
> >     >>
> >     >> Is there any way to determine that this is an ArrayList
> containing
> >     >> String objects?  I can't seem to figure out how to get this
> >     >> information out of the Class instance.  I can figure out what the
> >     >> type variable name was, er like T, but that is well, completely
> >     >> useless IMO.
> >     >>
> >     >> Does anyone know if its possible and how to do it?
> >     >
> >     > I spent a couple days reading up on generics and trying stuff and
> >     > concluded it was not possible.  There are some statements in the
> >     > book on generics that confirm this.  Hopefully you will find a way
> >     > to prove I'm wrong 'cause it sure would be handy :-)
> >     >
> >     > sorry
> >     > david jencks
> >     >
> >     >>
> >     >> --jason
> >     >
> >
> >
>
>
> --
> Gareth Evans
>
> Senior Developer
>
> MSoft eSolutions Limited
> Technology Centre
> Inward Way
> Rossmore Business Park
> Ellesmere Port
> Cheshire
> CH65 3EN
>
> --
> Tel:    +44 (0)870 0100 704
> Fax:    +44 (0)870 9010 705
> E-Mail: gareth@msoft.co.uk
> Web:    www.msoft.co.uk
>
> ----------------------------------------------
> Terms:
> Please note that any prices quoted within this e-mail are subject to VAT.
> All program details and code described in this e-mail are subject to
> copyright (c) of MSoft eSolutions Limited and remain the intellectual
> property of MSoft eSolutions Limited.
> Any proposal or pricing information contained within this e-mail are
> subject to MSoft eSolutions' Terms and Conditions
> ----------------------------------------------
> Disclaimer:
> This message is intended only for use of the addressee. If this message
> was sent to you in error, please notify the sender and delete this
> message. MSoft eSolutions Limited cannot accept responsibility for
> viruses,
> so please scan attachments. Views expressed in this message do not
> necessarily reflect those of MSoft eSolutions Limited who will not
> necessarily be bound by its contents.
>
>
>

Re: Anyway to get the generic type from a generic class?

Posted by Gareth Evans <ga...@msoft.co.uk>.
Hi,

I use this code in a generic dao implementation

public abstract class GenericDaoImpl<T> implements GenericDao<T> {

    private Class<T>        parameterizedClass;

    @SuppressWarnings( "unchecked" )
    public GenericDaoImpl() {
        this.parameterizedClass = (Class<T>) ( (ParameterizedType) 
getClass().getGenericSuperclass() ).getActualTypeArguments()[0];
    }

Not sure if this is the sort of thing your after?

Gareth

Vamsavardhana Reddy wrote:
>
>
> On 10/1/07, *Jason Dillon* <jason@planet57.com 
> <ma...@planet57.com>> wrote:
>
>     Alright... thanks, that is what I figured :-(  So lame... I can
>     figure out that a class as T and E bound, but I can't figure out the
>     type of those buggers...
>
>
> The underlying object does not impose any restrictions as such (and so 
> there is nothing special about these objects).  It is only for the 
> compiler that will help you prevent using some undesired type 
> unknowingly (for e.g, adding String to a Integer list).  I guess it is 
> evident from the  example in my last post.
>
>
>     so stupid :-(
>
>     --jason
>
>
>     On Sep 30, 2007, at 11:08 PM, David Jencks wrote:
>
>     >
>     > On Sep 30, 2007, at 10:48 PM, Jason Dillon wrote:
>     >
>     >> Any of you generics experts out there know if there is any way to
>     >> get the generic type from a generic class... like, say you have:
>     >>
>     >>     Class type = new ArrayList<String>().getClass();
>     >>
>     >> Is there any way to determine that this is an ArrayList containing
>     >> String objects?  I can't seem to figure out how to get this
>     >> information out of the Class instance.  I can figure out what the
>     >> type variable name was, er like T, but that is well, completely
>     >> useless IMO.
>     >>
>     >> Does anyone know if its possible and how to do it?
>     >
>     > I spent a couple days reading up on generics and trying stuff and
>     > concluded it was not possible.  There are some statements in the
>     > book on generics that confirm this.  Hopefully you will find a way
>     > to prove I'm wrong 'cause it sure would be handy :-)
>     >
>     > sorry
>     > david jencks
>     >
>     >>
>     >> --jason
>     >
>
>


-- 
Gareth Evans

Senior Developer

MSoft eSolutions Limited
Technology Centre
Inward Way
Rossmore Business Park
Ellesmere Port
Cheshire
CH65 3EN

-- 
Tel:    +44 (0)870 0100 704
Fax:    +44 (0)870 9010 705
E-Mail: gareth@msoft.co.uk
Web:    www.msoft.co.uk

----------------------------------------------
Terms:
Please note that any prices quoted within this e-mail are subject to VAT.
All program details and code described in this e-mail are subject to
copyright © of MSoft eSolutions Limited and remain the intellectual
property of MSoft eSolutions Limited.
Any proposal or pricing information contained within this e-mail are
subject to MSoft eSolutions' Terms and Conditions
----------------------------------------------
Disclaimer:
This message is intended only for use of the addressee. If this message
was sent to you in error, please notify the sender and delete this
message. MSoft eSolutions Limited cannot accept responsibility for viruses,
so please scan attachments. Views expressed in this message do not
necessarily reflect those of MSoft eSolutions Limited who will not
necessarily be bound by its contents.



Re: Anyway to get the generic type from a generic class?

Posted by Vamsavardhana Reddy <c1...@gmail.com>.
On 10/1/07, Jason Dillon <ja...@planet57.com> wrote:
>
> Alright... thanks, that is what I figured :-(  So lame... I can
> figure out that a class as T and E bound, but I can't figure out the
> type of those buggers...


The underlying object does not impose any restrictions as such (and so there
is nothing special about these objects).  It is only for the compiler that
will help you prevent using some undesired type unknowingly (for e.g, adding
String to a Integer list).  I guess it is evident from the  example in my
last post.


so stupid :-(
>
> --jason
>
>
> On Sep 30, 2007, at 11:08 PM, David Jencks wrote:
>
> >
> > On Sep 30, 2007, at 10:48 PM, Jason Dillon wrote:
> >
> >> Any of you generics experts out there know if there is any way to
> >> get the generic type from a generic class... like, say you have:
> >>
> >>     Class type = new ArrayList<String>().getClass();
> >>
> >> Is there any way to determine that this is an ArrayList containing
> >> String objects?  I can't seem to figure out how to get this
> >> information out of the Class instance.  I can figure out what the
> >> type variable name was, er like T, but that is well, completely
> >> useless IMO.
> >>
> >> Does anyone know if its possible and how to do it?
> >
> > I spent a couple days reading up on generics and trying stuff and
> > concluded it was not possible.  There are some statements in the
> > book on generics that confirm this.  Hopefully you will find a way
> > to prove I'm wrong 'cause it sure would be handy :-)
> >
> > sorry
> > david jencks
> >
> >>
> >> --jason
> >
>
>

Re: Anyway to get the generic type from a generic class?

Posted by "Kresten Krab Thorup (Trifork)" <kr...@trifork.com>.
Yes, this is quite unfortunate, and I can tell you that this is one  
of the issues where there was much discussion in the EG and in the  
groups of people trying to influence JSR 14.  The bottom line is that  
the chosen implementation of generics works without any changes to  
the JVM, and that code compiled with generics could theoretically be  
run with and older JVM.  In practice this kind of backwards  
compatibility doesn't work anyway because of the bytecode version  
burned into class files by a JDK 1.5 compiler.  But as allways these  
decisions are piled up agains a lot of different technical as well as  
political issues...

Kresten


On Oct 1, 2007, at 8:11 AM, Jason Dillon wrote:

> Alright... thanks, that is what I figured :-(  So lame... I can  
> figure out that a class as T and E bound, but I can't figure out  
> the type of those buggers... so stupid :-(
>
> --jason
>
>
> On Sep 30, 2007, at 11:08 PM, David Jencks wrote:
>
>>
>> On Sep 30, 2007, at 10:48 PM, Jason Dillon wrote:
>>
>>> Any of you generics experts out there know if there is any way to  
>>> get the generic type from a generic class... like, say you have:
>>>
>>>     Class type = new ArrayList<String>().getClass();
>>>
>>> Is there any way to determine that this is an ArrayList  
>>> containing String objects?  I can't seem to figure out how to get  
>>> this information out of the Class instance.  I can figure out  
>>> what the type variable name was, er like T, but that is well,  
>>> completely useless IMO.
>>>
>>> Does anyone know if its possible and how to do it?
>>
>> I spent a couple days reading up on generics and trying stuff and  
>> concluded it was not possible.  There are some statements in the  
>> book on generics that confirm this.  Hopefully you will find a way  
>> to prove I'm wrong 'cause it sure would be handy :-)
>>
>> sorry
>> david jencks
>>
>>>
>>> --jason
>>
>

Kresten Krab Thorup, Ph.D.
CTO, Trifork

                 WHO'S AT JAOO? - http://jaoo.dk
-----------------------------------------------------------------------
Trifork A/S, Margrethepladsen 3, 8000  Århus C, Denmark
Tel: +45 8732 8787 / Fax: +45 8732 8788 / Mob: +45 2343 4626




Re: Anyway to get the generic type from a generic class?

Posted by Jason Dillon <ja...@planet57.com>.
Alright... thanks, that is what I figured :-(  So lame... I can  
figure out that a class as T and E bound, but I can't figure out the  
type of those buggers... so stupid :-(

--jason


On Sep 30, 2007, at 11:08 PM, David Jencks wrote:

>
> On Sep 30, 2007, at 10:48 PM, Jason Dillon wrote:
>
>> Any of you generics experts out there know if there is any way to  
>> get the generic type from a generic class... like, say you have:
>>
>>     Class type = new ArrayList<String>().getClass();
>>
>> Is there any way to determine that this is an ArrayList containing  
>> String objects?  I can't seem to figure out how to get this  
>> information out of the Class instance.  I can figure out what the  
>> type variable name was, er like T, but that is well, completely  
>> useless IMO.
>>
>> Does anyone know if its possible and how to do it?
>
> I spent a couple days reading up on generics and trying stuff and  
> concluded it was not possible.  There are some statements in the  
> book on generics that confirm this.  Hopefully you will find a way  
> to prove I'm wrong 'cause it sure would be handy :-)
>
> sorry
> david jencks
>
>>
>> --jason
>


Re: Anyway to get the generic type from a generic class?

Posted by David Jencks <da...@yahoo.com>.
On Sep 30, 2007, at 10:48 PM, Jason Dillon wrote:

> Any of you generics experts out there know if there is any way to  
> get the generic type from a generic class... like, say you have:
>
>     Class type = new ArrayList<String>().getClass();
>
> Is there any way to determine that this is an ArrayList containing  
> String objects?  I can't seem to figure out how to get this  
> information out of the Class instance.  I can figure out what the  
> type variable name was, er like T, but that is well, completely  
> useless IMO.
>
> Does anyone know if its possible and how to do it?

I spent a couple days reading up on generics and trying stuff and  
concluded it was not possible.  There are some statements in the book  
on generics that confirm this.  Hopefully you will find a way to  
prove I'm wrong 'cause it sure would be handy :-)

sorry
david jencks

>
> --jason