generic protobuf deserializer in Java

Viewed 5970

I'm trying to write a generic java class that can be used to deserialize/parse any protobuf message.

Here's how the code should look in a perfect world:

public abstract class ProtoDeserializer<T extends Message> {

    public T deserialize(final byte[] bytes) throws Exception {     
        Parser<T> parser = T.getParserForType(); // Syntax Error: this method is not static!

        T message = parser.parseFrom(bytes);
        validate(message);
        return message;
    }

    public abstract void validate(final T message) throws Exception;
}

However, I am not able to get the correct parser for a generic protobuf message. What is the correct way to implement such a generic class?

1 Answers
Related