Skip to main content

Posts

Showing posts from October, 2011

Java Problem: Generic Inheritance and Calling GetMethod().getReturnType()

In my current project, I have classes which are modeled like the following. At some point, a method like getReturnTypeForGetId() is called on classes A and B. Calling the method with A returns Integer as expected, but B returns Serializable.What am I missing here? Am I getting bitten by some heinous erasure thing, or am I just missing out on some sort of generic context-clobbering?EDIT: Adding an over-ridden getId() method to B fixes the problem, but I would still like to understand what I am running into.I've also asked this question on stackoverflow.importjava.io.Serializable;publicclassWeirdTester{staticinterfaceIdentifiable<T extends Serializable>{ T getId();voidsetId(final T id);}staticabstractclassBaseEntity<T extends Serializable>implements Identifiable<T>{private T id;public T getId(){return id;}publicvoidsetId(final T id){this.id= id;}}staticclassAimplements Identifiable<Integer>{private Integer id;public Integer getId(){ret…