To expand Kenton Varda's answer:
First, I would reorganize your method into separate methods to get the input stream and parse it. Only the latter has reason to be generic.
public InputStream getInputStream() {
Now you intend to parse the input stream and build the POJO from protobuf. Of course, IMO expects that at this moment your code should know what type of object you are going to receive, because otherwise how would you do something clever with it further? For instance.
InputStream is = getInputStream(); Object o = parseGenericInputStream(is); doSomethingWithParsedObject(o); // how to do this if you don't know o type?
You should reasonably know the type o after you have taken it apart (and therefore before analyzing it), otherwise you cannot do anything with it that I can think of.
So ... again with thanks to Kenton Warde:
public void doStuff() { ... InputStream is = getInputStream(); MyProtobufClass pojo = parseGenericInputStream(MyProtobufClass.PARSER, is); doSomethingWithParsedObject(pojo); ... } private <T> T parseGenericInputStream(Parser<T> parser, InputStream inputStream) throws InvalidProtocolBufferException { return parser.parseFrom(inputStream); }
At this point, although you are writing a general method for a single line of code, that is not worth it if you ask me.
source share