Reputation: 68140
In such a function:
<T> void foo(T obj)
The type of obj.getClass()
is Class<?>
and not Class<? extends T>
. Why?
The following code works fine:
String foo = "";
Class<? extends String> fooClass = foo.getClass();
So the signature of T#getClass()
seems to return a Class<? extends T>
, right?
Why is the signature different if T
really is a generic?
To overcome the problem (and to make it more clear what I wander about), I have implemented this function:
@SuppressWarnings("unchecked") static <T> Class<? extends T> classOf(T obj) {
return (Class<? extends T>) obj.getClass();
}
Again the question: Why is the cast needed here and not in the String
case? And why is the
SuppressWarnings
needed? Isn't it always clear from the code that it will always be able to safely do this cast?
Is there any way I can get a Class<? extends T>
from obj
? If yes, how? If not, why not?
One way would be to use classOf
. That would be safe, right? If that is always safe and gives a safe way to really get a Class<? extends T>
(instead of a Class<?>
), why is there no such function in Java? Or is there?
How about that case:
<T> void bar(T[] array)
array.getClass().getComponentType()
again returns a Class<?>
and not a Class<? extends T>
. Why?
I have implemented this function:
@SuppressWarnings("unchecked") static <T> Class<? extends T> classOf(T[] array) {
return (Class<? extends T>) array.getClass().getComponentType();
}
Is this again safe to use?
To clarify more what I wonder about. Consider this demo code:
static interface I<T> {
Class<? extends T> myClass();
}
static class A implements I<A> {
public Class<? extends A> myClass() {
return this.getClass();
}
}
static <T> void foo(I<T> obj) {
Class<? extends T> clazz = obj.myClass(); // this works
}
This works fine. But the same does not for Object#getClass()
.
Why wasn't it possible for example to have a generic interface like ClassInstance<T>
with the function getClass()
and every Java Object automatically implementing this? This would have exactly those improvements I am talking about over the solution to have it extending from a non-generic base class Object
.
Or having Object
as a generic class:
static abstract class Object<T> {
abstract Class<? extends T> myClass();
}
static class B extends Object<B> {
public Class<? extends B> myClass() {
return this.getClass();
}
}
static <T> void bar(Object<T> obj) {
Class<? extends T> clazz = obj.myClass(); // this works
}
Now think of myClass()
as getClass()
and think about that the compiler would automatically add that to every class. It would have resolved a lot of those casting issues.
The main question I am talking about is: Why wasn't it made like this?
Or to put it again in different words: Here, I describe in more detail the solution of such classOf
function which overcomes the problem. Why wasn't it made like this, i.e. why is the original function not like this?
(I don't really want to get an answer like: the way Java works right now, i.e. extending from a non-generic Object
which defines this function, makes this not possible. I am asking why it wasn't solved somehow differently so that it would have been possible.)
Upvotes: 10
Views: 2632
Reputation: 227
I agree with Peter Lawney and CurtainDog here: for once, type erasure is not the culprit; indeed, as Peter Lawney has said, what is at stake is the lack of a Class<this>
construct.
But such things are performed in other classes and interfaces, such as Comparable
.
The problem is that the method getClass
is defined at the level of Object
, and returns Class<?>
, which is the best you can do at that level. To do better, we would need to use generic, which would make every class in Java a generic class.
You would introduce an interface such as :
public interface ClassAwareObject<T> {
Class<T> getMyClass();
}
A class which implements this method can now be used in scenarii where you need casts in standard Java :
public class Compteur implements ClassAwareObject<Compteur> {
...
public interface ClassAwareObject<T> {
Class<? extends T> getMyClass();
}
}
And now you can write something like
Class<Compteur> c = compteur.getMyClass();
Obviously, the problem is that you need to redefine the method for each class for which you need this behaviour if you want the correct class to be sent.
So the JDK could not make Object.getClass()
follow this pattern, as it would have broken every class written before.
But it's not a problem of type erasure : it's a problem which can be dealt with with static typing.
Upvotes: 0
Reputation: 3669
In Java generics are just a source level tool for safer development.
The JVM does not know anything about generics. The Java compiler throws away this information, and all generic types are indeed just Object references at runtime. To compensate for that the compiler inserts the necessary casts. This procedure is called Type Erasure (Google!).
List<String> x = new ArrayList<String>();
x.add("hello");
String hello = x.get(0);
becomes the following at runtime
List x = new ArrayList();
x.add("hello");
String hello = (String) x.get(0);
To solve your problem you could try to investigate the individual elements in your array (arr[0].getClass()
).
Upvotes: 4
Reputation: 3205
There are a couple of not totally accurate answers here. Generics are indeed implemented using type erasure, however this does not mean that all type information is lost. The compiler will erase the type to the lowest bound it can.
So <T extends String> gets erased to String; this is why getClass on a String returns Class<? extends String>. Your unbounded <T> however gets erased to Object; and so getClass returns Class<? extends Object>, i.e. Class<?>
Generics are complex, and they don't always do what you want, but there are ways to work around many things (by improving your bounds, accessing runtime type information via reflection, and passing class objects around). Type erasure is actually a pretty clever solution, undeserving of much of the bad press it has received.
Upvotes: 3
Reputation: 533500
The basic problem is that getClass() doesn't return the class because its defined at the Object level. i.e. it is mearly defined as a class which extends object. They could have defined getClass() like.
Class<this> getClass() { /**/ }
but instead its
Class<?> getClass()
which means generics has no understanding of what getClass returns.
Upvotes: 6
Reputation: 106351
Because of type erasure, the runtime doesn't keep type information for generic arrays. In effect, the JVM internally treats all generic arrays as if they are an Object[].
If you want to get a runtime type, your best option may simply be to call getClass() on the first item in the array. You'll obviously need to find a way to handle the empty case, and the case where the contained objects are of multiple types, etc.
Upvotes: 0