You can declare an abstract parseFrom method in the Message class. Then concrete classes will be required to implement it, which is exactly what you need for the above code to function properly.
Based on Tom Khotin's comment, the version of my answer was modified here:
public class GPBFormat<T extends Message> implements IGPBFormat<T> { private Class<T> clazz; public GPBFormat(Class<T> clazz) { this.clazz = clazz; } @Override public byte[] serialize(T t) { return t.toByteArray(); } @Override public T deSerialize(byte[] value) { try { T thing = clazz.newInstance(); thing.parseFrom(value); return thing; } catch (Exception e) {
Each concrete class will need a no-arg constructor.
source share