1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #include <google/protobuf/compiler/java/java_extension_lite.h>
32
33 #include <google/protobuf/io/printer.h>
34 #include <google/protobuf/stubs/strutil.h>
35 #include <google/protobuf/compiler/java/java_context.h>
36 #include <google/protobuf/compiler/java/java_doc_comment.h>
37 #include <google/protobuf/compiler/java/java_helpers.h>
38 #include <google/protobuf/compiler/java/java_name_resolver.h>
39
40 namespace google {
41 namespace protobuf {
42 namespace compiler {
43 namespace java {
44
ImmutableExtensionLiteGenerator(const FieldDescriptor * descriptor,Context * context)45 ImmutableExtensionLiteGenerator::ImmutableExtensionLiteGenerator(
46 const FieldDescriptor* descriptor, Context* context)
47 : descriptor_(descriptor), name_resolver_(context->GetNameResolver()) {
48 if (descriptor_->extension_scope() != NULL) {
49 scope_ =
50 name_resolver_->GetImmutableClassName(descriptor_->extension_scope());
51 } else {
52 scope_ = name_resolver_->GetImmutableClassName(descriptor_->file());
53 }
54 }
55
~ImmutableExtensionLiteGenerator()56 ImmutableExtensionLiteGenerator::~ImmutableExtensionLiteGenerator() {}
57
Generate(io::Printer * printer)58 void ImmutableExtensionLiteGenerator::Generate(io::Printer* printer) {
59 std::map<std::string, std::string> vars;
60 const bool kUseImmutableNames = true;
61 InitTemplateVars(descriptor_, scope_, kUseImmutableNames, name_resolver_,
62 &vars);
63 printer->Print(vars, "public static final int $constant_name$ = $number$;\n");
64
65 WriteFieldDocComment(printer, descriptor_);
66 if (descriptor_->is_repeated()) {
67 printer->Print(
68 vars,
69 "public static final\n"
70 " com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n"
71 " $containing_type$,\n"
72 " $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n"
73 " .newRepeatedGeneratedExtension(\n"
74 " $containing_type$.getDefaultInstance(),\n"
75 " $prototype$,\n"
76 " $enum_map$,\n"
77 " $number$,\n"
78 " com.google.protobuf.WireFormat.FieldType.$type_constant$,\n"
79 " $packed$,\n"
80 " $singular_type$.class);\n");
81 } else {
82 printer->Print(
83 vars,
84 "public static final\n"
85 " com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n"
86 " $containing_type$,\n"
87 " $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n"
88 " .newSingularGeneratedExtension(\n"
89 " $containing_type$.getDefaultInstance(),\n"
90 " $default$,\n"
91 " $prototype$,\n"
92 " $enum_map$,\n"
93 " $number$,\n"
94 " com.google.protobuf.WireFormat.FieldType.$type_constant$,\n"
95 " $singular_type$.class);\n");
96 }
97 printer->Annotate("name", descriptor_);
98 }
99
GenerateNonNestedInitializationCode(io::Printer * printer)100 int ImmutableExtensionLiteGenerator::GenerateNonNestedInitializationCode(
101 io::Printer* printer) {
102 return 0;
103 }
104
GenerateRegistrationCode(io::Printer * printer)105 int ImmutableExtensionLiteGenerator::GenerateRegistrationCode(
106 io::Printer* printer) {
107 printer->Print("registry.add($scope$.$name$);\n", "scope", scope_, "name",
108 UnderscoresToCamelCaseCheckReserved(descriptor_));
109 return 7;
110 }
111
112 } // namespace java
113 } // namespace compiler
114 } // namespace protobuf
115 } // namespace google
116