United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-5099091 Make the parameter type available from a generic instance
JDK-5099091 : Make the parameter type available from a generic instance

Details
Type:
Enhancement
Submit Date:
2004-09-08
Status:
Closed
Updated Date:
2004-09-09
Project Name:
JDK
Resolved Date:
2004-09-09
Component:
core-libs
OS:
windows_xp
Sub-Component:
java.lang
CPU:
x86
Priority:
P5
Resolution:
Duplicate
Affected Versions:
5.0
Fixed Versions:

Related Reports
Duplicate:

Sub Tasks

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
                                     
2004-09-08



Hardware and Software, Engineered to Work Together