javanano_extension.cc revision 286271f75aa6ff1f3746379b77d6dc55415baa16
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Protocol Buffers - Google's data interchange format
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2008 Google Inc.  All rights reserved.
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// http://code.google.com/p/protobuf/
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Redistribution and use in source and binary forms, with or without
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// modification, are permitted provided that the following conditions are
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// met:
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Redistributions of source code must retain the above copyright
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notice, this list of conditions and the following disclaimer.
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     * Redistributions in binary form must reproduce the above
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// copyright notice, this list of conditions and the following disclaimer
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the documentation and/or other materials provided with the
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// distribution.
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//     * Neither the name of Google Inc. nor the names of its
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// contributors may be used to endorse or promote products derived from
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// this software without specific prior written permission.
18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
19a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Author: bduff@google.com (Brian Duff)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <google/protobuf/compiler/javanano/javanano_extension.h>
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <google/protobuf/compiler/javanano/javanano_helpers.h>
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <google/protobuf/io/printer.h>
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <google/protobuf/stubs/strutil.h>
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <google/protobuf/wire_format.h>
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace google {
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace protobuf {
41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace compiler {
42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace javanano {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using internal::WireFormat;
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SetVariables(const FieldDescriptor* descriptor, const Params params,
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  map<string, string>* variables) {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  (*variables)["name"] =
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  (*variables)["number"] = SimpleItoa(descriptor->number());
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  (*variables)["extends"] = ClassName(params, descriptor->containing_type());
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string type;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  JavaType java_type = GetJavaType(descriptor->type());
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (java_type) {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case JAVATYPE_ENUM:
578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      type = "java.lang.Integer";
588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      break;
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    case JAVATYPE_MESSAGE:
608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      type = ClassName(params, descriptor->message_type());
618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      break;
628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    default:
638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      type = BoxedPrimitiveTypeName(java_type);
648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      break;
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  (*variables)["type"] = type;
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ExtensionGenerator::
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ExtensionGenerator(const FieldDescriptor* descriptor, const Params& params)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  : params_(params), descriptor_(descriptor) {
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SetVariables(descriptor, params, &variables_);
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ExtensionGenerator::~ExtensionGenerator() {}
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ExtensionGenerator::Generate(io::Printer* printer) const {
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (descriptor_->is_repeated()) {
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    printer->Print(variables_,
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "\n"
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "// extends $extends$\n"
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "public static final com.google.protobuf.nano.Extension<java.util.List<$type$>> $name$ = \n"
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "    com.google.protobuf.nano.Extension.createRepeated($number$,\n"
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "        new com.google.protobuf.nano.Extension.TypeLiteral<java.util.List<$type$>>(){});\n");
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    printer->Print(variables_,
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "\n"
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "// extends $extends$\n"
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "public static final com.google.protobuf.nano.Extension<$type$> $name$ =\n"
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "    com.google.protobuf.nano.Extension.create($number$,\n"
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      "        new com.google.protobuf.nano.Extension.TypeLiteral<$type$>(){});\n");
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace javanano
96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace compiler
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace protobuf
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace google
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)