vendredi 12 août 2016

How to create protobuf instances using java reflection?

Normally you create an probobuf class instance like this:

Bar.Builder bld = Bar.newBuilder();
bld.setXYZ(...

I have a usecase that uses the Java reflection to instantiate a protobuf class:

Class clsBar = Class.forName("com.xyz.Foo$Bar");
Object instance = clsBar.newInstance(); // error here!
Method mth = clsBar.getMethod(...);

The above code works fine with normal Java classes. But for a generated protobuf class "com.xyz.Foo$Bar", it gives me an NoSuchMethodException, as there isn't a default public ctor there.

Any suggestions on how to use Java refection to create probobuf instances? The question is for someone who's really good at protobuf internals. Thank!





Aucun commentaire:

Enregistrer un commentaire