Re: Anyway to get the generic type from a generic class?
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().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?
I guess he wants something like, "I have an object obj which is of type
ArrayList. 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 <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> I use this code in a generic dao implementation
>
> public abstract class GenericDaoImpl implements GenericDao {
>
> private ClassparameterizedClass;
>
> @SuppressWarnings( "unchecked" )
> public GenericDaoImpl() {
> this.parameterizedClass = (Class) ( (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* <[EMAIL PROTECTED]
> > > 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().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: [EMAIL PROTECTED]
> 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?
Hi,
I use this code in a generic dao implementation
public abstract class GenericDaoImpl implements GenericDao {
private ClassparameterizedClass;
@SuppressWarnings( "unchecked" )
public GenericDaoImpl() {
this.parameterizedClass = (Class) ( (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* <[EMAIL PROTECTED]
> 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().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: [EMAIL PROTECTED]
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?
On 10/1/07, Jason Dillon <[EMAIL PROTECTED]> 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().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?
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().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?
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().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?
Jason,
As per my understanding, an ArrayList 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 sl = new ArrayList();
sl.add("Me");
((ArrayList)sl).add(new Integer(10));
System.out.println(sl);
}
Vamsi
On 10/1/07, Jason Dillon <[EMAIL PROTECTED]> 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().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
>
