Skip to content

Instantly share code, notes, and snippets.

@ryoasai
Created March 28, 2011 17:06
Show Gist options
  • Save ryoasai/890851 to your computer and use it in GitHub Desktop.
Save ryoasai/890851 to your computer and use it in GitHub Desktop.
Object.getClass() return type.
import java.util.ArrayList;
import java.util.List;
/**
* <p>Demonstrating why the Object.getClass() method's return type should be
* Class<? extends |T|> but not Class<? extends T>, where |T| is the erasure
* of the static type T. </p>
* <p>
* Type parameter of every Class instance should always be reifiable type
* or wildcard type with reifiable upper bound.
* </p>
* @author Ryo Asai
*/
public class FakeClassFactory<T> {
T data;
public FakeClassFactory(T data) {
this.data = data;
}
@SuppressWarnings("unchecked") // really unsafe
public Class<? extends T> getClassUnsafe() {
return (Class<? extends T>)data.getClass();
}
public static void main(String[] args) throws Exception {
List<String> strList = new ArrayList<String>();
List<Integer> intList = new ArrayList<Integer>();
FakeClassFactory<List<String>> fakeStrListClassFactory = new FakeClassFactory<List<String>>(strList);
Class<? extends List<String>> fakeStrListClass = fakeStrListClassFactory.getClassUnsafe();
FakeClassFactory<List<Integer>> fakeIntListClassFactory = new FakeClassFactory<List<Integer>>(intList);
Class<? extends List<Integer>> fakeIntListClass = fakeIntListClassFactory.getClassUnsafe();
// Actually the same instance.
// Because the type parameter T is erased, and there is really no
// runtime representation of the parameterized type such as List<String>.
System.out.println(fakeStrListClass == fakeIntListClass);
// Invalid cast but no warnings at this line.
strList = fakeStrListClass.cast(intList);
strList.add("test");
int value = intList.get(0); // ClassCastException
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment