OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 #include <google/protobuf/io/zero_copy_stream.h> | 44 #include <google/protobuf/io/zero_copy_stream.h> |
45 #include <google/protobuf/descriptor.pb.h> | 45 #include <google/protobuf/descriptor.pb.h> |
46 #include <google/protobuf/descriptor.h> | 46 #include <google/protobuf/descriptor.h> |
47 #include <google/protobuf/stubs/strutil.h> | 47 #include <google/protobuf/stubs/strutil.h> |
48 | 48 |
49 namespace google { | 49 namespace google { |
50 namespace protobuf { | 50 namespace protobuf { |
51 namespace compiler { | 51 namespace compiler { |
52 namespace java { | 52 namespace java { |
53 | 53 |
54 SharedCodeGenerator::SharedCodeGenerator(const FileDescriptor* file, | 54 SharedCodeGenerator::SharedCodeGenerator(const FileDescriptor* file) |
55 const Options& options) | 55 : name_resolver_(new ClassNameResolver), |
56 : name_resolver_(new ClassNameResolver), file_(file), options_(options) {} | 56 enforce_lite_(false), |
| 57 file_(file) {} |
57 | 58 |
58 SharedCodeGenerator::~SharedCodeGenerator() { | 59 SharedCodeGenerator::~SharedCodeGenerator() { |
59 } | 60 } |
60 | 61 |
61 void SharedCodeGenerator::Generate(GeneratorContext* context, | 62 void SharedCodeGenerator::Generate(GeneratorContext* context, |
62 std::vector<string>* file_list, | 63 vector<string>* file_list) { |
63 std::vector<string>* annotation_file_list) { | |
64 string java_package = FileJavaPackage(file_); | 64 string java_package = FileJavaPackage(file_); |
65 string package_dir = JavaPackageToDir(java_package); | 65 string package_dir = JavaPackageToDir(java_package); |
66 | 66 |
67 if (HasDescriptorMethods(file_, options_.enforce_lite)) { | 67 if (HasDescriptorMethods(file_, enforce_lite_)) { |
68 // Generate descriptors. | 68 // Generate descriptors. |
69 string classname = name_resolver_->GetDescriptorClassName(file_); | 69 string classname = name_resolver_->GetDescriptorClassName(file_); |
70 string filename = package_dir + classname + ".java"; | 70 string filename = package_dir + classname + ".java"; |
71 file_list->push_back(filename); | 71 file_list->push_back(filename); |
72 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(
filename)); | 72 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(
filename)); |
73 GeneratedCodeInfo annotations; | 73 google::protobuf::scoped_ptr<io::Printer> printer(new io::Printer(output.get
(), '$')); |
74 io::AnnotationProtoCollector<GeneratedCodeInfo> annotation_collector( | 74 |
75 &annotations); | |
76 google::protobuf::scoped_ptr<io::Printer> printer( | |
77 new io::Printer(output.get(), '$', | |
78 options_.annotate_code ? &annotation_collector : NULL)); | |
79 string info_relative_path = classname + ".java.pb.meta"; | |
80 string info_full_path = filename + ".pb.meta"; | |
81 printer->Print( | 75 printer->Print( |
82 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" | 76 "// Generated by the protocol buffer compiler. DO NOT EDIT!\n" |
83 "// source: $filename$\n" | 77 "// source: $filename$\n" |
84 "\n", | 78 "\n", |
85 "filename", file_->name()); | 79 "filename", file_->name()); |
86 if (!java_package.empty()) { | 80 if (!java_package.empty()) { |
87 printer->Print( | 81 printer->Print( |
88 "package $package$;\n" | 82 "package $package$;\n" |
89 "\n", | 83 "\n", |
90 "package", java_package); | 84 "package", java_package); |
91 } | 85 } |
92 PrintGeneratedAnnotation(printer.get(), '$', | |
93 options_.annotate_code ? info_relative_path : ""); | |
94 printer->Print( | 86 printer->Print( |
95 "public final class $classname$ {\n" | 87 "public final class $classname$ {\n" |
96 " public static com.google.protobuf.Descriptors.FileDescriptor\n" | 88 " public static com.google.protobuf.Descriptors.FileDescriptor\n" |
97 " descriptor;\n" | 89 " descriptor;\n" |
98 " static {\n", | 90 " static {\n", |
99 "classname", classname); | 91 "classname", classname); |
100 printer->Annotate("classname", file_->name()); | |
101 printer->Indent(); | 92 printer->Indent(); |
102 printer->Indent(); | 93 printer->Indent(); |
103 GenerateDescriptors(printer.get()); | 94 GenerateDescriptors(printer.get()); |
104 printer->Outdent(); | 95 printer->Outdent(); |
105 printer->Outdent(); | 96 printer->Outdent(); |
106 printer->Print( | 97 printer->Print( |
107 " }\n" | 98 " }\n" |
108 "}\n"); | 99 "}\n"); |
109 | 100 |
110 if (options_.annotate_code) { | |
111 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> info_output( | |
112 context->Open(info_full_path)); | |
113 annotations.SerializeToZeroCopyStream(info_output.get()); | |
114 annotation_file_list->push_back(info_full_path); | |
115 } | |
116 | |
117 printer.reset(); | 101 printer.reset(); |
118 output.reset(); | 102 output.reset(); |
119 } | 103 } |
120 } | 104 } |
121 | 105 |
| 106 |
122 void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { | 107 void SharedCodeGenerator::GenerateDescriptors(io::Printer* printer) { |
123 // Embed the descriptor. We simply serialize the entire FileDescriptorProto | 108 // Embed the descriptor. We simply serialize the entire FileDescriptorProto |
124 // and embed it as a string literal, which is parsed and built into real | 109 // and embed it as a string literal, which is parsed and built into real |
125 // descriptors at initialization time. We unfortunately have to put it in | 110 // descriptors at initialization time. We unfortunately have to put it in |
126 // a string literal, not a byte array, because apparently using a literal | 111 // a string literal, not a byte array, because apparently using a literal |
127 // byte array causes the Java compiler to generate *instructions* to | 112 // byte array causes the Java compiler to generate *instructions* to |
128 // initialize each and every byte of the array, e.g. as if you typed: | 113 // initialize each and every byte of the array, e.g. as if you typed: |
129 // b[0] = 123; b[1] = 456; b[2] = 789; | 114 // b[0] = 123; b[1] = 456; b[2] = 789; |
130 // This makes huge bytecode files and can easily hit the compiler's internal | 115 // This makes huge bytecode files and can easily hit the compiler's internal |
131 // code size limits (error "code to large"). String literals are apparently | 116 // code size limits (error "code to large"). String literals are apparently |
132 // embedded raw, which is what we want. | 117 // embedded raw, which is what we want. |
133 FileDescriptorProto file_proto; | 118 FileDescriptorProto file_proto; |
134 file_->CopyTo(&file_proto); | 119 file_->CopyTo(&file_proto); |
135 | 120 |
| 121 |
136 string file_data; | 122 string file_data; |
137 file_proto.SerializeToString(&file_data); | 123 file_proto.SerializeToString(&file_data); |
138 | 124 |
139 printer->Print( | 125 printer->Print( |
140 "java.lang.String[] descriptorData = {\n"); | 126 "java.lang.String[] descriptorData = {\n"); |
141 printer->Indent(); | 127 printer->Indent(); |
142 | 128 |
143 // Only write 40 bytes per line. | 129 // Only write 40 bytes per line. |
144 static const int kBytesPerLine = 40; | 130 static const int kBytesPerLine = 40; |
145 for (int i = 0; i < file_data.size(); i += kBytesPerLine) { | 131 for (int i = 0; i < file_data.size(); i += kBytesPerLine) { |
(...skipping 26 matching lines...) Expand all Loading... |
172 " descriptor = root;\n" | 158 " descriptor = root;\n" |
173 // Custom options will be handled when immutable messages' outer class is | 159 // Custom options will be handled when immutable messages' outer class is |
174 // loaded. Here we just return null and let custom options be unknown | 160 // loaded. Here we just return null and let custom options be unknown |
175 // fields. | 161 // fields. |
176 " return null;\n" | 162 " return null;\n" |
177 " }\n" | 163 " }\n" |
178 " };\n"); | 164 " };\n"); |
179 | 165 |
180 // ----------------------------------------------------------------- | 166 // ----------------------------------------------------------------- |
181 // Find out all dependencies. | 167 // Find out all dependencies. |
182 std::vector<std::pair<string, string> > dependencies; | 168 vector<pair<string, string> > dependencies; |
183 for (int i = 0; i < file_->dependency_count(); i++) { | 169 for (int i = 0; i < file_->dependency_count(); i++) { |
184 if (ShouldIncludeDependency(file_->dependency(i))) { | 170 if (ShouldIncludeDependency(file_->dependency(i))) { |
185 string filename = file_->dependency(i)->name(); | 171 string filename = file_->dependency(i)->name(); |
186 string classname = FileJavaPackage(file_->dependency(i)) + "." + | 172 string classname = FileJavaPackage(file_->dependency(i)) + "." + |
187 name_resolver_->GetDescriptorClassName( | 173 name_resolver_->GetDescriptorClassName( |
188 file_->dependency(i)); | 174 file_->dependency(i)); |
189 dependencies.push_back(std::make_pair(filename, classname)); | 175 dependencies.push_back(std::make_pair(filename, classname)); |
190 } | 176 } |
191 } | 177 } |
192 | 178 |
(...skipping 18 matching lines...) Expand all Loading... |
211 | 197 |
212 bool SharedCodeGenerator::ShouldIncludeDependency( | 198 bool SharedCodeGenerator::ShouldIncludeDependency( |
213 const FileDescriptor* descriptor) { | 199 const FileDescriptor* descriptor) { |
214 return true; | 200 return true; |
215 } | 201 } |
216 | 202 |
217 } // namespace java | 203 } // namespace java |
218 } // namespace compiler | 204 } // namespace compiler |
219 } // namespace protobuf | 205 } // namespace protobuf |
220 } // namespace google | 206 } // namespace google |
OLD | NEW |