Go to the documentation of this file.
35 #include <google/protobuf/compiler/java/java_message.h>
43 #include <google/protobuf/compiler/java/java_context.h>
44 #include <google/protobuf/compiler/java/java_doc_comment.h>
45 #include <google/protobuf/compiler/java/java_enum.h>
46 #include <google/protobuf/compiler/java/java_extension.h>
47 #include <google/protobuf/compiler/java/java_generator_factory.h>
48 #include <google/protobuf/compiler/java/java_helpers.h>
49 #include <google/protobuf/compiler/java/java_message_builder.h>
50 #include <google/protobuf/compiler/java/java_message_builder_lite.h>
51 #include <google/protobuf/compiler/java/java_name_resolver.h>
52 #include <google/protobuf/descriptor.pb.h>
53 #include <google/protobuf/io/coded_stream.h>
54 #include <google/protobuf/io/printer.h>
55 #include <google/protobuf/wire_format.h>
56 #include <google/protobuf/stubs/strutil.h>
57 #include <google/protobuf/stubs/substitute.h>
64 using internal::WireFormat;
65 using internal::WireFormatLite;
69 ClassNameResolver* name_resolver) {
72 return name_resolver->GetImmutableClassName(value_field->message_type());
94 name_resolver_(
context->GetNameResolver()),
97 <<
"Generator factory error: A non-lite message generator is used to "
98 "generate lite messages.";
112 std::map<std::string, std::string> vars;
122 vars[
"private"] =
"";
124 vars[
"private"] =
"private ";
127 vars[
"final"] =
"final ";
138 "$private$static $final$com.google.protobuf.Descriptors.Descriptor\n"
139 " internal_$identifier$_descriptor;\n");
140 *bytecode_estimate += 30;
149 .GenerateStaticVariables(printer, bytecode_estimate);
155 int bytecode_estimate = 0;
156 std::map<std::string, std::string> vars;
167 "internal_$identifier$_descriptor =\n"
168 " getDescriptor().getMessageTypes().get($index$);\n");
169 bytecode_estimate += 30;
173 "internal_$identifier$_descriptor =\n"
174 " internal_$parent$_descriptor.getNestedTypes().get($index$);\n");
175 bytecode_estimate += 30;
186 .GenerateStaticVariableInitializers(printer);
188 return bytecode_estimate;
193 std::map<std::string, std::string> vars;
198 vars[
"private"] =
"";
200 vars[
"private"] =
"private ";
203 vars[
"final"] =
"final ";
210 "$private$static $final$\n"
211 " com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n"
212 " internal_$identifier$_fieldAccessorTable;\n");
220 *bytecode_estimate +=
226 int bytecode_estimate = 10;
228 "internal_$identifier$_fieldAccessorTable = new\n"
229 " com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable(\n"
230 " internal_$identifier$_descriptor,\n"
231 " new java.lang.String[] { ",
241 bytecode_estimate += 6;
242 printer->Print(
"\"$field_name$\", ",
"field_name", info->capitalized_name);
248 bytecode_estimate += 6;
249 printer->Print(
"\"$oneof_name$\", ",
"oneof_name", info->capitalized_name);
251 printer->Print(
"});\n");
252 return bytecode_estimate;
262 "$deprecation$public interface ${$$classname$OrBuilder$}$ extends\n"
263 " $extra_interfaces$\n"
264 " com.google.protobuf.GeneratedMessage$ver$.\n"
265 " ExtendableMessageOrBuilder<$classname$> {\n",
273 "$deprecation$public interface ${$$classname$OrBuilder$}$ extends\n"
274 " $extra_interfaces$\n"
275 " com.google.protobuf.MessageOrBuilder {\n",
285 printer->Print(
"\n");
287 .GenerateInterfaceMembers(printer);
292 "public $classname$.$oneof_capitalized_name$Case "
293 "get$oneof_capitalized_name$Case();\n",
294 "oneof_capitalized_name",
300 printer->Print(
"}\n");
308 std::map<std::string, std::string> variables;
309 variables[
"static"] = is_own_file ?
"" :
"static ";
313 variables[
"deprecation"] =
324 "$deprecation$public $static$final class $classname$ extends\n");
328 " com.google.protobuf.GeneratedMessage$ver$.ExtendableMessage<\n"
329 " $classname$> implements\n"
330 " $extra_interfaces$\n"
331 " $classname$OrBuilder {\n");
333 "com.google.protobuf.GeneratedMessage$1.ExtendableBuilder<$0, ?>",
339 "$deprecation$public $static$final class $classname$ extends\n");
341 printer->Print(variables,
342 " com.google.protobuf.GeneratedMessage$ver$ implements\n"
343 " $extra_interfaces$\n"
344 " $classname$OrBuilder {\n");
349 printer->Print(
"private static final long serialVersionUID = 0L;\n");
357 "// Use $classname$.newBuilder() to construct.\n"
358 "private $classname$($buildertype$ builder) {\n"
362 printer->Print(
"private $classname$() {\n",
"classname",
descriptor_->
name());
370 printer->Print(variables,
371 "@java.lang.Override\n"
372 "@SuppressWarnings({\"unused\"})\n"
373 "protected java.lang.Object newInstance(\n"
374 " UnusedPrivateParameter unused) {\n"
375 " return new $classname$();\n"
380 "@java.lang.Override\n"
381 "public final com.google.protobuf.UnknownFieldSet\n"
382 "getUnknownFields() {\n"
383 " return this.unknownFields;\n"
402 messageGenerator.GenerateInterface(printer);
403 messageGenerator.Generate(printer);
412 int totalInts = (totalBits + 31) / 32;
413 for (
int i = 0;
i < totalInts;
i++) {
414 printer->Print(
"private int $bit_field_name$;\n",
"bit_field_name",
419 std::map<std::string, std::string> vars;
422 vars[
"oneof_capitalized_name"] =
427 "private int $oneof_name$Case_ = 0;\n"
428 "private java.lang.Object $oneof_name$_;\n");
432 "public enum $oneof_capitalized_name$Case\n"
435 " implements com.google.protobuf.Internal.EnumLite,\n"
436 " com.google.protobuf.AbstractMessage.InternalOneOfEnum {\n");
438 for (
int j = 0;
j < (oneof)->field_count();
j++) {
441 "$deprecation$$field_name$($field_number$),\n",
"deprecation",
442 field->options().deprecated() ?
"@java.lang.Deprecated " :
"",
446 printer->Print(
"$cap_oneof_name$_NOT_SET(0);\n",
"cap_oneof_name",
449 "private final int value;\n"
450 "private $oneof_capitalized_name$Case(int value) {\n"
451 " this.value = value;\n"
456 " * @param value The number of the enum to look for.\n"
457 " * @return The enum associated with the given number.\n"
458 " * @deprecated Use {@link #forNumber(int)} instead.\n"
460 "@java.lang.Deprecated\n"
461 "public static $oneof_capitalized_name$Case valueOf(int value) {\n"
462 " return forNumber(value);\n"
465 "public static $oneof_capitalized_name$Case forNumber(int value) {\n"
466 " switch (value) {\n");
467 for (
int j = 0;
j < (oneof)->field_count();
j++) {
469 printer->Print(
" case $field_number$: return $field_name$;\n",
474 " case 0: return $cap_oneof_name$_NOT_SET;\n"
475 " default: return null;\n"
478 "public int getNumber() {\n"
479 " return this.value;\n"
481 "cap_oneof_name",
ToUpper(vars[
"oneof_name"]));
483 printer->Print(
"};\n\n");
486 "public $oneof_capitalized_name$Case\n"
487 "get$oneof_capitalized_name$Case() {\n"
488 " return $oneof_capitalized_name$Case.forNumber(\n"
489 " $oneof_name$Case_);\n"
500 printer->Print(
"public static final int $constant_name$ = $number$;\n",
505 printer->Print(
"\n");
520 "// @@protoc_insertion_point(class_scope:$full_name$)\n",
525 printer->Print(
"private static final $classname$ DEFAULT_INSTANCE;\n",
530 " DEFAULT_INSTANCE = new $classname$();\n"
536 "public static $classname$ getDefaultInstance() {\n"
537 " return DEFAULT_INSTANCE;\n"
545 "public static $classname$ of($field_type$ value) {\n"
546 " return newBuilder().setValue(value).build();\n"
556 "@java.lang.Override\n"
557 "public $classname$ getDefaultInstanceForType() {\n"
558 " return DEFAULT_INSTANCE;\n"
572 printer->Print(
"}\n\n");
579 std::unique_ptr<const FieldDescriptor*[]> sorted_fields(
582 std::vector<const Descriptor::ExtensionRange*> sorted_extensions;
587 std::sort(sorted_extensions.begin(), sorted_extensions.end(),
588 ExtensionRangeOrdering());
590 "@java.lang.Override\n"
591 "public void writeTo(com.google.protobuf.CodedOutputStream output)\n"
592 " throws java.io.IOException {\n");
602 printer->Print(
"getSerializedSize();\n");
608 "com.google.protobuf.GeneratedMessage$ver$\n"
609 " .ExtendableMessage<$classname$>.ExtensionWriter\n"
610 " extensionWriter = newMessageSetExtensionWriter();\n",
615 "com.google.protobuf.GeneratedMessage$ver$\n"
616 " .ExtendableMessage<$classname$>.ExtensionWriter\n"
617 " extensionWriter = newExtensionWriter();\n",
624 for (
int i = 0, j = 0;
628 }
else if (j == sorted_extensions.size()) {
630 }
else if (sorted_fields[i]->
number() < sorted_extensions[j]->
start) {
638 printer->Print(
"unknownFields.writeAsMessageSetTo(output);\n");
640 printer->Print(
"unknownFields.writeTo(output);\n");
647 "@java.lang.Override\n"
648 "public int getSerializedSize() {\n"
649 " int size = memoizedSize;\n"
650 " if (size != -1) return size;\n"
654 printer->Print(
"size = 0;\n");
662 printer->Print(
"size += extensionsSerializedSizeAsMessageSet();\n");
664 printer->Print(
"size += extensionsSerializedSize();\n");
669 printer->Print(
"size += unknownFields.getSerializedSizeAsMessageSet();\n");
671 printer->Print(
"size += unknownFields.getSerializedSize();\n");
675 "memoizedSize = size;\n"
689 "public static $classname$ parseFrom(\n"
690 " java.nio.ByteBuffer data)\n"
691 " throws com.google.protobuf.InvalidProtocolBufferException {\n"
692 " return PARSER.parseFrom(data);\n"
694 "public static $classname$ parseFrom(\n"
695 " java.nio.ByteBuffer data,\n"
696 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
697 " throws com.google.protobuf.InvalidProtocolBufferException {\n"
698 " return PARSER.parseFrom(data, extensionRegistry);\n"
700 "public static $classname$ parseFrom(\n"
701 " com.google.protobuf.ByteString data)\n"
702 " throws com.google.protobuf.InvalidProtocolBufferException {\n"
703 " return PARSER.parseFrom(data);\n"
705 "public static $classname$ parseFrom(\n"
706 " com.google.protobuf.ByteString data,\n"
707 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
708 " throws com.google.protobuf.InvalidProtocolBufferException {\n"
709 " return PARSER.parseFrom(data, extensionRegistry);\n"
711 "public static $classname$ parseFrom(byte[] data)\n"
712 " throws com.google.protobuf.InvalidProtocolBufferException {\n"
713 " return PARSER.parseFrom(data);\n"
715 "public static $classname$ parseFrom(\n"
717 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
718 " throws com.google.protobuf.InvalidProtocolBufferException {\n"
719 " return PARSER.parseFrom(data, extensionRegistry);\n"
721 "public static $classname$ parseFrom(java.io.InputStream input)\n"
722 " throws java.io.IOException {\n"
723 " return com.google.protobuf.GeneratedMessage$ver$\n"
724 " .parseWithIOException(PARSER, input);\n"
726 "public static $classname$ parseFrom(\n"
727 " java.io.InputStream input,\n"
728 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
729 " throws java.io.IOException {\n"
730 " return com.google.protobuf.GeneratedMessage$ver$\n"
731 " .parseWithIOException(PARSER, input, extensionRegistry);\n"
733 "public static $classname$ parseDelimitedFrom(java.io.InputStream "
735 " throws java.io.IOException {\n"
736 " return com.google.protobuf.GeneratedMessage$ver$\n"
737 " .parseDelimitedWithIOException(PARSER, input);\n"
739 "public static $classname$ parseDelimitedFrom(\n"
740 " java.io.InputStream input,\n"
741 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
742 " throws java.io.IOException {\n"
743 " return com.google.protobuf.GeneratedMessage$ver$\n"
744 " .parseDelimitedWithIOException(PARSER, input, "
745 "extensionRegistry);\n"
747 "public static $classname$ parseFrom(\n"
748 " com.google.protobuf.CodedInputStream input)\n"
749 " throws java.io.IOException {\n"
750 " return com.google.protobuf.GeneratedMessage$ver$\n"
751 " .parseWithIOException(PARSER, input);\n"
753 "public static $classname$ parseFrom(\n"
754 " com.google.protobuf.CodedInputStream input,\n"
755 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
756 " throws java.io.IOException {\n"
757 " return com.google.protobuf.GeneratedMessage$ver$\n"
758 " .parseWithIOException(PARSER, input, extensionRegistry);\n"
772 printer->Print(
"extensionWriter.writeUntil($end$, output);\n",
"end",
781 "@java.lang.Override\n"
782 "public Builder newBuilderForType() { return newBuilder(); }\n");
785 "public static Builder newBuilder() {\n"
786 " return DEFAULT_INSTANCE.toBuilder();\n"
788 "public static Builder newBuilder($classname$ prototype) {\n"
789 " return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);\n"
791 "@java.lang.Override\n"
792 "public Builder toBuilder() {\n"
793 " return this == DEFAULT_INSTANCE\n"
794 " ? new Builder() : new Builder().mergeFrom(this);\n"
800 "@java.lang.Override\n"
801 "protected Builder newBuilderForType(\n"
802 " com.google.protobuf.GeneratedMessage$ver$.BuilderParent parent) {\n"
803 " Builder builder = new Builder(parent);\n"
809 builderGenerator.Generate(printer);
816 "public static final com.google.protobuf.Descriptors.Descriptor\n"
817 " getDescriptor() {\n"
818 " return $fileclass$.internal_$identifier$_descriptor;\n"
824 std::vector<const FieldDescriptor*> map_fields;
829 map_fields.push_back(
field);
832 if (!map_fields.empty()) {
834 "@SuppressWarnings({\"rawtypes\"})\n"
835 "@java.lang.Override\n"
836 "protected com.google.protobuf.MapField internalGetMapField(\n"
838 " switch (number) {\n");
841 for (
int i = 0;
i < map_fields.size(); ++
i) {
846 " return internalGet$capitalized_name$();\n",
847 "number",
StrCat(
field->number()),
"capitalized_name",
848 info->capitalized_name);
852 " throw new RuntimeException(\n"
853 " \"Invalid map field number: \" + number);\n");
861 "@java.lang.Override\n"
862 "protected com.google.protobuf.GeneratedMessage$ver$.FieldAccessorTable\n"
863 " internalGetFieldAccessorTable() {\n"
864 " return $fileclass$.internal_$identifier$_fieldAccessorTable\n"
865 " .ensureFieldAccessorsInitialized(\n"
866 " $classname$.class, $classname$.Builder.class);\n"
881 printer->Print(
"private byte memoizedIsInitialized = -1;\n");
883 "@java.lang.Override\n"
884 "public final boolean isInitialized() {\n");
889 "byte isInitialized = memoizedIsInitialized;\n"
890 "if (isInitialized == 1) return true;\n"
891 "if (isInitialized == 0) return false;\n"
901 if (
field->is_required()) {
903 "if (!has$name$()) {\n"
904 " memoizedIsInitialized = 0;\n"
907 "name", info->capitalized_name);
917 switch (
field->label()) {
920 "if (!get$name$().isInitialized()) {\n"
921 " memoizedIsInitialized = 0;\n"
926 "name", info->capitalized_name);
930 "if (has$name$()) {\n"
931 " if (!get$name$().isInitialized()) {\n"
932 " memoizedIsInitialized = 0;\n"
936 "name", info->capitalized_name);
941 "for ($type$ item : get$name$Map().values()) {\n"
942 " if (!item.isInitialized()) {\n"
943 " memoizedIsInitialized = 0;\n"
948 MapValueImmutableClassdName(
field->message_type(),
950 "name", info->capitalized_name);
953 "for (int i = 0; i < get$name$Count(); i++) {\n"
954 " if (!get$name$(i).isInitialized()) {\n"
955 " memoizedIsInitialized = 0;\n"
961 "name", info->capitalized_name);
970 "if (!extensionsAreInitialized()) {\n"
971 " memoizedIsInitialized = 0;\n"
978 printer->Print(
" memoizedIsInitialized = 1;\n");
990 if (
field->is_repeated()) {
1003 "@java.lang.Override\n"
1004 "public boolean equals(");
1005 printer->Print(
"final java.lang.Object obj) {\n");
1008 "if (obj == this) {\n"
1011 "if (!(obj instanceof $classname$)) {\n"
1012 " return super.equals(obj);\n"
1014 "$classname$ other = ($classname$) obj;\n"
1022 bool check_has_bits = CheckHasBitsForEqualsAndHashCode(
field);
1023 if (check_has_bits) {
1025 "if (has$name$() != other.has$name$()) return false;\n"
1026 "if (has$name$()) {\n",
1027 "name", info->capitalized_name);
1031 if (check_has_bits) {
1033 printer->Print(
"}\n");
1041 "if (!get$oneof_capitalized_name$Case().equals("
1042 "other.get$oneof_capitalized_name$Case())) return false;\n",
1043 "oneof_capitalized_name",
1045 printer->Print(
"switch ($oneof_name$Case_) {\n",
"oneof_name",
1048 for (
int j = 0;
j < (oneof)->field_count();
j++) {
1050 printer->Print(
"case $field_number$:\n",
"field_number",
1054 printer->Print(
"break;\n");
1061 printer->Print(
"}\n");
1068 "if (!unknownFields.equals(other.unknownFields)) return false;\n");
1071 "if (!getExtensionFields().equals(other.getExtensionFields()))\n"
1072 " return false;\n");
1074 printer->Print(
"return true;\n");
1081 "@java.lang.Override\n"
1082 "public int hashCode() {\n");
1084 printer->Print(
"if (memoizedHashCode != 0) {\n");
1086 printer->Print(
"return memoizedHashCode;\n");
1090 "int hash = 41;\n");
1094 printer->Print(
"hash = (19 * hash) + getDescriptorForType().hashCode();\n");
1096 printer->Print(
"hash = (19 * hash) + getDescriptor().hashCode();\n");
1104 bool check_has_bits = CheckHasBitsForEqualsAndHashCode(
field);
1105 if (check_has_bits) {
1106 printer->Print(
"if (has$name$()) {\n",
"name", info->capitalized_name);
1110 if (check_has_bits) {
1112 printer->Print(
"}\n");
1119 printer->Print(
"switch ($oneof_name$Case_) {\n",
"oneof_name",
1122 for (
int j = 0;
j < (oneof)->field_count();
j++) {
1124 printer->Print(
"case $field_number$:\n",
"field_number",
1128 printer->Print(
"break;\n");
1135 printer->Print(
"}\n");
1139 printer->Print(
"hash = hashFields(hash, getExtensionFields());\n");
1142 printer->Print(
"hash = (29 * hash) + unknownFields.hashCode();\n");
1144 "memoizedHashCode = hash;\n"
1158 .GenerateRegistrationCode(printer);
1163 .GenerateExtensionRegistrationCode(printer);
1170 std::unique_ptr<const FieldDescriptor*[]> sorted_fields(
1174 "private $classname$(\n"
1175 " com.google.protobuf.CodedInputStream input,\n"
1176 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
1177 " throws com.google.protobuf.InvalidProtocolBufferException {\n",
1184 "if (extensionRegistry == null) {\n"
1185 " throw new java.lang.NullPointerException();\n"
1189 int totalBuilderBits = 0;
1191 const ImmutableFieldGenerator&
field =
1193 totalBuilderBits +=
field.GetNumBitsForBuilder();
1195 int totalBuilderInts = (totalBuilderBits + 31) / 32;
1196 for (
int i = 0;
i < totalBuilderInts;
i++) {
1197 printer->Print(
"int mutable_$bit_field_name$ = 0;\n",
"bit_field_name",
1202 "com.google.protobuf.UnknownFieldSet.Builder unknownFields =\n"
1203 " com.google.protobuf.UnknownFieldSet.newBuilder();\n");
1205 printer->Print(
"try {\n");
1209 "boolean done = false;\n"
1210 "while (!done) {\n");
1214 "int tag = input.readTag();\n"
1215 "switch (tag) {\n");
1228 printer->Print(
"case $tag$: {\n",
"tag",
1239 if (
field->is_packable()) {
1244 printer->Print(
"case $tag$: {\n",
"tag",
1259 " if (!parseUnknownField(\n"
1260 " input, unknownFields, extensionRegistry, tag)) {\n"
1274 "} catch (com.google.protobuf.InvalidProtocolBufferException e) {\n"
1275 " throw e.setUnfinishedMessage(this);\n"
1276 "} catch (java.io.IOException e) {\n"
1277 " throw new com.google.protobuf.InvalidProtocolBufferException(\n"
1278 " e).setUnfinishedMessage(this);\n"
1289 printer->Print(
"this.unknownFields = unknownFields.build();\n");
1292 printer->Print(
"makeExtensionsImmutable();\n");
1304 "$visibility$ static final com.google.protobuf.Parser<$classname$>\n"
1305 " PARSER = new com.google.protobuf.AbstractParser<$classname$>() {\n",
1312 "@java.lang.Override\n"
1313 "public $classname$ parsePartialFrom(\n"
1314 " com.google.protobuf.CodedInputStream input,\n"
1315 " com.google.protobuf.ExtensionRegistryLite extensionRegistry)\n"
1316 " throws com.google.protobuf.InvalidProtocolBufferException {\n",
1319 printer->Print(
" return new $classname$(input, extensionRegistry);\n",
1327 "Builder builder = newBuilder();\n"
1329 " builder.mergeFrom(input, extensionRegistry);\n"
1330 "} catch (com.google.protobuf.InvalidProtocolBufferException e) {\n"
1331 " throw e.setUnfinishedMessage(builder.buildPartial());\n"
1332 "} catch (java.io.IOException e) {\n"
1333 " throw new com.google.protobuf.InvalidProtocolBufferException(\n"
1334 " e.getMessage()).setUnfinishedMessage(\n"
1335 " builder.buildPartial());\n"
1337 "return builder.buildPartial();\n");
1340 printer->Print(
"}\n");
1347 "public static com.google.protobuf.Parser<$classname$> parser() {\n"
1351 "@java.lang.Override\n"
1352 "public com.google.protobuf.Parser<$classname$> getParserForType() {\n"
1364 .GenerateInitializationCode(printer);
1372 "protected com.google.protobuf.MutableMessage\n"
1373 " internalMutableDefault() {\n"
1374 " return MutableDefaultLoader.get();\n"
1377 "private static final class MutableDefaultLoader {\n"
1378 " private static final java.lang.Object defaultOrRuntimeException;\n"
1380 " java.lang.Object local;\n"
1382 " local = internalMutableDefault(\"$mutable_name$\");\n"
1383 " } catch (java.lang.RuntimeException e) {\n"
1386 " defaultOrRuntimeException = local;\n"
1389 " private MutableDefaultLoader() {}\n"
1391 " public static com.google.protobuf.MutableMessage get() {\n"
1392 " if (defaultOrRuntimeException\n"
1393 " instanceof java.lang.RuntimeException) {\n"
1394 " throw (java.lang.RuntimeException) defaultOrRuntimeException;\n"
1397 " (com.google.protobuf.MutableMessage) "
1398 "defaultOrRuntimeException;\n"
1407 "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n"
1408 "@com.google.protobuf.kotlin.ProtoDslMarker\n");
1410 "public class Dsl private constructor(\n"
1411 " private val _builder: $message$.Builder\n"
1413 " public companion object {\n"
1414 " @kotlin.jvm.JvmSynthetic\n"
1415 " @kotlin.PublishedApi\n"
1416 " internal fun _create(builder: $message$.Builder): Dsl = "
1420 " @kotlin.jvm.JvmSynthetic\n"
1421 " @kotlin.PublishedApi\n"
1422 " internal fun _build(): $message$ = _builder.build()\n",
1428 printer->
Print(
"\n");
1430 .GenerateKotlinDslMembers(printer);
1435 "public val $oneof_name$Case: $message$.$oneof_capitalized_name$Case\n"
1436 " @JvmName(\"get$oneof_capitalized_name$Case\")\n"
1437 " get() = _builder.get$oneof_capitalized_name$Case()\n\n"
1438 "public fun clear$oneof_capitalized_name$() {\n"
1439 " _builder.clear$oneof_capitalized_name$()\n"
1442 "oneof_capitalized_name",
1452 printer->
Print(
"}\n");
1458 "@kotlin.jvm.JvmSynthetic\n"
1459 "public inline fun $camelcase_name$(block: $message_kt$.Dsl.() -> "
1463 " $message_kt$.Dsl._create($message$.newBuilder()).apply { block() "
1475 .GenerateKotlinMembers(printer);
1478 printer->
Print(
"}\n");
1484 "@kotlin.jvm.JvmSynthetic\n"
1485 "public inline fun $message$.copy(block: $message_kt$.Dsl.() -> "
1488 " $message_kt$.Dsl._create(this.toBuilder()).apply { block() "
1496 .GenerateTopLevelKotlinMembers(printer);
1505 "@Suppress(\"UNCHECKED_CAST\")\n"
1506 "@kotlin.jvm.JvmSynthetic\n"
1507 "public operator fun <T> get(extension: "
1508 "com.google.protobuf.ExtensionLite<$message$, T>): T {\n"
1509 " return if (extension.isRepeated) {\n"
1510 " get(extension as com.google.protobuf.ExtensionLite<$message$, "
1513 " _builder.getExtension(extension)\n"
1516 "message", message_name);
1519 "@kotlin.jvm.JvmSynthetic\n"
1521 "(com.google.protobuf.kotlin.OnlyForUseByGeneratedProtoCode::class)\n"
1522 "@kotlin.jvm.JvmName(\"-getRepeatedExtension\")\n"
1523 "public operator fun <E> get(\n"
1524 " extension: com.google.protobuf.ExtensionLite<$message$, List<E>>\n"
1525 "): com.google.protobuf.kotlin.ExtensionList<E, $message$> {\n"
1526 " return com.google.protobuf.kotlin.ExtensionList(extension, "
1527 "_builder.getExtension(extension))\n"
1529 "message", message_name);
1532 "@kotlin.jvm.JvmSynthetic\n"
1533 "public operator fun contains(extension: "
1534 "com.google.protobuf.ExtensionLite<$message$, *>): "
1536 " return _builder.hasExtension(extension)\n"
1538 "message", message_name);
1541 "@kotlin.jvm.JvmSynthetic\n"
1542 "public fun clear(extension: "
1543 "com.google.protobuf.ExtensionLite<$message$, *>) "
1545 " _builder.clearExtension(extension)\n"
1547 "message", message_name);
1550 "@kotlin.jvm.JvmSynthetic\n"
1551 "@kotlin.PublishedApi\n"
1552 "internal fun <T> setExtension(extension: "
1553 "com.google.protobuf.ExtensionLite<$message$, T>, "
1555 " _builder.setExtension(extension, value)\n"
1557 "message", message_name);
1560 "@kotlin.jvm.JvmSynthetic\n"
1561 "@Suppress(\"NOTHING_TO_INLINE\")\n"
1562 "public inline operator fun <T : Comparable<T>> set(\n"
1563 " extension: com.google.protobuf.ExtensionLite<$message$, T>,\n"
1566 " setExtension(extension, value)\n"
1568 "message", message_name);
1571 "@kotlin.jvm.JvmSynthetic\n"
1572 "@Suppress(\"NOTHING_TO_INLINE\")\n"
1573 "public inline operator fun set(\n"
1574 " extension: com.google.protobuf.ExtensionLite<$message$, "
1575 "com.google.protobuf.ByteString>,\n"
1576 " value: com.google.protobuf.ByteString\n"
1578 " setExtension(extension, value)\n"
1580 "message", message_name);
1583 "@kotlin.jvm.JvmSynthetic\n"
1584 "@Suppress(\"NOTHING_TO_INLINE\")\n"
1585 "public inline operator fun <T : com.google.protobuf.MessageLite> set(\n"
1586 " extension: com.google.protobuf.ExtensionLite<$message$, T>,\n"
1589 " setExtension(extension, value)\n"
1591 "message", message_name);
1594 "@kotlin.jvm.JvmSynthetic\n"
1595 "public fun <E> com.google.protobuf.kotlin.ExtensionList<E, "
1596 "$message$>.add(value: E) {\n"
1597 " _builder.addExtension(this.extension, value)\n"
1599 "message", message_name);
1602 "@kotlin.jvm.JvmSynthetic\n"
1603 "@Suppress(\"NOTHING_TO_INLINE\")\n"
1604 "public inline operator fun <E> "
1605 "com.google.protobuf.kotlin.ExtensionList<E, "
1606 "$message$>.plusAssign"
1610 "message", message_name);
1613 "@kotlin.jvm.JvmSynthetic\n"
1614 "public fun <E> com.google.protobuf.kotlin.ExtensionList<E, "
1615 "$message$>.addAll(values: Iterable<E>) {\n"
1616 " for (value in values) {\n"
1620 "message", message_name);
1623 "@kotlin.jvm.JvmSynthetic\n"
1624 "@Suppress(\"NOTHING_TO_INLINE\")\n"
1625 "public inline operator fun <E> "
1626 "com.google.protobuf.kotlin.ExtensionList<E, "
1627 "$message$>.plusAssign(values: "
1631 "message", message_name);
1634 "@kotlin.jvm.JvmSynthetic\n"
1635 "public operator fun <E> com.google.protobuf.kotlin.ExtensionList<E, "
1636 "$message$>.set(index: Int, value: "
1638 " _builder.setExtension(this.extension, index, value)\n"
1640 "message", message_name);
1643 "@kotlin.jvm.JvmSynthetic\n"
1644 "@Suppress(\"NOTHING_TO_INLINE\")\n"
1645 "public inline fun com.google.protobuf.kotlin.ExtensionList<*, "
1646 "$message$>.clear() {\n"
1647 " clear(extension)\n"
1649 "message", message_name);
1654 "private static String getTypeUrl(\n"
1655 " java.lang.String typeUrlPrefix,\n"
1656 " com.google.protobuf.Descriptors.Descriptor descriptor) {\n"
1657 " return typeUrlPrefix.endsWith(\"/\")\n"
1658 " ? typeUrlPrefix + descriptor.getFullName()\n"
1659 " : typeUrlPrefix + \"/\" + descriptor.getFullName();\n"
1662 "private static String getTypeNameFromTypeUrl(\n"
1663 " java.lang.String typeUrl) {\n"
1664 " int pos = typeUrl.lastIndexOf('/');\n"
1665 " return pos == -1 ? \"\" : typeUrl.substring(pos + 1);\n"
1668 "public static <T extends com.google.protobuf.Message> Any pack(\n"
1670 " return Any.newBuilder()\n"
1671 " .setTypeUrl(getTypeUrl(\"type.googleapis.com\",\n"
1672 " message.getDescriptorForType()))\n"
1673 " .setValue(message.toByteString())\n"
1678 " * Packs a message using the given type URL prefix. The type URL will\n"
1679 " * be constructed by concatenating the message type's full name to the\n"
1680 " * prefix with an optional \"/\" separator if the prefix doesn't end\n"
1681 " * with \"/\" already.\n"
1683 "public static <T extends com.google.protobuf.Message> Any pack(\n"
1684 " T message, java.lang.String typeUrlPrefix) {\n"
1685 " return Any.newBuilder()\n"
1686 " .setTypeUrl(getTypeUrl(typeUrlPrefix,\n"
1687 " message.getDescriptorForType()))\n"
1688 " .setValue(message.toByteString())\n"
1692 "public <T extends com.google.protobuf.Message> boolean is(\n"
1693 " java.lang.Class<T> clazz) {\n"
1694 " T defaultInstance =\n"
1695 " com.google.protobuf.Internal.getDefaultInstance(clazz);\n"
1696 " return getTypeNameFromTypeUrl(getTypeUrl()).equals(\n"
1697 " defaultInstance.getDescriptorForType().getFullName());\n"
1700 "private volatile com.google.protobuf.Message cachedUnpackValue;\n"
1702 "@java.lang.SuppressWarnings(\"unchecked\")\n"
1703 "public <T extends com.google.protobuf.Message> T unpack(\n"
1704 " java.lang.Class<T> clazz)\n"
1705 " throws com.google.protobuf.InvalidProtocolBufferException {\n"
1706 " boolean invalidClazz = false;\n"
1707 " if (cachedUnpackValue != null) {\n"
1708 " if (cachedUnpackValue.getClass() == clazz) {\n"
1709 " return (T) cachedUnpackValue;\n"
1711 " invalidClazz = true;\n"
1713 " if (invalidClazz || !is(clazz)) {\n"
1714 " throw new com.google.protobuf.InvalidProtocolBufferException(\n"
1715 " \"Type of the Any message does not match the given class.\");\n"
1717 " T defaultInstance =\n"
1718 " com.google.protobuf.Internal.getDefaultInstance(clazz);\n"
1719 " T result = (T) defaultInstance.getParserForType()\n"
1720 " .parseFrom(getValue());\n"
1721 " cachedUnpackValue = result;\n"
const std::string & full_name() const
void GenerateEqualsAndHashCode(io::Printer *printer)
void GenerateMessageSerializationMethods(io::Printer *printer)
void Print(const std::map< std::string, std::string > &variables, const char *text)
void MaybePrintGeneratedAnnotation(Context *context, io::Printer *printer, Descriptor *descriptor, bool immutable, const std::string &suffix="")
void GenerateTopLevelKotlinMembers(io::Printer *printer) const override
virtual void GenerateInterface(io::Printer *printer)
bool HasDescriptorMethods(const Descriptor *descriptor, bool enforce_lite)
void GenerateIsInitialized(io::Printer *printer)
bool IsMapEntry(const Descriptor *descriptor)
void GenerateKotlinMembers(io::Printer *printer) const override
#define GOOGLE_CHECK_EQ(A, B)
int nested_type_count() const
void GenerateParseFromMethods(io::Printer *printer)
const Descriptor * containing_type() const
bool MultipleJavaFiles(const FileDescriptor *descriptor, bool immutable)
GRPC_CUSTOM_PRINTER Printer
void GenerateSerializeOneExtensionRange(io::Printer *printer, const Descriptor::ExtensionRange *range)
std::string GetImmutableClassName(const DescriptorType *descriptor)
ImmutableMessageGenerator(const Descriptor *descriptor, Context *context)
string StrCat(const AlphaNum &a, const AlphaNum &b)
MessageGenerator(const Descriptor *descriptor)
bool IsRealOneof(const FieldDescriptor *descriptor)
const MessageOptions & options() const
void GenerateParsingConstructor(io::Printer *printer)
bool HasHasbit(const FieldDescriptor *descriptor)
std::string ExtraMessageOrBuilderInterfaces(const Descriptor *descriptor)
const FieldDescriptor * field(int index) const
void WriteMessageDocComment(io::Printer *printer, const Descriptor *message)
int GenerateFieldAccessorTableInitializer(io::Printer *printer)
bool IsWrappersProtoFile(const FileDescriptor *descriptor)
const OneofDescriptor * containing_oneof
static void * tag(intptr_t t)
void GenerateMutableCopy(io::Printer *printer)
void GenerateFieldAccessorTable(io::Printer *printer, int *bytecode_estimate)
const ExtensionRange * extension_range(int index) const
bool ExposePublicParser(const FileDescriptor *descriptor)
virtual void GenerateStaticVariables(io::Printer *printer, int *bytecode_estimate)
const FieldGeneratorInfo * GetFieldGeneratorInfo(const FieldDescriptor *field) const
const OneofDescriptor * oneof_decl(int index) const
JavaType GetJavaType(const FieldDescriptor *field)
std::string ExtraMessageInterfaces(const Descriptor *descriptor)
int oneof_decl_count() const
int extension_count() const
const EnumDescriptor * enum_type(int index) const
virtual ~MessageGenerator()
void GenerateDescriptorMethods(io::Printer *printer)
std::string GetBitFieldName(int index)
void GenerateBuilder(io::Printer *printer)
string Substitute(const char *format, const SubstituteArg &arg0, const SubstituteArg &arg1, const SubstituteArg &arg2, const SubstituteArg &arg3, const SubstituteArg &arg4, const SubstituteArg &arg5, const SubstituteArg &arg6, const SubstituteArg &arg7, const SubstituteArg &arg8, const SubstituteArg &arg9)
const FieldDescriptor * extension(int index) const
static const int kMaxStaticSize
int extension_range_count() const
void GenerateAnyMethods(io::Printer *printer)
std::set< const OneofDescriptor * > oneofs_
virtual ~ImmutableMessageGenerator()
ClassNameResolver * name_resolver_
bool HasGeneratedMethods(const Descriptor *descriptor) const
void GenerateKotlinDsl(io::Printer *printer) const override
const Descriptor * nested_type(int index) const
int enum_type_count() const
void GenerateSerializeOneField(io::Printer *printer, const FieldDescriptor *field)
std::string GetJavaMutableClassName(const Descriptor *descriptor)
const FieldDescriptor * field
void GenerateParser(io::Printer *printer)
std::string UniqueFileScopeIdentifier(const Descriptor *descriptor)
void GenerateKotlinExtensions(io::Printer *printer) const
FieldGeneratorMap< ImmutableFieldGenerator > field_generators_
const std::string & name() const
const FileDescriptor * file() const
#define GOOGLE_CHECK(EXPRESSION)
std::string GetKotlinFactoryName(const Descriptor *descriptor)
const Descriptor * descriptor_
string ToUpper(const string &s)
std::string FieldConstantName(const FieldDescriptor *field)
bool no_standard_descriptor_accessor() const
virtual int GenerateStaticVariableInitializers(io::Printer *printer)
grpc::ClientContext context
ClassNameResolver * GetNameResolver() const
bool HasRequiredFields(const Descriptor *type, std::unordered_set< const Descriptor * > *already_seen)
std::string GetClassName(const Descriptor *descriptor, bool immutable)
std::string capitalized_name
const OneofGeneratorInfo * GetOneofGeneratorInfo(const OneofDescriptor *oneof) const
bool IsOwnFile(const Descriptor *descriptor, bool immutable)
bool HasPackedFields(const Descriptor *descriptor)
std::string GetKotlinExtensionsClassName(const Descriptor *descriptor)
const char * PrimitiveTypeName(JavaType type)
const FieldDescriptor ** SortFieldsByNumber(const Descriptor *descriptor)
static const char descriptor[1336]
virtual void GenerateExtensionRegistrationCode(io::Printer *printer)
virtual void Generate(io::Printer *printer)
std::string GeneratedCodeVersionSuffix()
void GenerateInitializers(io::Printer *printer)
bool IsAnyMessage(const Descriptor *descriptor)
bool message_set_wire_format() const
grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:09