RE: Question on java generics

2006-06-28 Thread Meeraj Kunnumpurath
Raymond,

You can get the type of T if you have a parameterized field of a generic
type. Some thing like this,

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;

public class TestT {

private TestString myField;

/**
 * @param args
 */
public static void main(String[] args) throws Exception {

Field field = Test.class.getDeclaredField(myField);
ParameterizedType parameterizedType =
(ParameterizedType)field.getGenericType();
 
System.err.println(parameterizedType.getActualTypeArguments()[0]);
}

}

Ta
Meeraj 

-Original Message-
From: Yang ZHONG [mailto:[EMAIL PROTECTED] 
Sent: 28 June 2006 01:19
To: tuscany-dev@ws.apache.org
Subject: Re: Question on java generics

1) No. T is erasable.

2) If myClass is a class or generic type, you should be able to new
TestmyClass.
On the other hand, if myClass is a pointer/variable, no you can't
new TestmyClass


On 6/27/06, Raymond Feng [EMAIL PROTECTED] wrote:

 Generics gurus,

 Assuming I have the following class:

 public class TestT {
 ...
 }

 1) Is there a way to get the Class object for T in class Test? I know 
 T.class is illegal.
 2) Can I create the an instance of Test using a Class object myClass. 
 I know new TestmyClass is not valid.

 Thanks,
 Raymond





-- 

Yang ZHONG


This message has been checked for all email viruses by MessageLabs.



*

You can find us at www.voca.com

*
This communication is confidential and intended for 
the exclusive use of the addressee only. You should 
not disclose its contents to any other person.
If you are not the intended recipient please notify 
the sender named above immediately.

Registered in England, No 1023742,
Registered Office: Voca Limited
Drake House, Three Rivers Court,
Homestead Road, Rickmansworth,
Hertfordshire, WD3 1FX


This message has been checked for all email viruses by MessageLabs.

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Question on java generics

2006-06-28 Thread Jeremy Boynes

On 6/28/06, Meeraj Kunnumpurath [EMAIL PROTECTED] wrote:

Raymond,

You can get the type of T if you have a parameterized field of a generic
type. Some thing like this,



snip/

I think you can also get the parameterized type information from Class
itself. Jim was doing some introspection like this to help with
registration callbacks - have a look in BuilderRegistryImpl#register.

--
Jeremy

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Question on java generics

2006-06-28 Thread Yang ZHONG

Given
 CollectionString strings = ...;
 CollectionInteger integers = ...;

According to Java language spec, this is true:
 strings.getClass() == integers.getClass()

After all, Java doesn't generate .class files for EACH
parametization/instantiation like C++.

It seems impossible to get the parameter type from strings.getClass().

HOWEVER, what I have been discussing and what I guessed Raymond originally
wanted is INSTANCE type.
For DECLARATION type, it's different, as Meeraj ponited out, you can get
field DECLARATION type.

As for class itself, I guess parametized info may be available as super type
DECLARATION.
It'll be very interesting if template/generic INSTANTIATION info can be
found given only one .class file each type.


On 6/28/06, Jeremy Boynes [EMAIL PROTECTED] wrote:


On 6/28/06, Meeraj Kunnumpurath [EMAIL PROTECTED] wrote:
 Raymond,

 You can get the type of T if you have a parameterized field of a generic
 type. Some thing like this,


snip/

I think you can also get the parameterized type information from Class
itself. Jim was doing some introspection like this to help with
registration callbacks - have a look in BuilderRegistryImpl#register.

--
Jeremy





--

Yang ZHONG


RE: Question on java generics

2006-06-28 Thread Meeraj Kunnumpurath
I don't think you can get the instance type information from within the
class as it is erased by the compiler. 

-Original Message-
From: Yang ZHONG [mailto:[EMAIL PROTECTED] 
Sent: 28 June 2006 18:22
To: tuscany-dev@ws.apache.org
Subject: Re: Question on java generics

Given
  CollectionString strings = ...;
  CollectionInteger integers = ...;

According to Java language spec, this is true:
  strings.getClass() == integers.getClass()

After all, Java doesn't generate .class files for EACH
parametization/instantiation like C++.

It seems impossible to get the parameter type from strings.getClass().

HOWEVER, what I have been discussing and what I guessed Raymond
originally wanted is INSTANCE type.
For DECLARATION type, it's different, as Meeraj ponited out, you can get
field DECLARATION type.

As for class itself, I guess parametized info may be available as super
type DECLARATION.
It'll be very interesting if template/generic INSTANTIATION info can be
found given only one .class file each type.


On 6/28/06, Jeremy Boynes [EMAIL PROTECTED] wrote:

 On 6/28/06, Meeraj Kunnumpurath [EMAIL PROTECTED] wrote:
  Raymond,
 
  You can get the type of T if you have a parameterized field of a 
  generic type. Some thing like this,
 

 snip/

 I think you can also get the parameterized type information from Class

 itself. Jim was doing some introspection like this to help with 
 registration callbacks - have a look in BuilderRegistryImpl#register.

 --
 Jeremy




-- 

Yang ZHONG


This message has been checked for all email viruses by MessageLabs.



*

You can find us at www.voca.com

*
This communication is confidential and intended for 
the exclusive use of the addressee only. You should 
not disclose its contents to any other person.
If you are not the intended recipient please notify 
the sender named above immediately.

Registered in England, No 1023742,
Registered Office: Voca Limited
Drake House, Three Rivers Court,
Homestead Road, Rickmansworth,
Hertfordshire, WD3 1FX


This message has been checked for all email viruses by MessageLabs.

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Question on java generics

2006-06-27 Thread Raymond Feng
Generics gurus,

Assuming I have the following class:

public class TestT {
...
}

1) Is there a way to get the Class object for T in class Test? I know T.class 
is illegal.
2) Can I create the an instance of Test using a Class object myClass. I know 
new TestmyClass is not valid.

Thanks,
Raymond 



Re: Question on java generics

2006-06-27 Thread Yang ZHONG

1) No. T is erasable.

2) If myClass is a class or generic type, you should be able to new
TestmyClass.
   On the other hand, if myClass is a pointer/variable, no you can't new
TestmyClass


On 6/27/06, Raymond Feng [EMAIL PROTECTED] wrote:


Generics gurus,

Assuming I have the following class:

public class TestT {
...
}

1) Is there a way to get the Class object for T in class Test? I know
T.class is illegal.
2) Can I create the an instance of Test using a Class object myClass. I
know new TestmyClass is not valid.

Thanks,
Raymond






--

Yang ZHONG