2

I'm really confused right now. I'm using following code in another project without any problems:

public class ReadOnlyTable<T extends Model> implements ReadOnly<T> {
protected at.viswars.database.framework.core.DatabaseTable datasource;
private boolean debug = true;

protected ReadOnlyTable() {
    initTable();
}

protected void reloadDataSource() {
    initTable();
}

private void initTable() {
    boolean readOnlyClass = false;
    if (getClass().getSuperclass().equals(ReadOnlyTable.class)) readOnlyClass = true;

    Class<T> dataClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

The last line will run without problems. Now i made a second project and as i had problems with reading out the Class i tried to do the most simple case possible:

public class GenericObject<T> implements IGenericObject<T> {
public GenericObject() {
    init();
}

private void init() {
    Class<T> clazz = (Class<T>)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

}

This class is instanciated here:

        GenericObject<String> go = new GenericObject<String>();

In my second example i will always receive following error message:

Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

What i am missing?? This drives me crazy. Any help is appreciated!

Rayden78
  • 43
  • 1
  • 1
  • 9

2 Answers2

5

It is impossible to retrieve String from go. The type parameter of an object instantiation expression (new GenericObject<String>()) is not present anywhere at runtime. This is type erasure.

The technique you are trying to use concerns when a class or interface extends or implements another class or interface with a specific type argument. e.g. class Something extends GenericObject<String>. Then it is possible to get this declaration information from the class object. This is completely unrelated to what you are doing.

newacct
  • 119,665
  • 29
  • 163
  • 224
3

You are calling getGenericSuperclass(), but your class does not have a generic superclass. Its superclass is Object, which cannot be cast to ParameterizedType as the error says.

You probably want to use getGenericInterfaces()[0], since your class only implements an interface.

More info in here: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getGenericInterfaces()

user000001
  • 32,226
  • 12
  • 81
  • 108
  • 1
    You correctly point out the error but `getGenericInterfaces` won't help either. `getClass()` will not necessarily return `GenericObject.class` since `GenericObject` isn't `final`. Even if it does, `getGenericInterfaces()[0]` will return a `Type` representing `IGenericObject`, with `T` unresolved. – Paul Bellora Feb 09 '13 at 16:03
  • Ok, you are right according the error message i got above, didn't notice it talked about ParametrizedType. Anyway whats the big difference to my first listing, ReadOnlyTable is the topmost class in hierarchy, so there should be the same Problem. It tried this line: Class clazz = (Class)((ParameterizedType) getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0]; gave me the error: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class Sorry i'm really not familiar with this stuff, will try checking your link – Rayden78 Feb 09 '13 at 16:08
  • related: http://stackoverflow.com/questions/14657542/how-to-use-java-reflection-to-check-a-given-class-is-implements-iterable-exten/14659922#14659922 – Paul Bellora Feb 09 '13 at 16:13
  • Thank you very much for this Link Paul Bellora, i run it on my first listing and there was a resolved type, so i think i can track down the exact difference. I will update this question when i found my mistake. – Rayden78 Feb 10 '13 at 11:10