-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
5.0, 6, 7
-
Fix Understood
-
x86
-
linux, windows_xp, windows_7
The jls3 states that for a given t of type T,
t.getClass() is typed Class<? extends erasure(T)>.
I think the getClass() typing rule could be changed to
Class<? extends wildcard(T)>
The wildcard operation is defined by:
if T is parametrized, wildcard(T)=erasure(T)<?>
else , wildcard(T)=T
JUSTIFICATION :
1) This rule introduce a raw type.
Raw type must ONLY be used to interact with legacy code.
2) The new Rule introduce a wildcard.
Relationship between parametrized type and wildcard are based
on subtyping rules.
Relationship between parametrized type and wildcard are based
on raw type conversion.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
the program must not compile
ACTUAL -
the program compile
---------- BEGIN SOURCE ----------
public void f(ArrayList<Integer> l1, ArrayList<String> l2)
throws InstantiationException, IllegalAccessException {
l1.getClass().newInstance().addAll(l2.getClass().newInstance());
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
public Class<? extends ArrayList<?>> wildcardGetClass(ArrayList<?> list) {
return (Class<? extends ArrayList<?>>)list.getClass();
}
public void f(ArrayList<Integer> l1, ArrayList<String> l2)
throws InstantiationException, IllegalAccessException {
wildcardGetClass(l1).newInstance().addAll(
wildcardGetClass(l2).newInstance());
}
###@###.### 10/26/04 22:47 GMT
- duplicates
-
JDK-6452570 List.class must be of type Class<List<?>> and (not) Class<List>
-
- Closed
-
-
JDK-7101374 Incorrect "Type mismatch" reported for generic cast of class literals.
-
- Closed
-
- relates to
-
JDK-4982096 Object.getClass() leads to type system loophole
-
- Resolved
-
-
JDK-5004321 Object.getClass() should return erased class type
-
- Resolved
-
-
JDK-6184887 getClass() doen't take into account that some classes are final
-
- Closed
-
-
JDK-8054182 15.12.2.6: Clarify the return type of 'getClass'
-
- Closed
-
-
JDK-6209029 The type of a class literal shouldn't be erased
-
- Open
-