15

I would expect that from the aspect of compile time as well as from the aspect of runtime it wouldn't be a problem for .getClass() to provide a correctly-typed return value.

But I must be wrong.

public class _GetClassGenerics2 {

  static class MyClass {
  }

  public static void main(String[] args) {
    MyClass myInstance = new MyClass();
    // here it works
    Class<? extends MyClass> type = myInstance.getClass();

    myMethod(myInstance);
  }

  public static <T extends MyClass> void myMethod(T instance) {
    Class<? extends T> type = instance.getClass();
// java.lang.RuntimeException: Uncompilable source code - incompatible types
//  required: java.lang.Class<? extends T>
//  found:    java.lang.Class<capture#1 of ? extends _GetClassGenerics2.MyClass>
  }

}

EDIT: It doesn't work with Class<T> and Class<? super T> either.

3

4 Answers 4

6

java.lang.Class does not represent a type (use java.lang.reflect.Type for that). If T, were say ArrayList<String> then it makes no sense for there to be a Class<ArrayList<String>>.

It's worth noting that in this particular case there is no need for the method to be generic.

public static <T extends MyClass> void myMethod(T instance) {

Is equivalent to:

public static void myMethod(MyClass instance) {
4

As per the Javadoc of the getClass method:

The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called. For example, no cast is required in this code fragment

Here, the value for |X| in your code snippet is MyClass, hence instance.getClass() is assignable to only Class<? extends MyClass> or Class<?>.

The reason for this specific wording is because when you say that for this variable having type T where <T extends MyClass>, there can be multiple classes which extend MyClass and hence capable of satisfying the T extends MyClass criteria. Without runtime information there is no way of knowing which concrete implementation subclass of MyClass was passed in the method. Hence to provide a generic solution, it returns <? extends MyClass> since that would hold true for any subclass of MyClass irrespective of what class instance is passed in.

8
  • 3
    What I don't get is why X is MyClass, not T. Dec 16, 2010 at 15:35
  • 1
    @Sergey Tachenov: Because AFAICT, |X| is an erasure and hence can't be a parametrized type. If it were simply T, the Javadoc wouldn't have explicitly mentioned erasure of static type. Dec 16, 2010 at 16:05
  • 3
    Oh, finally I got it. Language specification 4.6 explicitly says that "The erasure of a type variable (§4.4) is the erasure of its leftmost bound". Which is MyClass in our case. I don't see why they made it this way, though. Dec 16, 2010 at 16:34
  • @Sergey: That's because when you say that for this variable having type T where <T extends MyClass>, there can be multiple classes which extend MyClass and hence capable of satisfying the T extends MyClass criteria. Without runtime information there is no way of knowing which concrete implementation subclass of MyClass was passed in the method. Hence to provide a generic solution, it returns <? extends MyClass> since that would hold true for any subclass of MyClass irrespective of what class instance is passed in. Hope that clarifies it. Dec 16, 2010 at 16:46
  • 1
    So basically it is because there is no way to ensure at compile time that the cast is correct since the actual type for T is unknown? I think I got it now. By the way, please fix your answer, there is some code missing in the edit. Dec 16, 2010 at 17:11
2

Java does not support a generic type of <this> e.g.

Object could implement

class Object {
    Class<this> getClass()
}

But there is no way for getClass() to express that it will return a type which is the class of the object. The compiler has no native understand of what this method does either.

IMHO, This behaviour should have been supported.

9
  • Good answer. Note however that you kind of explain the second step before the first. The immediate problem is that Object.getClass() does not return the "right" type. You explain why it does not do this.
    – sleske
    Dec 16, 2010 at 12:58
  • Could you point to any resource that explains what exactly is <this>? I can't find it in the language specification and can't compile something like that. Dec 16, 2010 at 19:07
  • Sorry, Java does support the "this-type" feature? It would be great, but I guess you mean does not, right? Because Class<this> getSomething() wouldn't work. Dec 16, 2010 at 19:14
  • @java.is.for.desktop thank you for picking that up, fixed it now. Dec 16, 2010 at 20:35
  • "But there is no way for getClass() to express that it will return a type which is the class of the object." According to that logic, there is also no way for getClass() "to express" that it returns Class<? extends (the erasure of the static type of this)>, but that's actually what it returns.
    – newacct
    May 26, 2016 at 0:24
0

Instead of

Class<? extends T> type = instance.getClass();

you need to use

Class<? extends MyClass> type = instance.getClass();

You cannot directly use the T here.

The reason is the method signature of Object.getClass() (which you are invoking). It's:

public final Class<? extends Object> getClass()

So you are trying to convert from Class<? extends Object> to Class<? extends T>, which is not allowed (because you are down-casting). It is allowed using an explicit cast:

Class<? extends T> type = (Class<? extends T>) instance.getClass();

will work (though it generates a type safety warning).

7
  • This won't be a problem, but the question is why. Is there a reason? At this point we know that instance must be either of class T (whatever that is) or of its subclass. So why ? extends T isn't allowed? Dec 16, 2010 at 12:43
  • @Sergey: The reason is that, while we know that instance must be of type T or subclass, Object.getClass() does not take that into account. See my edited answer.
    – sleske
    Dec 16, 2010 at 12:57
  • 1
    I still don't get it. The javadoc says "The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called." In this case X is T. Are you trying to say that X is actually Object? And that's because it is type parameter (as it works outside of the generic method)? Dec 16, 2010 at 13:12
  • @Sergey: The compiler does not care about the "actual result type" of getClass(). The compiler does static type-checking, therefore only cares about the method signature. And the signature says Class<? extends Object>. If you know that the result type is really a subclass of what the method signature says, you can cast it, but you must cast explicitly.
    – sleske
    Dec 16, 2010 at 16:20
  • By "actual result type" Javadoc means "actual result type of a signature" or "static return type" because it also mentions that no explicit cast is required (to the actual result type), so it's not really an answer. The real answer is mentioned in the comments to Sanjay's answer. Dec 16, 2010 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.