I have a generic method which has generic type to return. I would like to call it properly in order to return compilation error in case the returning type, I use it for, does not comply the boundaries this way avoiding run-time errors (vs compilation) and use all the power of Java generic types.
public <T extends IDeserializable> T restoreObject(byte[] objectArray) {
T object = T.getDeserialized(objectArray);
return object;
}
However, calling it this way
File ret = restoreObject(new byte[] {1,2,3});
would not produce compilation error and therefore the error will be generated at runtime only. The File type does not implement IDeserializable, however java does not generate compilation error.
public interface IDeserializable {
public static <T> T getDeserialized(byte[] data) {
return null;
}
}