Is there any way to hook into the generation of java classes when invoking protoc
? I know there are plugins which can be created for protoc but as far as I understood it is only possible to generate additional files there. What I would like to do is adding additional code into the java classes based on some options in the protobuf schemas.
So for the following schema:
syntax = "proto3";
import "google/protobuf/descriptor.proto";
enum DateFormat {
LOCAL_DATE = 0;
LOCAL_DATE_TIME = 1;
}
extend google.protobuf.FieldOptions {
DateFormat format = 95765;
}
message MyMessage {
string name = 1;
sint64 id = 2;
string dateTimeField = 3 [(format) = LOCAL_DATE_TIME];
}
I would like to have the dateTimeField
in the generated java class of type LocalDateTime
which contains the already parsed date time.
I finally found a solution for this. Plugins can not only create new files, but also use insertion points to extend classes generated by the java code generator, see https://protobuf.dev/reference/java/java-generated/#plugins.
There is a great article which describes exactly how to create a plugin which uses those insertion points.