JDK-5099091 : Make the parameter type available from a generic instance
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-09-08
  • Updated: 2004-09-09
  • Resolved: 2004-09-09
Related Reports
Duplicate :  
Description

Name: jl125535			Date: 09/08/2004


A DESCRIPTION OF THE REQUEST :
Type that is used as parameter for templates(generics) is not accessible at in runtime. For example the following programs do not compile.
--------------------------------------------
public class a<T> {

  public void tellMeParameterType() {
    System.out.println(T.class.getName());
  }

  public static void main(String args[]) {
     (new a<String>()).tellMeYourType();
     (new a<Integer>()).tellMeYourType();
  }
}

-----------------------------------------------------

public class b<T> {

  public void isMine(Object object) {
    System.out.println(object instanceof T);
  }

  public static void main(String args[]) {
     (new b<String>()).isMine("Test");
     (new b<Integer>()).isMine("Test");
  }
}

JUSTIFICATION :
This functionality is essential for frameworks that use reflection and generics. Sometime decision have to be made basing on type supplied as parameter.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
If type is being asked in instance assign it to private variable then use this variable in instance.  Also methods like newInstance() could require such variable to be supplied or use upper bound  it is not supplied. Another variant is to allow type to be referenced in constructor only so the following sample will work. In that case user will pay only for actually used features:

public class b1<T> {
  final Class myClass;
  public b1() {
    myClass = T.class;
  };

  public void isMine(Object object) {
    System.out.println(myClass.isAssignableFrom(object.getClass()));
  }

  public static void main(String args[]) {
     (new b1<String>()).isMine("Test");
     (new b1<Integer>()).isMine("Test");
  }
}
ACTUAL -
Samples just do not work.

---------- BEGIN SOURCE ----------
See samples above. The best thing is all of them start to work. But at least b1 should work in order for generics to be useful.
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
None. Except to pass class explicitly in constructor of the object. But it would completly defeat purpose of generics.
(Incident Review ID: 302318) 
======================================================================

Comments
EVALUATION This capability would require reification of type variables; dup of bug 5098163. ###@###.### 2004-09-08
08-09-2004