File indexing completed on 2026-10-04 09:09:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
0013 #define GOOGLE_PROTOBUF_COMPILER_JAVA_HELPERS_H__
0014
0015 #include <cstdint>
0016 #include <string>
0017
0018 #include "absl/status/status.h"
0019 #include "absl/strings/string_view.h"
0020 #include "google/protobuf/compiler/java/names.h"
0021 #include "google/protobuf/compiler/java/options.h"
0022 #include "google/protobuf/descriptor.h"
0023 #include "google/protobuf/descriptor.pb.h"
0024 #include "google/protobuf/io/printer.h"
0025
0026
0027 #include "google/protobuf/port_def.inc"
0028
0029 namespace google {
0030 namespace protobuf {
0031 namespace compiler {
0032 namespace java {
0033
0034
0035
0036 extern const char kThickSeparator[];
0037 extern const char kThinSeparator[];
0038
0039 bool IsForbiddenKotlin(absl::string_view field_name);
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 void PrintGeneratedAnnotation(io::Printer* printer, char delimiter = '$',
0050 absl::string_view annotation_file = "",
0051 Options options = {});
0052
0053
0054
0055 void PrintEnumVerifierLogic(
0056 io::Printer* printer, const FieldDescriptor* descriptor,
0057 const absl::flat_hash_map<absl::string_view, std::string>& variables,
0058 absl::string_view var_name, absl::string_view terminating_string,
0059 bool enforce_lite);
0060
0061
0062
0063 void PrintGencodeVersionValidator(io::Printer* printer, bool oss_runtime,
0064 absl::string_view java_class_name);
0065
0066
0067
0068 std::string ToCamelCase(absl::string_view input, bool lower_first);
0069
0070
0071
0072 std::string CamelCaseFieldName(const FieldDescriptor* field);
0073
0074
0075
0076
0077 std::string UniqueFileScopeIdentifier(const Descriptor* descriptor);
0078
0079
0080
0081
0082 std::string FileClassName(const FileDescriptor* file, bool immutable);
0083
0084
0085 std::string FileJavaPackage(const FileDescriptor* file, bool immutable,
0086 Options options = {});
0087
0088
0089 std::string JavaPackageToDir(std::string package_name);
0090
0091
0092 std::string EscapeKotlinKeywords(std::string name);
0093
0094
0095
0096 std::string ExtraMessageInterfaces(const Descriptor* descriptor);
0097
0098
0099
0100 std::string ExtraMutableMessageInterfaces(const Descriptor* descriptor);
0101
0102
0103 std::string ExtraBuilderInterfaces(const Descriptor* descriptor);
0104
0105
0106
0107 std::string ExtraMessageOrBuilderInterfaces(const Descriptor* descriptor);
0108
0109
0110
0111 inline std::string ShortMutableJavaClassName(const Descriptor* descriptor) {
0112 return std::string(descriptor->name());
0113 }
0114
0115
0116
0117
0118 inline bool IsDescriptorProto(const Descriptor* descriptor) {
0119 return descriptor->file()->name() == "net/proto2/proto/descriptor.proto" ||
0120 descriptor->file()->name() == "google/protobuf/descriptor.proto";
0121 }
0122
0123
0124
0125 std::string GetOneofStoredType(const FieldDescriptor* field);
0126
0127
0128
0129 enum class Proto1EnumRepresentation {
0130 kEnum,
0131 kInteger,
0132 };
0133
0134
0135 inline Proto1EnumRepresentation GetProto1EnumRepresentation(
0136 const EnumDescriptor* descriptor) {
0137 if (descriptor->containing_type() != nullptr) {
0138 return Proto1EnumRepresentation::kEnum;
0139 }
0140 return Proto1EnumRepresentation::kInteger;
0141 }
0142
0143 absl::Status ValidateNestInFileClassFeature(const Descriptor& descriptor);
0144 absl::Status ValidateNestInFileClassFeature(const EnumDescriptor& descriptor);
0145
0146
0147
0148
0149 bool NestedInFileClass(const Descriptor& descriptor, bool immutable);
0150 bool NestedInFileClass(const EnumDescriptor& descriptor, bool immutable);
0151 bool NestedInFileClass(const ServiceDescriptor& descriptor, bool immutable);
0152
0153
0154
0155
0156
0157
0158
0159 template <typename Descriptor>
0160 bool IsOwnFile(const Descriptor* descriptor, bool immutable) {
0161 return descriptor->containing_type() == nullptr &&
0162 !NestedInFileClass(*descriptor, immutable);
0163 }
0164
0165 template <>
0166 inline bool IsOwnFile(const ServiceDescriptor* descriptor, bool immutable) {
0167 return !NestedInFileClass(*descriptor, immutable);
0168 }
0169
0170
0171
0172
0173
0174 template <typename Descriptor>
0175 std::string AnnotationFileName(const Descriptor* descriptor,
0176 absl::string_view suffix) {
0177 return absl::StrCat(descriptor->name(), suffix, ".java.pb.meta");
0178 }
0179
0180
0181
0182 std::string FieldConstantName(const FieldDescriptor* field);
0183
0184
0185
0186
0187 FieldDescriptor::Type GetType(const FieldDescriptor* field);
0188
0189 enum JavaType {
0190 JAVATYPE_INT,
0191 JAVATYPE_LONG,
0192 JAVATYPE_FLOAT,
0193 JAVATYPE_DOUBLE,
0194 JAVATYPE_BOOLEAN,
0195 JAVATYPE_STRING,
0196 JAVATYPE_BYTES,
0197 JAVATYPE_ENUM,
0198 JAVATYPE_MESSAGE
0199 };
0200
0201 JavaType GetJavaType(const FieldDescriptor* field);
0202
0203 absl::string_view PrimitiveTypeName(JavaType type);
0204
0205
0206
0207
0208 absl::string_view BoxedPrimitiveTypeName(JavaType type);
0209
0210
0211
0212 absl::string_view KotlinTypeName(JavaType type);
0213
0214
0215
0216
0217 absl::string_view FieldTypeName(FieldDescriptor::Type field_type);
0218
0219 class ClassNameResolver;
0220 std::string DefaultValue(const FieldDescriptor* field, bool immutable,
0221 ClassNameResolver* name_resolver,
0222 Options options = {});
0223 inline std::string ImmutableDefaultValue(const FieldDescriptor* field,
0224 ClassNameResolver* name_resolver,
0225 Options options = {}) {
0226 return DefaultValue(field, true, name_resolver, options);
0227 }
0228 bool IsDefaultValueJavaDefault(const FieldDescriptor* field);
0229 bool IsByteStringWithCustomDefaultValue(const FieldDescriptor* field);
0230
0231
0232 inline bool HasDescriptorMethods(const Descriptor* ,
0233 bool enforce_lite) {
0234 return !enforce_lite;
0235 }
0236 inline bool HasDescriptorMethods(const EnumDescriptor* ,
0237 bool enforce_lite) {
0238 return !enforce_lite;
0239 }
0240 inline bool HasDescriptorMethods(const FileDescriptor* ,
0241 bool enforce_lite) {
0242 return !enforce_lite;
0243 }
0244
0245
0246 inline bool HasGenericServices(const FileDescriptor* file, bool enforce_lite) {
0247 return file->service_count() > 0 &&
0248 HasDescriptorMethods(file, enforce_lite) &&
0249 file->options().java_generic_services();
0250 }
0251
0252
0253
0254
0255 std::string GetBitFieldName(int index);
0256
0257
0258
0259 std::string GetBitFieldNameForBit(int bitIndex);
0260
0261
0262
0263
0264 std::string GenerateGetBit(int bitIndex);
0265
0266
0267
0268
0269 std::string GenerateSetBit(int bitIndex);
0270
0271
0272
0273
0274 std::string GenerateClearBit(int bitIndex);
0275
0276
0277
0278
0279
0280 std::string GenerateGetBitFromLocal(int bitIndex);
0281
0282
0283
0284
0285
0286 std::string GenerateSetBitToLocal(int bitIndex);
0287
0288
0289
0290
0291
0292 std::string GenerateGetBitMutableLocal(int bitIndex);
0293
0294
0295
0296
0297
0298 std::string GenerateSetBitMutableLocal(int bitIndex);
0299
0300
0301 bool IsReferenceType(JavaType type);
0302
0303
0304
0305 absl::string_view GetCapitalizedType(const FieldDescriptor* field,
0306 bool immutable, Options options);
0307
0308
0309
0310 int FixedSize(FieldDescriptor::Type type);
0311
0312
0313 struct FieldOrderingByNumber {
0314 inline bool operator()(const FieldDescriptor* a,
0315 const FieldDescriptor* b) const {
0316 return a->number() < b->number();
0317 }
0318 };
0319
0320 struct ExtensionRangeOrdering {
0321 bool operator()(const Descriptor::ExtensionRange* a,
0322 const Descriptor::ExtensionRange* b) const {
0323 return a->start_number() < b->start_number();
0324 }
0325 };
0326
0327
0328
0329 const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor);
0330
0331
0332 inline bool HasPackedFields(const Descriptor* descriptor) {
0333 for (int i = 0; i < descriptor->field_count(); i++) {
0334 if (descriptor->field(i)->is_packed()) {
0335 return true;
0336 }
0337 }
0338 return false;
0339 }
0340
0341
0342
0343 bool HasRequiredFields(const Descriptor* descriptor);
0344
0345 bool IsRealOneof(const FieldDescriptor* descriptor);
0346
0347 inline bool HasHasbit(const FieldDescriptor* descriptor) {
0348 return descriptor->has_presence() && !descriptor->real_containing_oneof();
0349 }
0350
0351
0352 bool HasRepeatedFields(const Descriptor* descriptor);
0353
0354 inline bool IsMapEntry(const Descriptor* descriptor) {
0355 return descriptor->options().map_entry();
0356 }
0357
0358 inline bool IsMapField(const FieldDescriptor* descriptor) {
0359 return descriptor->is_map();
0360 }
0361
0362 inline bool IsAnyMessage(const Descriptor* descriptor) {
0363 return descriptor->full_name() == "google.protobuf.Any";
0364 }
0365
0366 inline bool IsWrappersProtoFile(const FileDescriptor* descriptor) {
0367 return descriptor->name() == "google/protobuf/wrappers.proto";
0368 }
0369
0370 void WriteUInt32ToUtf16CharSequence(uint32_t number,
0371 std::vector<uint16_t>* output);
0372
0373 inline void WriteIntToUtf16CharSequence(int value,
0374 std::vector<uint16_t>* output) {
0375 WriteUInt32ToUtf16CharSequence(static_cast<uint32_t>(value), output);
0376 }
0377
0378
0379 void EscapeUtf16ToString(uint16_t code, std::string* output);
0380
0381
0382
0383 std::pair<int, int> GetTableDrivenNumberOfEntriesAndLookUpStartFieldNumber(
0384 const FieldDescriptor** fields, int count);
0385
0386 const FieldDescriptor* MapKeyField(const FieldDescriptor* descriptor);
0387
0388 const FieldDescriptor* MapValueField(const FieldDescriptor* descriptor);
0389
0390 inline std::string JvmSynthetic(bool jvm_dsl) {
0391 return jvm_dsl ? "@kotlin.jvm.JvmSynthetic\n" : "";
0392 }
0393
0394 struct JvmNameContext {
0395 const Options& options;
0396 io::Printer* printer;
0397 bool lite = true;
0398 };
0399
0400 inline void JvmName(absl::string_view name, const JvmNameContext& context) {
0401 if (context.lite && !context.options.jvm_dsl) return;
0402 context.printer->Emit("@kotlin.jvm.JvmName(\"");
0403
0404 context.printer->Emit(name);
0405 context.printer->Emit("\")\n");
0406 }
0407
0408 }
0409 }
0410 }
0411 }
0412
0413 #include "google/protobuf/port_undef.inc"
0414 #endif