1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
|
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: dweis@google.com (Daniel Weis)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
#include <compiler/java/java_message_lite.h>
#include <algorithm>
#include <cstdint>
#include <map>
#include <memory>
#include <vector>
#include <compiler/java/java_context.h>
#include <compiler/java/java_doc_comment.h>
#include <compiler/java/java_enum_lite.h>
#include <compiler/java/java_extension_lite.h>
#include <compiler/java/java_generator_factory.h>
#include <compiler/java/java_helpers.h>
#include <compiler/java/java_message_builder.h>
#include <compiler/java/java_message_builder_lite.h>
#include <compiler/java/java_name_resolver.h>
#include <descriptor.pb.h>
#include <io/coded_stream.h>
#include <io/printer.h>
#include <wire_format.h>
#include <stubs/strutil.h>
#include <stubs/substitute.h>
namespace google {
namespace protobuf {
namespace compiler {
namespace java {
using internal::WireFormat;
using internal::WireFormatLite;
// ===================================================================
ImmutableMessageLiteGenerator::ImmutableMessageLiteGenerator(
const Descriptor* descriptor, Context* context)
: MessageGenerator(descriptor),
context_(context),
name_resolver_(context->GetNameResolver()),
field_generators_(descriptor, context_) {
GOOGLE_CHECK(!HasDescriptorMethods(descriptor->file(), context->EnforceLite()))
<< "Generator factory error: A lite message generator is used to "
"generate non-lite messages.";
for (int i = 0; i < descriptor_->field_count(); i++) {
if (IsRealOneof(descriptor_->field(i))) {
oneofs_.insert(descriptor_->field(i)->containing_oneof());
}
}
}
ImmutableMessageLiteGenerator::~ImmutableMessageLiteGenerator() {}
void ImmutableMessageLiteGenerator::GenerateStaticVariables(
io::Printer* printer, int* bytecode_estimate) {
// Generate static members for all nested types.
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
// TODO(kenton): Reuse MessageGenerator objects?
ImmutableMessageLiteGenerator(descriptor_->nested_type(i), context_)
.GenerateStaticVariables(printer, bytecode_estimate);
}
}
int ImmutableMessageLiteGenerator::GenerateStaticVariableInitializers(
io::Printer* printer) {
int bytecode_estimate = 0;
// Generate static member initializers for all nested types.
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
// TODO(kenton): Reuse MessageGenerator objects?
bytecode_estimate +=
ImmutableMessageLiteGenerator(descriptor_->nested_type(i), context_)
.GenerateStaticVariableInitializers(printer);
}
return bytecode_estimate;
}
// ===================================================================
void ImmutableMessageLiteGenerator::GenerateInterface(io::Printer* printer) {
MaybePrintGeneratedAnnotation(context_, printer, descriptor_,
/* immutable = */ true, "OrBuilder");
if (descriptor_->extension_range_count() > 0) {
printer->Print(
"$deprecation$public interface ${$$classname$OrBuilder$}$ extends \n"
" $extra_interfaces$\n"
" com.google.protobuf.GeneratedMessageLite.\n"
" ExtendableMessageOrBuilder<\n"
" $classname$, $classname$.Builder> {\n",
"deprecation",
descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "",
"extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_),
"classname", descriptor_->name(), "{", "", "}", "");
} else {
printer->Print(
"$deprecation$public interface ${$$classname$OrBuilder$}$ extends\n"
" $extra_interfaces$\n"
" com.google.protobuf.MessageLiteOrBuilder {\n",
"deprecation",
descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "",
"extra_interfaces", ExtraMessageOrBuilderInterfaces(descriptor_),
"classname", descriptor_->name(), "{", "", "}", "");
}
printer->Annotate("{", "}", descriptor_);
printer->Indent();
for (int i = 0; i < descriptor_->field_count(); i++) {
printer->Print("\n");
field_generators_.get(descriptor_->field(i))
.GenerateInterfaceMembers(printer);
}
for (auto oneof : oneofs_) {
printer->Print(
"\n"
"public $classname$.$oneof_capitalized_name$Case "
"get$oneof_capitalized_name$Case();\n",
"oneof_capitalized_name",
context_->GetOneofGeneratorInfo(oneof)->capitalized_name, "classname",
context_->GetNameResolver()->GetImmutableClassName(descriptor_));
}
printer->Outdent();
printer->Print("}\n");
}
// ===================================================================
void ImmutableMessageLiteGenerator::Generate(io::Printer* printer) {
bool is_own_file = IsOwnFile(descriptor_, /* immutable = */ true);
std::map<std::string, std::string> variables;
variables["static"] = is_own_file ? " " : " static ";
variables["classname"] = descriptor_->name();
variables["extra_interfaces"] = ExtraMessageInterfaces(descriptor_);
variables["deprecation"] =
descriptor_->options().deprecated() ? "@java.lang.Deprecated " : "";
WriteMessageDocComment(printer, descriptor_);
MaybePrintGeneratedAnnotation(context_, printer, descriptor_,
/* immutable = */ true);
// The builder_type stores the super type name of the nested Builder class.
std::string builder_type;
if (descriptor_->extension_range_count() > 0) {
printer->Print(
variables,
"$deprecation$public $static$final class $classname$ extends\n"
" com.google.protobuf.GeneratedMessageLite.ExtendableMessage<\n"
" $classname$, $classname$.Builder> implements\n"
" $extra_interfaces$\n"
" $classname$OrBuilder {\n");
builder_type = strings::Substitute(
"com.google.protobuf.GeneratedMessageLite.ExtendableBuilder<$0, ?>",
name_resolver_->GetImmutableClassName(descriptor_));
} else {
printer->Print(
variables,
"$deprecation$public $static$final class $classname$ extends\n"
" com.google.protobuf.GeneratedMessageLite<\n"
" $classname$, $classname$.Builder> implements\n"
" $extra_interfaces$\n"
" $classname$OrBuilder {\n");
builder_type = "com.google.protobuf.GeneratedMessageLite.Builder";
}
printer->Indent();
GenerateConstructor(printer);
// Nested types
for (int i = 0; i < descriptor_->enum_type_count(); i++) {
EnumLiteGenerator(descriptor_->enum_type(i), true, context_)
.Generate(printer);
}
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
// Don't generate Java classes for map entry messages.
if (IsMapEntry(descriptor_->nested_type(i))) continue;
ImmutableMessageLiteGenerator messageGenerator(descriptor_->nested_type(i),
context_);
messageGenerator.GenerateInterface(printer);
messageGenerator.Generate(printer);
}
// Integers for bit fields.
int totalBits = 0;
for (int i = 0; i < descriptor_->field_count(); i++) {
totalBits +=
field_generators_.get(descriptor_->field(i)).GetNumBitsForMessage();
}
int totalInts = (totalBits + 31) / 32;
for (int i = 0; i < totalInts; i++) {
printer->Print("private int $bit_field_name$;\n", "bit_field_name",
GetBitFieldName(i));
}
// oneof
std::map<std::string, std::string> vars;
for (auto oneof : oneofs_) {
vars["oneof_name"] = context_->GetOneofGeneratorInfo(oneof)->name;
vars["oneof_capitalized_name"] =
context_->GetOneofGeneratorInfo(oneof)->capitalized_name;
vars["oneof_index"] = StrCat((oneof)->index());
// oneofCase_ and oneof_
printer->Print(vars,
"private int $oneof_name$Case_ = 0;\n"
"private java.lang.Object $oneof_name$_;\n");
// OneofCase enum
printer->Print(vars, "public enum $oneof_capitalized_name$Case {\n");
printer->Indent();
for (int j = 0; j < (oneof)->field_count(); j++) {
const FieldDescriptor* field = (oneof)->field(j);
printer->Print("$field_name$($field_number$),\n", "field_name",
ToUpper(field->name()), "field_number",
StrCat(field->number()));
}
printer->Print("$cap_oneof_name$_NOT_SET(0);\n", "cap_oneof_name",
ToUpper(vars["oneof_name"]));
printer->Print(vars,
"private final int value;\n"
"private $oneof_capitalized_name$Case(int value) {\n"
" this.value = value;\n"
"}\n");
printer->Print(
vars,
"/**\n"
" * @deprecated Use {@link #forNumber(int)} instead.\n"
" */\n"
"@java.lang.Deprecated\n"
"public static $oneof_capitalized_name$Case valueOf(int value) {\n"
" return forNumber(value);\n"
"}\n"
"\n"
"public static $oneof_capitalized_name$Case forNumber(int value) {\n"
" switch (value) {\n");
for (int j = 0; j < (oneof)->field_count(); j++) {
const FieldDescriptor* field = (oneof)->field(j);
printer->Print(" case $field_number$: return $field_name$;\n",
"field_number", StrCat(field->number()),
"field_name", ToUpper(field->name()));
}
printer->Print(
" case 0: return $cap_oneof_name$_NOT_SET;\n"
" default: return null;\n"
" }\n"
"}\n"
// TODO(b/135620659): Rename this to "getFieldNumber" or something to
// disambiguate it from actual proto enums.
"public int getNumber() {\n"
" return this.value;\n"
"}\n",
"cap_oneof_name", ToUpper(vars["oneof_name"]));
printer->Outdent();
printer->Print("};\n\n");
// oneofCase()
printer->Print(vars,
"@java.lang.Override\n"
"public $oneof_capitalized_name$Case\n"
"get$oneof_capitalized_name$Case() {\n"
" return $oneof_capitalized_name$Case.forNumber(\n"
" $oneof_name$Case_);\n"
"}\n"
"\n"
"private void clear$oneof_capitalized_name$() {\n"
" $oneof_name$Case_ = 0;\n"
" $oneof_name$_ = null;\n"
"}\n"
"\n");
}
// Fields
for (int i = 0; i < descriptor_->field_count(); i++) {
printer->Print("public static final int $constant_name$ = $number$;\n",
"constant_name", FieldConstantName(descriptor_->field(i)),
"number", StrCat(descriptor_->field(i)->number()));
field_generators_.get(descriptor_->field(i)).GenerateMembers(printer);
printer->Print("\n");
}
GenerateParseFromMethods(printer);
GenerateBuilder(printer);
if (HasRequiredFields(descriptor_)) {
// Memoizes whether the protocol buffer is fully initialized (has all
// required fields). 0 means false, 1 means true, and all other values
// mean not yet computed.
printer->Print("private byte memoizedIsInitialized = 2;\n");
}
printer->Print(
"@java.lang.Override\n"
"@java.lang.SuppressWarnings({\"unchecked\", \"fallthrough\"})\n"
"protected final java.lang.Object dynamicMethod(\n"
" com.google.protobuf.GeneratedMessageLite.MethodToInvoke method,\n"
" java.lang.Object arg0, java.lang.Object arg1) {\n"
" switch (method) {\n"
" case NEW_MUTABLE_INSTANCE: {\n"
" return new $classname$();\n"
" }\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_));
printer->Indent();
printer->Indent();
printer->Print("case NEW_BUILDER: {\n");
printer->Indent();
GenerateDynamicMethodNewBuilder(printer);
printer->Outdent();
printer->Print(
"}\n"
"case BUILD_MESSAGE_INFO: {\n");
printer->Indent();
GenerateDynamicMethodNewBuildMessageInfo(printer);
printer->Outdent();
printer->Print(
"}\n"
"// fall through\n"
"case GET_DEFAULT_INSTANCE: {\n"
" return DEFAULT_INSTANCE;\n"
"}\n"
"case GET_PARSER: {\n"
// Generally one would use the lazy initialization holder pattern for
// manipulating static fields but that has exceptional cost on Android as
// it will generate an extra class for every message. Instead, use the
// double-check locking pattern which works just as well.
//
// The "parser" temporary mirrors the "PARSER" field to eliminate a read
// at the final return statement.
" com.google.protobuf.Parser<$classname$> parser = PARSER;\n"
" if (parser == null) {\n"
" synchronized ($classname$.class) {\n"
" parser = PARSER;\n"
" if (parser == null) {\n"
" parser =\n"
" new DefaultInstanceBasedParser<$classname$>(\n"
" DEFAULT_INSTANCE);\n"
" PARSER = parser;\n"
" }\n"
" }\n"
" }\n"
" return parser;\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_));
printer->Outdent();
if (HasRequiredFields(descriptor_)) {
printer->Print(
"}\n"
"case GET_MEMOIZED_IS_INITIALIZED: {\n"
" return memoizedIsInitialized;\n"
"}\n"
"case SET_MEMOIZED_IS_INITIALIZED: {\n"
" memoizedIsInitialized = (byte) (arg0 == null ? 0 : 1);\n"
" return null;\n"
"}\n");
} else {
printer->Print(
"}\n"
"case GET_MEMOIZED_IS_INITIALIZED: {\n"
" return (byte) 1;\n"
"}\n"
"case SET_MEMOIZED_IS_INITIALIZED: {\n"
" return null;\n"
"}\n");
}
printer->Outdent();
printer->Print(
" }\n"
" throw new UnsupportedOperationException();\n"
"}\n"
"\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_));
printer->Print(
"\n"
"// @@protoc_insertion_point(class_scope:$full_name$)\n",
"full_name", descriptor_->full_name());
// Carefully initialize the default instance in such a way that it doesn't
// conflict with other initialization.
printer->Print("private static final $classname$ DEFAULT_INSTANCE;\n",
"classname",
name_resolver_->GetImmutableClassName(descriptor_));
printer->Print(
"static {\n"
" $classname$ defaultInstance = new $classname$();\n"
" // New instances are implicitly immutable so no need to make\n"
" // immutable.\n"
" DEFAULT_INSTANCE = defaultInstance;\n"
// Register the default instance in a map. This map will be used by
// experimental runtime to lookup default instance given a class instance
// without using Java reflection.
" com.google.protobuf.GeneratedMessageLite.registerDefaultInstance(\n"
" $classname$.class, defaultInstance);\n"
"}\n"
"\n",
"classname", descriptor_->name());
printer->Print(
"public static $classname$ getDefaultInstance() {\n"
" return DEFAULT_INSTANCE;\n"
"}\n"
"\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_));
// 'of' method for Wrappers
if (IsWrappersProtoFile(descriptor_->file())) {
printer->Print(
"public static $classname$ of($field_type$ value) {\n"
" return newBuilder().setValue(value).build();\n"
"}\n"
"\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_),
"field_type", PrimitiveTypeName(GetJavaType(descriptor_->field(0))));
}
GenerateParser(printer);
// Extensions must be declared after the DEFAULT_INSTANCE is initialized
// because the DEFAULT_INSTANCE is used by the extension to lazily retrieve
// the outer class's FileDescriptor.
for (int i = 0; i < descriptor_->extension_count(); i++) {
ImmutableExtensionLiteGenerator(descriptor_->extension(i), context_)
.Generate(printer);
}
printer->Outdent();
printer->Print("}\n\n");
}
void ImmutableMessageLiteGenerator::GenerateDynamicMethodNewBuildMessageInfo(
io::Printer* printer) {
printer->Indent();
// Collect field info into a sequence of UTF-16 chars. It will be embedded
// as a Java string in the generated code.
std::vector<uint16_t> chars;
int flags = 0;
if (IsProto2(descriptor_->file())) {
flags |= 0x1;
}
if (descriptor_->options().message_set_wire_format()) {
flags |= 0x2;
}
WriteIntToUtf16CharSequence(flags, &chars);
WriteIntToUtf16CharSequence(descriptor_->field_count(), &chars);
if (descriptor_->field_count() == 0) {
printer->Print("java.lang.Object[] objects = null;");
} else {
// A single array of all fields (including oneof, oneofCase, hasBits).
printer->Print("java.lang.Object[] objects = new java.lang.Object[] {\n");
printer->Indent();
// Record the number of oneofs.
WriteIntToUtf16CharSequence(oneofs_.size(), &chars);
for (auto oneof : oneofs_) {
printer->Print(
"\"$oneof_name$_\",\n"
"\"$oneof_name$Case_\",\n",
"oneof_name", context_->GetOneofGeneratorInfo(oneof)->name);
}
// Integers for bit fields.
int total_bits = 0;
for (int i = 0; i < descriptor_->field_count(); i++) {
total_bits +=
field_generators_.get(descriptor_->field(i)).GetNumBitsForMessage();
}
int total_ints = (total_bits + 31) / 32;
for (int i = 0; i < total_ints; i++) {
printer->Print("\"$bit_field_name$\",\n", "bit_field_name",
GetBitFieldName(i));
}
WriteIntToUtf16CharSequence(total_ints, &chars);
int map_count = 0;
int repeated_count = 0;
std::unique_ptr<const FieldDescriptor*[]> sorted_fields(
SortFieldsByNumber(descriptor_));
for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* field = sorted_fields[i];
if (field->is_map()) {
map_count++;
} else if (field->is_repeated()) {
repeated_count++;
}
}
WriteIntToUtf16CharSequence(sorted_fields[0]->number(), &chars);
WriteIntToUtf16CharSequence(
sorted_fields[descriptor_->field_count() - 1]->number(), &chars);
WriteIntToUtf16CharSequence(descriptor_->field_count(), &chars);
WriteIntToUtf16CharSequence(map_count, &chars);
WriteIntToUtf16CharSequence(repeated_count, &chars);
std::vector<const FieldDescriptor*> fields_for_is_initialized_check;
for (int i = 0; i < descriptor_->field_count(); i++) {
if (descriptor_->field(i)->is_required() ||
(GetJavaType(descriptor_->field(i)) == JAVATYPE_MESSAGE &&
HasRequiredFields(descriptor_->field(i)->message_type()))) {
fields_for_is_initialized_check.push_back(descriptor_->field(i));
}
}
WriteIntToUtf16CharSequence(fields_for_is_initialized_check.size(), &chars);
for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* field = sorted_fields[i];
field_generators_.get(field).GenerateFieldInfo(printer, &chars);
}
printer->Outdent();
printer->Print("};\n");
}
printer->Print("java.lang.String info =\n");
std::string line;
for (size_t i = 0; i < chars.size(); i++) {
uint16_t code = chars[i];
EscapeUtf16ToString(code, &line);
if (line.size() >= 80) {
printer->Print(" \"$string$\" +\n", "string", line);
line.clear();
}
}
printer->Print(" \"$string$\";\n", "string", line);
printer->Print("return newMessageInfo(DEFAULT_INSTANCE, info, objects);\n");
printer->Outdent();
}
// ===================================================================
void ImmutableMessageLiteGenerator::GenerateParseFromMethods(
io::Printer* printer) {
printer->Print(
"public static $classname$ parseFrom(\n"
" java.nio.ByteBuffer data)\n"
" throws com.google.protobuf.InvalidProtocolBufferException {\n"
" return com.google.protobuf.GeneratedMessageLite.parseFrom(\n"
" DEFAULT_INSTANCE, data);\n"
"}\n"
"public static $classname$ parseFrom(\n"
" java.nio.ByteBuffer data,\n"
" com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
" throws com.google.protobuf.InvalidProtocolBufferException {\n"
" return com.google.protobuf.GeneratedMessageLite.parseFrom(\n"
" DEFAULT_INSTANCE, data, extensionRegistry);\n"
"}\n"
"public static $classname$ parseFrom(\n"
" com.google.protobuf.ByteString data)\n"
" throws com.google.protobuf.InvalidProtocolBufferException {\n"
" return com.google.protobuf.GeneratedMessageLite.parseFrom(\n"
" DEFAULT_INSTANCE, data);\n"
"}\n"
"public static $classname$ parseFrom(\n"
" com.google.protobuf.ByteString data,\n"
" com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
" throws com.google.protobuf.InvalidProtocolBufferException {\n"
" return com.google.protobuf.GeneratedMessageLite.parseFrom(\n"
" DEFAULT_INSTANCE, data, extensionRegistry);\n"
"}\n"
"public static $classname$ parseFrom(byte[] data)\n"
" throws com.google.protobuf.InvalidProtocolBufferException {\n"
" return com.google.protobuf.GeneratedMessageLite.parseFrom(\n"
" DEFAULT_INSTANCE, data);\n"
"}\n"
"public static $classname$ parseFrom(\n"
" byte[] data,\n"
" com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
" throws com.google.protobuf.InvalidProtocolBufferException {\n"
" return com.google.protobuf.GeneratedMessageLite.parseFrom(\n"
" DEFAULT_INSTANCE, data, extensionRegistry);\n"
"}\n"
"public static $classname$ parseFrom(java.io.InputStream input)\n"
" throws java.io.IOException {\n"
" return com.google.protobuf.GeneratedMessageLite.parseFrom(\n"
" DEFAULT_INSTANCE, input);\n"
"}\n"
"public static $classname$ parseFrom(\n"
" java.io.InputStream input,\n"
" com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
" throws java.io.IOException {\n"
" return com.google.protobuf.GeneratedMessageLite.parseFrom(\n"
" DEFAULT_INSTANCE, input, extensionRegistry);\n"
"}\n"
"public static $classname$ parseDelimitedFrom(java.io.InputStream "
"input)\n"
" throws java.io.IOException {\n"
" return parseDelimitedFrom(DEFAULT_INSTANCE, input);\n"
"}\n"
"public static $classname$ parseDelimitedFrom(\n"
" java.io.InputStream input,\n"
" com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
" throws java.io.IOException {\n"
" return parseDelimitedFrom(DEFAULT_INSTANCE, input, "
"extensionRegistry);\n"
"}\n"
"public static $classname$ parseFrom(\n"
" com.google.protobuf.CodedInputStream input)\n"
" throws java.io.IOException {\n"
" return com.google.protobuf.GeneratedMessageLite.parseFrom(\n"
" DEFAULT_INSTANCE, input);\n"
"}\n"
"public static $classname$ parseFrom(\n"
" com.google.protobuf.CodedInputStream input,\n"
" com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
" throws java.io.IOException {\n"
" return com.google.protobuf.GeneratedMessageLite.parseFrom(\n"
" DEFAULT_INSTANCE, input, extensionRegistry);\n"
"}\n"
"\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_));
}
// ===================================================================
void ImmutableMessageLiteGenerator::GenerateBuilder(io::Printer* printer) {
printer->Print(
"public static Builder newBuilder() {\n"
" return (Builder) DEFAULT_INSTANCE.createBuilder();\n"
"}\n"
"public static Builder newBuilder($classname$ prototype) {\n"
" return (Builder) DEFAULT_INSTANCE.createBuilder(prototype);\n"
"}\n"
"\n",
"classname", name_resolver_->GetImmutableClassName(descriptor_));
MessageBuilderLiteGenerator builderGenerator(descriptor_, context_);
builderGenerator.Generate(printer);
}
// ===================================================================
void ImmutableMessageLiteGenerator::GenerateDynamicMethodNewBuilder(
io::Printer* printer) {
printer->Print("return new Builder();\n");
}
// ===================================================================
void ImmutableMessageLiteGenerator::GenerateExtensionRegistrationCode(
io::Printer* printer) {
for (int i = 0; i < descriptor_->extension_count(); i++) {
ImmutableExtensionLiteGenerator(descriptor_->extension(i), context_)
.GenerateRegistrationCode(printer);
}
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
ImmutableMessageLiteGenerator(descriptor_->nested_type(i), context_)
.GenerateExtensionRegistrationCode(printer);
}
}
// ===================================================================
void ImmutableMessageLiteGenerator::GenerateConstructor(io::Printer* printer) {
printer->Print("private $classname$() {\n", "classname", descriptor_->name());
printer->Indent();
// Initialize all fields to default.
GenerateInitializers(printer);
printer->Outdent();
printer->Print("}\n");
}
// ===================================================================
void ImmutableMessageLiteGenerator::GenerateParser(io::Printer* printer) {
printer->Print(
"private static volatile com.google.protobuf.Parser<$classname$> "
"PARSER;\n"
"\n"
"public static com.google.protobuf.Parser<$classname$> parser() {\n"
" return DEFAULT_INSTANCE.getParserForType();\n"
"}\n",
"classname", descriptor_->name());
}
// ===================================================================
void ImmutableMessageLiteGenerator::GenerateInitializers(io::Printer* printer) {
for (int i = 0; i < descriptor_->field_count(); i++) {
if (!IsRealOneof(descriptor_->field(i))) {
field_generators_.get(descriptor_->field(i))
.GenerateInitializationCode(printer);
}
}
}
void ImmutableMessageLiteGenerator::GenerateKotlinDsl(
io::Printer* printer) const {
printer->Print(
"@kotlin.OptIn"
"(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n"
"@com.google.protobuf.kotlin.ProtoDslMarker\n");
printer->Print(
"public class Dsl private constructor(\n"
" private val _builder: $message$.Builder\n"
") {\n"
" public companion object {\n"
" @kotlin.jvm.JvmSynthetic\n"
" @kotlin.PublishedApi\n"
" internal fun _create(builder: $message$.Builder): Dsl = "
"Dsl(builder)\n"
" }\n"
"\n"
" @kotlin.jvm.JvmSynthetic\n"
" @kotlin.PublishedApi\n"
" internal fun _build(): $message$ = _builder.build()\n",
"message", name_resolver_->GetClassName(descriptor_, true));
printer->Indent();
for (int i = 0; i < descriptor_->field_count(); i++) {
printer->Print("\n");
field_generators_.get(descriptor_->field(i))
.GenerateKotlinDslMembers(printer);
}
for (auto oneof : oneofs_) {
printer->Print(
"public val $oneof_name$Case: $message$.$oneof_capitalized_name$Case\n"
" @JvmName(\"get$oneof_capitalized_name$Case\")\n"
" get() = _builder.get$oneof_capitalized_name$Case()\n\n"
"public fun clear$oneof_capitalized_name$() {\n"
" _builder.clear$oneof_capitalized_name$()\n"
"}\n",
"oneof_name", context_->GetOneofGeneratorInfo(oneof)->name,
"oneof_capitalized_name",
context_->GetOneofGeneratorInfo(oneof)->capitalized_name, "message",
name_resolver_->GetClassName(descriptor_, true));
}
if (descriptor_->extension_range_count() > 0) {
GenerateKotlinExtensions(printer);
}
printer->Outdent();
printer->Print("}\n");
}
void ImmutableMessageLiteGenerator::GenerateKotlinMembers(
io::Printer* printer) const {
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"public inline fun $camelcase_name$(block: $message_kt$.Dsl.() -> "
"kotlin.Unit): "
"$message$ =\n"
" $message_kt$.Dsl._create($message$.newBuilder()).apply { block() "
"}._build()\n",
"camelcase_name", name_resolver_->GetKotlinFactoryName(descriptor_),
"message_kt", name_resolver_->GetKotlinExtensionsClassName(descriptor_),
"message", name_resolver_->GetClassName(descriptor_, true));
printer->Print("public object $name$Kt {\n", "name", descriptor_->name());
printer->Indent();
GenerateKotlinDsl(printer);
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
if (IsMapEntry(descriptor_->nested_type(i))) continue;
ImmutableMessageLiteGenerator(descriptor_->nested_type(i), context_)
.GenerateKotlinMembers(printer);
}
printer->Outdent();
printer->Print("}\n");
}
void ImmutableMessageLiteGenerator::GenerateTopLevelKotlinMembers(
io::Printer* printer) const {
printer->Print(
"public inline fun $message$.copy(block: $message_kt$.Dsl.() -> "
"kotlin.Unit): "
"$message$ =\n"
" $message_kt$.Dsl._create(this.toBuilder()).apply { block() "
"}._build()\n",
"message", name_resolver_->GetClassName(descriptor_, true), "message_kt",
name_resolver_->GetKotlinExtensionsClassName(descriptor_));
for (int i = 0; i < descriptor_->nested_type_count(); i++) {
if (IsMapEntry(descriptor_->nested_type(i))) continue;
ImmutableMessageLiteGenerator(descriptor_->nested_type(i), context_)
.GenerateTopLevelKotlinMembers(printer);
}
}
void ImmutableMessageLiteGenerator::GenerateKotlinExtensions(
io::Printer* printer) const {
std::string message_name = name_resolver_->GetClassName(descriptor_, true);
printer->Print(
"@Suppress(\"UNCHECKED_CAST\")\n"
"@kotlin.jvm.JvmSynthetic\n"
"public operator fun <T> get(extension: "
"com.google.protobuf.ExtensionLite<$message$, T>): T {\n"
" return if (extension.isRepeated) {\n"
" get(extension as com.google.protobuf.ExtensionLite<$message$, "
"List<*>>) as T\n"
" } else {\n"
" _builder.getExtension(extension)\n"
" }\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@kotlin.OptIn"
"(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n"
"@kotlin.jvm.JvmName(\"-getRepeatedExtension\")\n"
"public operator fun <E> get(\n"
" extension: com.google.protobuf.ExtensionLite<$message$, List<E>>\n"
"): com.google.protobuf.kotlin.ExtensionList<E, $message$> {\n"
" return com.google.protobuf.kotlin.ExtensionList(extension, "
"_builder.getExtension(extension))\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"public operator fun contains(extension: "
"com.google.protobuf.ExtensionLite<$message$, *>): "
"Boolean {\n"
" return _builder.hasExtension(extension)\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"public fun clear(extension: "
"com.google.protobuf.ExtensionLite<$message$, *>) "
"{\n"
" _builder.clearExtension(extension)\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@kotlin.PublishedApi\n"
"internal fun <T> setExtension(extension: "
"com.google.protobuf.ExtensionLite<$message$, T>, "
"value: T) {\n"
" _builder.setExtension(extension, value)\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun <T : Comparable<T>> set(\n"
" extension: com.google.protobuf.ExtensionLite<$message$, T>,\n"
" value: T\n"
") {\n"
" setExtension(extension, value)\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun set(\n"
" extension: com.google.protobuf.ExtensionLite<$message$, "
"com.google.protobuf.ByteString>,\n"
" value: com.google.protobuf.ByteString\n"
") {\n"
" setExtension(extension, value)\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun <T : com.google.protobuf.MessageLite> set(\n"
" extension: com.google.protobuf.ExtensionLite<$message$, T>,\n"
" value: T\n"
") {\n"
" setExtension(extension, value)\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"public fun<E> com.google.protobuf.kotlin.ExtensionList<E, "
"$message$>.add(value: E) {\n"
" _builder.addExtension(this.extension, value)\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun <E> "
"com.google.protobuf.kotlin.ExtensionList<E, "
"$message$>.plusAssign"
"(value: E) {\n"
" add(value)\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"public fun<E> com.google.protobuf.kotlin.ExtensionList<E, "
"$message$>.addAll(values: Iterable<E>) {\n"
" for (value in values) {\n"
" add(value)\n"
" }\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline operator fun <E> "
"com.google.protobuf.kotlin.ExtensionList<E, "
"$message$>.plusAssign(values: "
"Iterable<E>) {\n"
" addAll(values)\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"public operator fun <E> com.google.protobuf.kotlin.ExtensionList<E, "
"$message$>.set(index: Int, value: "
"E) {\n"
" _builder.setExtension(this.extension, index, value)\n"
"}\n\n",
"message", message_name);
printer->Print(
"@kotlin.jvm.JvmSynthetic\n"
"@Suppress(\"NOTHING_TO_INLINE\")\n"
"public inline fun com.google.protobuf.kotlin.ExtensionList<*, "
"$message$>.clear() {\n"
" clear(extension)\n"
"}\n\n",
"message", message_name);
}
} // namespace java
} // namespace compiler
} // namespace protobuf
} // namespace google
|