10e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// Protocol Buffers - Google's data interchange format
20e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// Copyright 2008 Google Inc.  All rights reserved.
30e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// http://code.google.com/p/protobuf/
40e055f079f53b07de3705838a7b4742ce56839f8Brian Duff//
50e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// Redistribution and use in source and binary forms, with or without
60e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// modification, are permitted provided that the following conditions are
70e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// met:
80e055f079f53b07de3705838a7b4742ce56839f8Brian Duff//
90e055f079f53b07de3705838a7b4742ce56839f8Brian Duff//     * Redistributions of source code must retain the above copyright
100e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// notice, this list of conditions and the following disclaimer.
110e055f079f53b07de3705838a7b4742ce56839f8Brian Duff//     * Redistributions in binary form must reproduce the above
120e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// copyright notice, this list of conditions and the following disclaimer
130e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// in the documentation and/or other materials provided with the
140e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// distribution.
150e055f079f53b07de3705838a7b4742ce56839f8Brian Duff//     * Neither the name of Google Inc. nor the names of its
160e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// contributors may be used to endorse or promote products derived from
170e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// this software without specific prior written permission.
180e055f079f53b07de3705838a7b4742ce56839f8Brian Duff//
190e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
210e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
220e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
230e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
240e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
250e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
260e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
270e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
280e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
290e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300e055f079f53b07de3705838a7b4742ce56839f8Brian Duff
310e055f079f53b07de3705838a7b4742ce56839f8Brian Duff// Author: bduff@google.com (Brian Duff)
320e055f079f53b07de3705838a7b4742ce56839f8Brian Duff
330e055f079f53b07de3705838a7b4742ce56839f8Brian Duff#include <google/protobuf/compiler/javanano/javanano_extension.h>
340e055f079f53b07de3705838a7b4742ce56839f8Brian Duff#include <google/protobuf/compiler/javanano/javanano_helpers.h>
350e055f079f53b07de3705838a7b4742ce56839f8Brian Duff#include <google/protobuf/io/printer.h>
360e055f079f53b07de3705838a7b4742ce56839f8Brian Duff#include <google/protobuf/stubs/strutil.h>
370e055f079f53b07de3705838a7b4742ce56839f8Brian Duff#include <google/protobuf/wire_format.h>
380e055f079f53b07de3705838a7b4742ce56839f8Brian Duff
390e055f079f53b07de3705838a7b4742ce56839f8Brian Duffnamespace google {
400e055f079f53b07de3705838a7b4742ce56839f8Brian Duffnamespace protobuf {
410e055f079f53b07de3705838a7b4742ce56839f8Brian Duffnamespace compiler {
420e055f079f53b07de3705838a7b4742ce56839f8Brian Duffnamespace javanano {
430e055f079f53b07de3705838a7b4742ce56839f8Brian Duff
440e055f079f53b07de3705838a7b4742ce56839f8Brian Duffusing internal::WireFormat;
450e055f079f53b07de3705838a7b4742ce56839f8Brian Duff
460e055f079f53b07de3705838a7b4742ce56839f8Brian Duffvoid SetVariables(const FieldDescriptor* descriptor, const Params params,
470e055f079f53b07de3705838a7b4742ce56839f8Brian Duff                  map<string, string>* variables) {
48f4e01452f159ae6b53f5edd25fa647ca2919ae10Tom Chao  (*variables)["name"] =
49f4e01452f159ae6b53f5edd25fa647ca2919ae10Tom Chao    RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
500e055f079f53b07de3705838a7b4742ce56839f8Brian Duff  (*variables)["number"] = SimpleItoa(descriptor->number());
510e055f079f53b07de3705838a7b4742ce56839f8Brian Duff  (*variables)["extends"] = ClassName(params, descriptor->containing_type());
520e055f079f53b07de3705838a7b4742ce56839f8Brian Duff
530e055f079f53b07de3705838a7b4742ce56839f8Brian Duff  string type;
540e055f079f53b07de3705838a7b4742ce56839f8Brian Duff  JavaType java_type = GetJavaType(descriptor->type());
550e055f079f53b07de3705838a7b4742ce56839f8Brian Duff  switch (java_type) {
560e055f079f53b07de3705838a7b4742ce56839f8Brian Duff    case JAVATYPE_ENUM:
570e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      type = "java.lang.Integer";
580e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      break;
590e055f079f53b07de3705838a7b4742ce56839f8Brian Duff    case JAVATYPE_MESSAGE:
600e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      type = ClassName(params, descriptor->message_type());
610e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      break;
620e055f079f53b07de3705838a7b4742ce56839f8Brian Duff    default:
630e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      type = BoxedPrimitiveTypeName(java_type);
640e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      break;
650e055f079f53b07de3705838a7b4742ce56839f8Brian Duff  }
660e055f079f53b07de3705838a7b4742ce56839f8Brian Duff  (*variables)["type"] = type;
670e055f079f53b07de3705838a7b4742ce56839f8Brian Duff}
680e055f079f53b07de3705838a7b4742ce56839f8Brian Duff
690e055f079f53b07de3705838a7b4742ce56839f8Brian DuffExtensionGenerator::
700e055f079f53b07de3705838a7b4742ce56839f8Brian DuffExtensionGenerator(const FieldDescriptor* descriptor, const Params& params)
710e055f079f53b07de3705838a7b4742ce56839f8Brian Duff  : params_(params), descriptor_(descriptor) {
720e055f079f53b07de3705838a7b4742ce56839f8Brian Duff  SetVariables(descriptor, params, &variables_);
730e055f079f53b07de3705838a7b4742ce56839f8Brian Duff}
740e055f079f53b07de3705838a7b4742ce56839f8Brian Duff
750e055f079f53b07de3705838a7b4742ce56839f8Brian DuffExtensionGenerator::~ExtensionGenerator() {}
760e055f079f53b07de3705838a7b4742ce56839f8Brian Duff
770e055f079f53b07de3705838a7b4742ce56839f8Brian Duffvoid ExtensionGenerator::Generate(io::Printer* printer) const {
780e055f079f53b07de3705838a7b4742ce56839f8Brian Duff  if (descriptor_->is_repeated()) {
790e055f079f53b07de3705838a7b4742ce56839f8Brian Duff    printer->Print(variables_,
800e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      "// Extends $extends$\n"
810e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      "public static final com.google.protobuf.nano.Extension<java.util.List<$type$>> $name$ = \n"
820e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      "    com.google.protobuf.nano.Extension.createRepeated($number$,\n"
830e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      "        new com.google.protobuf.nano.Extension.TypeLiteral<java.util.List<$type$>>(){});\n");
840e055f079f53b07de3705838a7b4742ce56839f8Brian Duff  } else {
850e055f079f53b07de3705838a7b4742ce56839f8Brian Duff    printer->Print(variables_,
860e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      "// Extends $extends$\n"
870e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      "public static final com.google.protobuf.nano.Extension<$type$> $name$ =\n"
880e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      "    com.google.protobuf.nano.Extension.create($number$,\n"
890e055f079f53b07de3705838a7b4742ce56839f8Brian Duff      "        new com.google.protobuf.nano.Extension.TypeLiteral<$type$>(){});\n");
900e055f079f53b07de3705838a7b4742ce56839f8Brian Duff  }
910e055f079f53b07de3705838a7b4742ce56839f8Brian Duff}
920e055f079f53b07de3705838a7b4742ce56839f8Brian Duff
930e055f079f53b07de3705838a7b4742ce56839f8Brian Duff}  // namespace javanano
940e055f079f53b07de3705838a7b4742ce56839f8Brian Duff}  // namespace compiler
950e055f079f53b07de3705838a7b4742ce56839f8Brian Duff}  // namespace protobuf
960e055f079f53b07de3705838a7b4742ce56839f8Brian Duff}  // namespace google
970e055f079f53b07de3705838a7b4742ce56839f8Brian Duff
98