I. Preface
This article describes how to parse the. proto file in java.
The protobuffer java Library provides direct parsing. the class related to the ptoto file, so it cannot be parsed directly like C ++. the proto file, I google for half a day to find a method on the Internet, so I had this blog post. (The protobuf jar file is required here. You can click here to download it)
II. Specific parsing Methods
You cannot directly use the library provided by protobuf to parse the. proto file, but you can use the. desc file that it provides to retrieve information about the. proto file. Therefore, you must first call the following command to generate the desc file:
[Plain]
Protoc-I = $ SRC_DIR descriptor_set_out = $ DST_DIR/***. desc $ SRC_DIR/***. proto
Protoc-I = $ SRC_DIR descriptor_set_out = $ DST_DIR/***. desc $ SRC_DIR/***. proto
Then you can use FileDescriptorSet to parse the file and obtain FileDescriptor. You can get the desired information by viewing the protobuf API on the official website. The Code is as follows:
[Java]
Package com. test. proto;
Import java. io. FileInputStream;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;
Import com. google. protobuf. DescriptorProtos. FileDescriptorProto;
Import com. google. protobuf. DescriptorProtos. FileDescriptorSet;
Import com. google. protobuf. Descriptors. Descriptor;
Import com. google. protobuf. Descriptors. FieldDescriptor;
Import com. google. protobuf. Descriptors. FileDescriptor;
Public class Test {
Public static void main (String [] args) throws Exception {
Runtime run = Runtime. getRuntime ();
String dir = System. getProperty ("user. dir ");
String source = dir + "/protoc /";
String cmd = "cmd/c" + source + "protoc.exe-I =" + source + "-- descriptor_set_out =" + source + "addressbook. desc "+ source +" addressbook. proto ";
System. out. println (cmd );
Process p = run.exe c (cmd );
// If it is terminated abnormally, the generation of the desc file fails.
If (p. waitFor ()! = 0 ){
If (p. exitValue () = 1) {// p. exitValue () = 0 indicates normal end, 1: Abnormal End
System. err. println ("command execution failed! ");
System. exit (1 );
}
}
Map <String, String> mapping = new HashMap <String, String> ();
FileInputStream fin = new FileInputStream ("addressbook. desc ");
FileDescriptorSet descriptorSet = FileDescriptorSet. parseFrom (fin );
For (FileDescriptorProto fdp: descriptorSet. getFileList ()){
FileDescriptor fd = FileDescriptor. buildFrom (fdp, new FileDescriptor [] {});
For (Descriptor descriptor: fd. getMessageTypes ()){
String className = fdp. getOptions (). getJavaPackage () + "."
+ Fdp. getOptions (). getJavaOuterClassname () + "$"
+ Descriptor. getName ();
List <FieldDescriptor> types = descriptor. getFields ();
For (FieldDescriptor type: types ){
System. out. println (type. getFullName ());
}
System. out. println (descriptor. getFullName () + "->" + className );
}
}
// Descriptor md = fd. getDescriptorForType ();
// Byte [] data = null;
// DynamicMessage m = DynamicMessage. parseFrom (md, data );
}
}
Package com. test. proto;
Import java. io. FileInputStream;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;
Import com. google. protobuf. DescriptorProtos. FileDescriptorProto;
Import com. google. protobuf. DescriptorProtos. FileDescriptorSet;
Import com. google. protobuf. Descriptors. Descriptor;
Import com. google. protobuf. Descriptors. FieldDescriptor;
Import com. google. protobuf. Descriptors. FileDescriptor;
Public class Test {
Public static void main (String [] args) throws Exception {
Runtime run = Runtime. getRuntime ();
String dir = System. getProperty ("user. dir ");
String source = dir + "/protoc /";
String cmd = "cmd/c" + source + "protoc.exe-I =" + source + "-- descriptor_set_out =" + source + "addressbook. desc "+ source +" addressbook. proto ";
System. out. println (cmd );
Process p = run.exe c (cmd );
// If it is terminated abnormally, the generation of the desc file fails.
If (p. waitFor ()! = 0 ){
If (p. exitValue () = 1) {// p. exitValue () = 0 indicates normal end, 1: Abnormal End
System. err. println ("command execution failed! ");
System. exit (1 );
}
}
Map <String, String> mapping = new HashMap <String, String> ();
FileInputStream fin = new FileInputStream ("addressbook. desc ");
FileDescriptorSet descriptorSet = FileDescriptorSet. parseFrom (fin );
For (FileDescriptorProto fdp: descriptorSet. getFileList ()){
FileDescriptor fd = FileDescriptor. buildFrom (fdp, new FileDescriptor [] {});
For (Descriptor descriptor: fd. getMessageTypes ()){
String className = fdp. getOptions (). getJavaPackage () + "."
+ Fdp. getOptions (). getJavaOuterClassname () + "$"
+ Descriptor. getName ();
List <FieldDescriptor> types = descriptor. getFields ();
For (FieldDescriptor type: types ){
System. out. println (type. getFullName ());
}
System. out. println (descriptor. getFullName () + "->" + className );
}
}
// Descriptor md = fd. getDescriptorForType ();
// Byte [] data = null;
// DynamicMessage m = DynamicMessage. parseFrom (md, data );
}
}
Iii. Results
[Java]
Package com. example. tutorial;
Option java_package = "com. example. tutorial ";
Option java_outer_classname = "AddressBookProtos ";
Message Person {
Required string name = 1;
Required int32 id = 2;
Optional string email = 3;
Enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
Message PhoneNumber {
Required string number = 1;
Optional PhoneType type = 2 [default = HOME];
}
Repeated PhoneNumber phone = 4;
}
Message AddressBook {
Repeated Person person = 1;
}
Package com. example. tutorial;
Option java_package = "com. example. tutorial ";
Option java_outer_classname = "AddressBookProtos ";
Message Person {
Required string name = 1;
Required int32 id = 2;
Optional string email = 3;
Enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
Message PhoneNumber {
Required string number = 1;
Optional PhoneType type = 2 [default = HOME];
}
Repeated PhoneNumber phone = 4;
}
Message AddressBook {
Repeated Person person = 1;
}