35 #include <google/protobuf/compiler/java/java_helpers.h>
40 #include <unordered_set>
43 #include <google/protobuf/stubs/stringprintf.h>
44 #include <google/protobuf/compiler/java/java_name_resolver.h>
45 #include <google/protobuf/compiler/java/java_names.h>
46 #include <google/protobuf/descriptor.pb.h>
47 #include <google/protobuf/wire_format.h>
48 #include <google/protobuf/stubs/strutil.h>
49 #include <google/protobuf/stubs/substitute.h>
50 #include <google/protobuf/stubs/hash.h>
57 using internal::WireFormat;
58 using internal::WireFormatLite;
61 "// ===================================================================\n";
63 "// -------------------------------------------------------------------\n";
67 const char* kDefaultPackage =
"";
72 const char* kForbiddenWordList[] = {
81 new std::unordered_set<std::string>({
82 "abstract",
"assert",
"boolean",
"break",
"byte",
83 "case",
"catch",
"char",
"class",
"const",
84 "continue",
"default",
"do",
"double",
"else",
85 "enum",
"extends",
"final",
"finally",
"float",
86 "for",
"goto",
"if",
"implements",
"import",
87 "instanceof",
"int",
"interface",
"long",
"native",
88 "new",
"package",
"private",
"protected",
"public",
89 "return",
"short",
"static",
"strictfp",
"super",
90 "switch",
"synchronized",
"this",
"throw",
"throws",
91 "transient",
"try",
"void",
"volatile",
"while",
96 if (field_name == kForbiddenWordList[i]) {
109 field_name =
field->message_type()->name();
111 field_name =
field->name();
113 if (IsForbidden(field_name)) {
126 if (annotation_file.empty()) {
130 "@javax.annotation.Generated(value=\"protoc\", comments=\"annotations:";
131 ptemplate.push_back(delimiter);
132 ptemplate.append(
"annotation_file");
133 ptemplate.push_back(delimiter);
134 ptemplate.append(
"\")\n");
135 printer->Print(ptemplate.c_str(),
"annotation_file", annotation_file);
140 const std::map<std::string, std::string>& variables,
141 const char* var_name,
142 const char* terminating_string,
bool enforce_lite) {
144 enforce_lite ?
StrCat(var_name,
".internalGetVerifier()")
146 "new com.google.protobuf.Internal.EnumVerifier() {\n"
147 " @java.lang.Override\n"
148 " public boolean isInRange(int number) {\n"
151 ".forNumber(number) != null;\n"
156 StrCat(enum_verifier_string, terminating_string).
c_str());
160 bool cap_next_letter) {
164 for (
int i = 0;
i <
input.size();
i++) {
166 if (cap_next_letter) {
171 cap_next_letter =
false;
172 }
else if (
'A' <=
input[i] &&
input[i] <=
'Z') {
173 if (i == 0 && !cap_next_letter) {
181 cap_next_letter =
false;
182 }
else if (
'0' <=
input[i] &&
input[i] <=
'9') {
184 cap_next_letter =
true;
186 cap_next_letter =
true;
197 bool capitalize_next = !lower_first;
203 capitalize_next =
true;
204 }
else if (capitalize_next) {
206 capitalize_next =
false;
213 if (lower_first && !
result.empty()) {
221 return (
ch >=
'a' &&
ch <=
'z') ? (
ch -
'a' +
'A') :
ch;
225 return (
ch >=
'A' &&
ch <=
'Z') ? (
ch -
'A' +
'a') :
ch;
255 const std::unordered_set<std::string>* kKotlinForbiddenNames =
256 new std::unordered_set<std::string>({
257 "as",
"as?",
"break",
"class",
"continue",
"do",
258 "else",
"false",
"for",
"fun",
"if",
"in",
259 "!in",
"interface",
"is",
"!is",
"null",
"object",
260 "package",
"return",
"super",
"this",
"throw",
"true",
261 "try",
"typealias",
"typeof",
"val",
"var",
"when",
264 return kKotlinForbiddenNames->find(field_name) !=
265 kKotlinForbiddenNames->end();
274 if (
'0' <= fieldName[0] && fieldName[0] <=
'9') {
275 return '_' + fieldName;
281 ClassNameResolver name_resolver;
282 return name_resolver.GetFileClassName(
file, immutable);
288 if (
file->options().has_java_package()) {
292 if (!
file->package().empty()) {
312 ClassNameResolver name_resolver;
313 return name_resolver.GetClassName(
descriptor,
true);
317 ClassNameResolver name_resolver;
318 return name_resolver.GetClassName(
descriptor,
true);
322 ClassNameResolver name_resolver;
323 return name_resolver.GetClassName(
descriptor,
true);
327 ClassNameResolver name_resolver;
328 return name_resolver.GetClassName(
descriptor,
true);
333 std::string interfaces =
"// @@protoc_insertion_point(message_implements:" +
340 std::string interfaces =
"// @@protoc_insertion_point(builder_implements:" +
346 std::string interfaces =
"// @@protoc_insertion_point(interface_extends:" +
358 return field->type();
420 return "java.lang.String";
422 return "com.google.protobuf.ByteString";
443 return "java.lang.Integer";
445 return "java.lang.Long";
447 return "java.lang.Float";
449 return "java.lang.Double";
451 return "java.lang.Boolean";
453 return "java.lang.String";
455 return "com.google.protobuf.ByteString";
478 return "kotlin.Long";
480 return "kotlin.Float";
482 return "kotlin.Double";
484 return "kotlin.Boolean";
486 return "kotlin.String";
488 return "com.google.protobuf.ByteString";
506 return "java.lang.Integer";
562 for (
int i = 0;
i <
text.size();
i++) {
563 if ((
text[i] & 0x80) != 0) {
571 ClassNameResolver* name_resolver) {
574 switch (
field->cpp_type()) {
587 if (
value == std::numeric_limits<double>::infinity()) {
588 return "Double.POSITIVE_INFINITY";
589 }
else if (
value == -std::numeric_limits<double>::infinity()) {
590 return "Double.NEGATIVE_INFINITY";
599 if (
value == std::numeric_limits<float>::infinity()) {
600 return "Float.POSITIVE_INFINITY";
601 }
else if (
value == -std::numeric_limits<float>::infinity()) {
602 return "Float.NEGATIVE_INFINITY";
610 return field->default_value_bool() ?
"true" :
"false";
613 if (
field->has_default_value()) {
616 "com.google.protobuf.Internal.bytesDefaultValue(\"$0\")",
619 return "com.google.protobuf.ByteString.EMPTY";
624 return "\"" +
CEscape(
field->default_value_string()) +
"\"";
628 "com.google.protobuf.Internal.stringDefaultValue(\"$0\")",
634 return name_resolver->GetClassName(
field->enum_type(), immutable) +
"." +
635 field->default_value_enum()->name();
638 return name_resolver->GetClassName(
field->message_type(), immutable) +
639 ".getDefaultInstance()";
652 switch (
field->cpp_type()) {
654 return field->default_value_int32() == 0;
656 return field->default_value_uint32() == 0;
658 return field->default_value_int64() == 0
L;
660 return field->default_value_uint64() == 0
L;
662 return field->default_value_double() == 0.0;
664 return field->default_value_float() == 0.0;
666 return field->default_value_bool() ==
false;
668 return field->default_value_enum()->number() == 0;
683 field->default_value_string() !=
"";
687 "0x00000001",
"0x00000002",
"0x00000004",
"0x00000008",
688 "0x00000010",
"0x00000020",
"0x00000040",
"0x00000080",
690 "0x00000100",
"0x00000200",
"0x00000400",
"0x00000800",
691 "0x00001000",
"0x00002000",
"0x00004000",
"0x00008000",
693 "0x00010000",
"0x00020000",
"0x00040000",
"0x00080000",
694 "0x00100000",
"0x00200000",
"0x00400000",
"0x00800000",
696 "0x01000000",
"0x02000000",
"0x04000000",
"0x08000000",
697 "0x10000000",
"0x20000000",
"0x40000000",
"0x80000000",
715 int bitInVarIndex = bitIndex % 32;
724 int bitInVarIndex = bitIndex % 32;
734 return GenerateGetBitInternal(
"", bitIndex);
738 return GenerateSetBitInternal(
"", bitIndex);
743 int bitInVarIndex = bitIndex % 32;
751 return GenerateGetBitInternal(
"from_", bitIndex);
755 return GenerateSetBitInternal(
"to_", bitIndex);
759 return GenerateGetBitInternal(
"mutable_", bitIndex);
763 return GenerateSetBitInternal(
"mutable_", bitIndex);
902 FieldOrderingByNumber());
912 std::unordered_set<const Descriptor*>* already_seen) {
913 if (already_seen->count(
type) > 0) {
924 already_seen->insert(
type);
929 if (
type->extension_range_count() > 0)
return true;
931 for (
int i = 0;
i <
type->field_count();
i++) {
933 if (
field->is_required()) {
947 std::unordered_set<const Descriptor*> already_seen;
954 if (
field->is_repeated()) {
976 std::vector<uint16_t>*
output) {
987 while (
number >= 0xD800) {
1031 static const int kMapFieldType = 50;
1032 static const int kOneofFieldTypeOffset = 51;
1033 static const int kRequiredBit = 0x100;
1034 static const int kUtf8CheckBit = 0x200;
1035 static const int kCheckInitialized = 0x400;
1036 static const int kMapWithProto2EnumValue = 0x800;
1037 static const int kHasHasBit = 0x1000;
1038 int extra_bits =
field->is_required() ? kRequiredBit : 0;
1040 extra_bits |= kUtf8CheckBit;
1044 extra_bits |= kCheckInitialized;
1047 extra_bits |= kHasHasBit;
1050 if (
field->is_map()) {
1053 field->message_type()->FindFieldByName(
"value");
1055 extra_bits |= kMapWithProto2EnumValue;
1058 return kMapFieldType | extra_bits;
1059 }
else if (
field->is_packed()) {
1061 }
else if (
field->is_repeated()) {
1065 kOneofFieldTypeOffset) |
1076 }
else if (
code ==
'\b') {
1078 }
else if (
code ==
'\n') {
1080 }
else if (
code ==
'\r') {
1082 }
else if (
code ==
'\f') {
1084 }
else if (
code ==
'\'') {
1086 }
else if (
code ==
'\"') {
1088 }
else if (
code ==
'\\') {
1090 }
else if (
code >= 0x20 &&
code <= 0x7f) {