Skip to content
Advertisement

proto3: Why setting java_multiple_files = true not creating separate java classes for each proto message?

I have a proto definition with nested classes

JavaScript

When I generate the java classes with option java_multiple_files = false; it creates a single java class, ClusterConfigOuterClass.java with all the Java classes/enums/etc generated for the top-level messages, services, and enumerations nested inside it. This is expected.

But if I use option java_multiple_files = true; then I am seeing it is generating two additional classes ClusterConfig.java, ClusterConfigOrBuilder.java along with ClusterConfigOuterClass.java. ClusteConfig.java now contains the nested classes.

The documentation states like below:-

java_multiple_files (file option): If false, only a single .java file will be generated for this .proto file, and all the Java classes/enums/etc. generated for the top-level messages, services, and enumerations will be nested inside of an outer class (see java_outer_classname). If true, separate .java files will be generated for each of the Java classes/enums/etc. generated for the top-level messages, services, and enumerations, and the wrapper Java class generated for this .proto file won’t contain any nested classes/enums/etc. This is a Boolean option that defaults to false. If not generating Java code, this option has no effect.

So should not each nested message like Kafka, Network, etc go into a separate java file?

  • Java 11
  • Protoc – 3.10

Advertisement

Answer

With only Nested classes, the java_multiple_files will not do anything, and this is on purpose. You are explicitly giving a context for Kafka, Network, etc… They are relevant only in their parent object.

Now, if you really wanted to generate multiple files, you would have to do something like:

JavaScript

Then, to go further, if you wanted to give them a Context in which these objects should be used (maybe because you have team, and you don’t want other developers to misuse these objects), you could put them in a package like:

JavaScript

and then to use your Kafka object for example, they will need to give the fully qualified type (except if they are in the same package):

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement