JDK-5041645 : Request Parameterized class ".class" Syntax
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-05-03
  • Updated: 2008-05-21
  • Resolved: 2008-05-21
Related Reports
Duplicate :  
Description

Name: rmT116609			Date: 05/03/2004


A DESCRIPTION OF THE REQUEST :
If A is a Type parameter, nothing in JSR-14 spec (spec 8) forbids me from using the expresson
    A.class
But if I do so, it generates a compiler Error.

JUSTIFICATION :
Sometimes I need to return a Class object of a parameterized type. An example is a parameterized implementation of a TableModel. It uses a separate Column<ColType> instance for each column, but when writing the getColumnClass() method, the expression ColType.class generates this compiler error:

    cannot select from a type variable

The .class member is sufficiently universal that it should be allowed.

I don't see any other way to return the Class object of a parameterized type. (Any other way would satisfy me, but I won't always have an instance of the type, so I can't call its getClass() method.)

Note: The class java.lang.Object has been modified so that the getClass()
  method returns an object of type
    Class<? extends Object>
  I don't know if this helps me get a class object of a parameterized type, but
  I found no reference to the ? symbol in the "specification of  proposed
  language modifiations" in spec8.pdf. So I have no idea what it means.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The expression A.class should be legal, even when A is a parameterized type. It should resolve to the actual class invoked by the constructor.
ACTUAL -
The expression A.class generates a compiler error.

---------- BEGIN SOURCE ----------
/* Compile this source code to see the error message. */
public class ClassBug <CB>
{
	private ClassBug()
	{
	}
	
	public Class getBugClass()
	{
		return CB.class;
	}
}

---------- END SOURCE ----------
(Incident Review ID: 260837) 
======================================================================

Comments
EVALUATION This is illegal. And it cannot be supported without reification. ###@###.### 2004-07-09
09-07-2004