temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3 | // http://code.google.com/p/protobuf/ |
| 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 30 | |
| 31 | // Author: kenton@google.com (Kenton Varda) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | |
| 35 | #include <google/protobuf/compiler/java/java_extension.h> |
| 36 | #include <google/protobuf/compiler/java/java_helpers.h> |
| 37 | #include <google/protobuf/stubs/strutil.h> |
| 38 | #include <google/protobuf/io/printer.h> |
| 39 | |
| 40 | namespace google { |
| 41 | namespace protobuf { |
| 42 | namespace compiler { |
| 43 | namespace java { |
| 44 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | |
| 47 | const char* TypeName(FieldDescriptor::Type field_type) { |
| 48 | switch (field_type) { |
| 49 | case FieldDescriptor::TYPE_INT32 : return "INT32"; |
| 50 | case FieldDescriptor::TYPE_UINT32 : return "UINT32"; |
| 51 | case FieldDescriptor::TYPE_SINT32 : return "SINT32"; |
| 52 | case FieldDescriptor::TYPE_FIXED32 : return "FIXED32"; |
| 53 | case FieldDescriptor::TYPE_SFIXED32: return "SFIXED32"; |
| 54 | case FieldDescriptor::TYPE_INT64 : return "INT64"; |
| 55 | case FieldDescriptor::TYPE_UINT64 : return "UINT64"; |
| 56 | case FieldDescriptor::TYPE_SINT64 : return "SINT64"; |
| 57 | case FieldDescriptor::TYPE_FIXED64 : return "FIXED64"; |
| 58 | case FieldDescriptor::TYPE_SFIXED64: return "SFIXED64"; |
| 59 | case FieldDescriptor::TYPE_FLOAT : return "FLOAT"; |
| 60 | case FieldDescriptor::TYPE_DOUBLE : return "DOUBLE"; |
| 61 | case FieldDescriptor::TYPE_BOOL : return "BOOL"; |
| 62 | case FieldDescriptor::TYPE_STRING : return "STRING"; |
| 63 | case FieldDescriptor::TYPE_BYTES : return "BYTES"; |
| 64 | case FieldDescriptor::TYPE_ENUM : return "ENUM"; |
| 65 | case FieldDescriptor::TYPE_GROUP : return "GROUP"; |
| 66 | case FieldDescriptor::TYPE_MESSAGE : return "MESSAGE"; |
| 67 | |
| 68 | // No default because we want the compiler to complain if any new |
| 69 | // types are added. |
| 70 | } |
| 71 | |
| 72 | GOOGLE_LOG(FATAL) << "Can't get here."; |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 78 | ExtensionGenerator::ExtensionGenerator(const FieldDescriptor* descriptor) |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 79 | : descriptor_(descriptor) { |
| 80 | if (descriptor_->extension_scope() != NULL) { |
| 81 | scope_ = ClassName(descriptor_->extension_scope()); |
| 82 | } else { |
| 83 | scope_ = ClassName(descriptor_->file()); |
| 84 | } |
| 85 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 86 | |
| 87 | ExtensionGenerator::~ExtensionGenerator() {} |
| 88 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 89 | // Initializes the vars referenced in the generated code templates. |
| 90 | void InitTemplateVars(const FieldDescriptor* descriptor, |
| 91 | const string& scope, |
| 92 | map<string, string>* vars_pointer) { |
| 93 | map<string, string> &vars = *vars_pointer; |
| 94 | vars["scope"] = scope; |
| 95 | vars["name"] = UnderscoresToCamelCase(descriptor); |
| 96 | vars["containing_type"] = ClassName(descriptor->containing_type()); |
| 97 | vars["number"] = SimpleItoa(descriptor->number()); |
| 98 | vars["constant_name"] = FieldConstantName(descriptor); |
| 99 | vars["index"] = SimpleItoa(descriptor->index()); |
| 100 | vars["default"] = |
| 101 | descriptor->is_repeated() ? "" : DefaultValue(descriptor); |
| 102 | vars["type_constant"] = TypeName(GetType(descriptor)); |
| 103 | vars["packed"] = descriptor->options().packed() ? "true" : "false"; |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 104 | vars["enum_map"] = "null"; |
| 105 | vars["prototype"] = "null"; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 106 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 107 | JavaType java_type = GetJavaType(descriptor); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 108 | string singular_type; |
| 109 | switch (java_type) { |
| 110 | case JAVATYPE_MESSAGE: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 111 | singular_type = ClassName(descriptor->message_type()); |
| 112 | vars["prototype"] = singular_type + ".getDefaultInstance()"; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 113 | break; |
| 114 | case JAVATYPE_ENUM: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 115 | singular_type = ClassName(descriptor->enum_type()); |
| 116 | vars["enum_map"] = singular_type + ".internalGetValueMap()"; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 117 | break; |
| 118 | default: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 119 | singular_type = BoxedPrimitiveTypeName(java_type); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 120 | break; |
| 121 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 122 | vars["type"] = descriptor->is_repeated() ? |
| 123 | "java.util.List<" + singular_type + ">" : singular_type; |
| 124 | vars["singular_type"] = singular_type; |
| 125 | } |
| 126 | |
| 127 | void ExtensionGenerator::Generate(io::Printer* printer) { |
| 128 | map<string, string> vars; |
| 129 | InitTemplateVars(descriptor_, scope_, &vars); |
| 130 | printer->Print(vars, |
| 131 | "public static final int $constant_name$ = $number$;\n"); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 132 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 133 | if (HasDescriptorMethods(descriptor_->file())) { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 134 | // Non-lite extensions |
| 135 | if (descriptor_->extension_scope() == NULL) { |
| 136 | // Non-nested |
| 137 | printer->Print( |
| 138 | vars, |
| 139 | "public static final\n" |
| 140 | " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" |
| 141 | " $containing_type$,\n" |
| 142 | " $type$> $name$ = com.google.protobuf.GeneratedMessage\n" |
| 143 | " .newFileScopedGeneratedExtension(\n" |
| 144 | " $singular_type$.class,\n" |
| 145 | " $prototype$);\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 146 | } else { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 147 | // Nested |
| 148 | printer->Print( |
| 149 | vars, |
| 150 | "public static final\n" |
| 151 | " com.google.protobuf.GeneratedMessage.GeneratedExtension<\n" |
| 152 | " $containing_type$,\n" |
| 153 | " $type$> $name$ = com.google.protobuf.GeneratedMessage\n" |
| 154 | " .newMessageScopedGeneratedExtension(\n" |
| 155 | " $scope$.getDefaultInstance(),\n" |
| 156 | " $index$,\n" |
| 157 | " $singular_type$.class,\n" |
| 158 | " $prototype$);\n"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 159 | } |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 160 | } else { |
| 161 | // Lite extensions |
| 162 | if (descriptor_->is_repeated()) { |
| 163 | printer->Print( |
| 164 | vars, |
| 165 | "public static final\n" |
| 166 | " com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n" |
| 167 | " $containing_type$,\n" |
| 168 | " $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n" |
| 169 | " .newRepeatedGeneratedExtension(\n" |
| 170 | " $containing_type$.getDefaultInstance(),\n" |
| 171 | " $prototype$,\n" |
| 172 | " $enum_map$,\n" |
| 173 | " $number$,\n" |
| 174 | " com.google.protobuf.WireFormat.FieldType.$type_constant$,\n" |
| 175 | " $packed$);\n"); |
| 176 | } else { |
| 177 | printer->Print( |
| 178 | vars, |
| 179 | "public static final\n" |
| 180 | " com.google.protobuf.GeneratedMessageLite.GeneratedExtension<\n" |
| 181 | " $containing_type$,\n" |
| 182 | " $type$> $name$ = com.google.protobuf.GeneratedMessageLite\n" |
| 183 | " .newSingularGeneratedExtension(\n" |
| 184 | " $containing_type$.getDefaultInstance(),\n" |
| 185 | " $default$,\n" |
| 186 | " $prototype$,\n" |
| 187 | " $enum_map$,\n" |
| 188 | " $number$,\n" |
| 189 | " com.google.protobuf.WireFormat.FieldType.$type_constant$);\n"); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void ExtensionGenerator::GenerateNonNestedInitializationCode( |
| 195 | io::Printer* printer) { |
| 196 | if (descriptor_->extension_scope() == NULL && |
| 197 | HasDescriptorMethods(descriptor_->file())) { |
| 198 | // Only applies to non-nested, non-lite extensions. |
| 199 | printer->Print( |
| 200 | "$name$.internalInit(descriptor.getExtensions().get($index$));\n", |
| 201 | "name", UnderscoresToCamelCase(descriptor_), |
| 202 | "index", SimpleItoa(descriptor_->index())); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 206 | void ExtensionGenerator::GenerateRegistrationCode(io::Printer* printer) { |
| 207 | printer->Print( |
| 208 | "registry.add($scope$.$name$);\n", |
| 209 | "scope", scope_, |
| 210 | "name", UnderscoresToCamelCase(descriptor_)); |
| 211 | } |
| 212 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 213 | } // namespace java |
| 214 | } // namespace compiler |
| 215 | } // namespace protobuf |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 216 | } // namespace google |