JDK-5098073 : (reflect) No method to get actual type parameters for generics
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-09-07
  • Updated: 2012-09-28
  • Resolved: 2004-09-07
Related Reports
Duplicate :  
Description

Name: rmT116609			Date: 09/07/2004


A DESCRIPTION OF THE REQUEST :
I have examine java.lang.reflect API and have not found a method to get actual  parameters for the instance of  generic class.  For example for class

class Base<T> {
}

class Test<T>  extends Base<String> {
}

There are no way to find a value to which T maps. This could be a method like
Test<String> t = Test<String>();
parameters = Test.class.getActualParameters(obj);
other  = Test.class.getActualParameters(obj);




JUSTIFICATION :
I have needed it for my experimental framework but other developers will need to face it.  The funcionality is required for any framework that makes heavy use of generics and Proxies.

 The task where it  was need was the following. I have a generic service interface object and a specific implementaiton class that has all methods of service, but these methods do not return anything and have extra interface.

So  interface class is like followig:

interface NumberGuesser<T extends Number> extends Serive {
   T guessNumber(T maxNumber, T minNumber);
}

The implementation class is the following:

class RandomNumberGuesser extends ServiceBase<NumberGuesser<Integer>> {
  Random random = new Random();
    void guessNumber(Resolver<Integer> resolver, Integer maxNumber, Integer minNumber)  {
       resolver.resolve(random.nextInt(maxNumber.intValue() - minNumber.intValue())+minNumber.intValue() );
   }
}

The mapping is done using java.lang.reflect.Proxy. I need to map every method  of  instantiated interface to the class. Because acutual  parameters are available, it is not possible establish mapping from interface method to implementation methods.  Parameters in iterface have type Number and implementation parameters have type Integer. So mapping does not look correct. If it were possible to determine acutal parameters of interface, specified as parameter to service base, it were possible to determine  that method signatures match exactly because of actual parameters of service interface.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The code like the following will work:
Test<String> t = Test<String>();
assert Class.getActuatualParametrs(obj)[0] == String.class;

ACTUAL -
No required methods are found in API.

---------- BEGIN SOURCE ----------
class Base<T,T2> {
}

class Test<T>  extends Base<Integer,T> {
}

Test<String> t = Test<String>();
assert Test.class.getActualParameters(obj)[0] == String.class;
assert Base.class.getActualParameters(obj)[0] == Integer.class;
assert Base.class.getActualParameters(obj)[1] == String.class;
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
The only way that  I have for my specific case is to ignore types of methods.  Methods are matched by name and number of parameters. But this behavior breaks user expectations.
(Incident Review ID: 301924) 
======================================================================

Comments
EVALUATION The submitter is requesting reifications of type parameters, a dup of 5098163. ###@###.### 2004-09-07
07-09-2004