1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "google/protobuf/compiler/java/lite/extension.h"
9
10 #include <string>
11
12 #include "absl/container/flat_hash_map.h"
13 #include "google/protobuf/compiler/java/context.h"
14 #include "google/protobuf/compiler/java/doc_comment.h"
15 #include "google/protobuf/compiler/java/helpers.h"
16 #include "google/protobuf/compiler/java/name_resolver.h"
17 #include "google/protobuf/io/printer.h"
18
19 // Must be last.
20 #include "google/protobuf/port_def.inc"
21
22 namespace google {
23 namespace protobuf {
24 namespace compiler {
25 namespace java {
26
ImmutableExtensionLiteGenerator(const FieldDescriptor * descriptor,Context * context)27 ImmutableExtensionLiteGenerator::ImmutableExtensionLiteGenerator(
28 const FieldDescriptor* descriptor, Context* context)
29 : descriptor_(descriptor),
30 name_resolver_(context->GetNameResolver()),
31 context_(context) {
32 if (descriptor_->extension_scope() != nullptr) {
33 scope_ =
34 name_resolver_->GetImmutableClassName(descriptor_->extension_scope());
35 } else {
36 scope_ = name_resolver_->GetImmutableClassName(descriptor_->file());
37 }
38 }
39
~ImmutableExtensionLiteGenerator()40 ImmutableExtensionLiteGenerator::~ImmutableExtensionLiteGenerator() {}
41
Generate(io::Printer * printer)42 void ImmutableExtensionLiteGenerator::Generate(io::Printer* printer) {
43 absl::flat_hash_map<absl::string_view, std::string> vars;
44 const bool kUseImmutableNames = true;
45 InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_,
46 &vars, context_);
47 printer->Print(vars, "public static final int $constant_name$ = $number$;\n");
48
49 WriteFieldDocComment(printer, descriptor_, context_->options());
50 if (descriptor_->is_repeated()) {
51 printer->Print(
52 vars,
53 "public static final\n"
54 " com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n"
55 " $containing_type$,\n"
56 " $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n"
57 " .newRepeatedGeneratedExtension(\n"
58 " $containing_type$.getDefaultInstance(),\n"
59 " $prototype$,\n"
60 " $enum_map$,\n"
61 " $number$,\n"
62 " com.google.protobuf.WireFormat.FieldType.$type_constant$,\n"
63 " $packed$,\n"
64 " $singular_type$.class);\n");
65 } else {
66 printer->Print(
67 vars,
68 "public static final\n"
69 " com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n"
70 " $containing_type$,\n"
71 " $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n"
72 " .newSingularGeneratedExtension(\n"
73 " $containing_type$.getDefaultInstance(),\n"
74 " $default$,\n"
75 " $prototype$,\n"
76 " $enum_map$,\n"
77 " $number$,\n"
78 " com.google.protobuf.WireFormat.FieldType.$type_constant$,\n"
79 " $singular_type$.class);\n");
80 }
81 printer->Annotate("name", descriptor_);
82 }
83
GenerateNonNestedInitializationCode(io::Printer * printer)84 int ImmutableExtensionLiteGenerator::GenerateNonNestedInitializationCode(
85 io::Printer* printer) {
86 return 0;
87 }
88
GenerateRegistrationCode(io::Printer * printer)89 int ImmutableExtensionLiteGenerator::GenerateRegistrationCode(
90 io::Printer* printer) {
91 printer->Print("registry.add($scope$.$name$);\n", "scope", scope_, "name",
92 UnderscoresToCamelCaseCheckReserved(descriptor_));
93 return 7;
94 }
95
96 } // namespace java
97 } // namespace compiler
98 } // namespace protobuf
99 } // namespace google
100
101 #include "google/protobuf/port_undef.inc"
102