You can use the processor option store_unknown_fields to switch this support on: aprotoc --javanano_out=store_unknown_fields=true:/tmp/out A separate option for extensions isn't required. Support for unknown fields must be turned on to allow storing and retrieving extensions, because they are just stored as unknown fields. If unknown fields are switched on, extension related code will be generated when a proto message includes an extension range, or an extension is encountered. By default, store_unknown_fields is false. No additional code is generated, and the generator will error out if protos contain extension ranges or extensions. Change-Id: I1e034c9e8f3305612953f72438189a7da6ed2167pull/91/head
parent
661f87ceb8
commit
5659cca8c8
14 changed files with 806 additions and 25 deletions
@ -0,0 +1,114 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2013 Google Inc. All rights reserved.
|
||||
// http://code.google.com/p/protobuf/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.protobuf.nano; |
||||
|
||||
import java.lang.reflect.ParameterizedType; |
||||
import java.lang.reflect.Type; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Represents an extension. |
||||
* |
||||
* @author bduff@google.com (Brian Duff) |
||||
* @param <T> the type of the extension. |
||||
*/ |
||||
public class Extension<T> { |
||||
public final int fieldNumber; |
||||
public boolean isRepeatedField; |
||||
public Class<T> fieldType; |
||||
public Class<T> listType; |
||||
|
||||
private Extension(int fieldNumber, TypeLiteral<T> type) { |
||||
this.fieldNumber = fieldNumber; |
||||
isRepeatedField = type.isList(); |
||||
fieldType = type.getTargetClass(); |
||||
listType = isRepeatedField ? type.getListType() : null; |
||||
} |
||||
|
||||
/** |
||||
* Creates a new instance of {@code Extension} for the specified {@code fieldNumber} and |
||||
* {@code type}. |
||||
*/ |
||||
public static <T> Extension<T> create(int fieldNumber, TypeLiteral<T> type) { |
||||
return new Extension<T>(fieldNumber, type); |
||||
} |
||||
|
||||
/** |
||||
* Creates a new instance of {@code Extension} for the specified {@code fieldNumber} and |
||||
* {@code type}. This version is used for repeated fields. |
||||
*/ |
||||
public static <T> Extension<List<T>> createRepeated(int fieldNumber, TypeLiteral<List<T>> type) { |
||||
return new Extension<List<T>>(fieldNumber, type); |
||||
} |
||||
|
||||
/** |
||||
* Represents a generic type literal. We can't typesafely reference a |
||||
* Class<List<Foo>>.class in Java, so we use this instead. |
||||
* See: http://gafter.blogspot.com/2006/12/super-type-tokens.html
|
||||
* |
||||
* <p>Somewhat specialized because we only ever have a Foo or a List<Foo>. |
||||
*/ |
||||
public static abstract class TypeLiteral<T> { |
||||
private final Type type; |
||||
|
||||
protected TypeLiteral() { |
||||
Type superclass = getClass().getGenericSuperclass(); |
||||
if (superclass instanceof Class) { |
||||
throw new RuntimeException("Missing type parameter"); |
||||
} |
||||
this.type = ((ParameterizedType) superclass).getActualTypeArguments()[0]; |
||||
} |
||||
|
||||
/** |
||||
* If the generic type is a list, returns {@code true}. |
||||
*/ |
||||
private boolean isList() { |
||||
return type instanceof ParameterizedType; |
||||
} |
||||
|
||||
@SuppressWarnings("unchecked") |
||||
private Class<T> getListType() { |
||||
return (Class<T>) ((ParameterizedType) type).getRawType(); |
||||
} |
||||
|
||||
/** |
||||
* If the generic type is a list, returns the type of element in the list. Otherwise, |
||||
* returns the actual type. |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
private Class<T> getTargetClass() { |
||||
if (isList()) { |
||||
return (Class<T>) ((ParameterizedType) type).getActualTypeArguments()[0]; |
||||
} |
||||
return (Class<T>) type; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,47 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2013 Google Inc. All rights reserved.
|
||||
// http://code.google.com/p/protobuf/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.protobuf.nano; |
||||
|
||||
/** |
||||
* Stores unknown fields. These might be extensions or fields that the generated API doesn't |
||||
* know about yet. |
||||
* |
||||
* @author bduff@google.com (Brian Duff) |
||||
*/ |
||||
public final class UnknownFieldData { |
||||
final int tag; |
||||
final byte[] bytes; |
||||
|
||||
UnknownFieldData(int tag, byte[] bytes) { |
||||
this.tag = tag; |
||||
this.bytes = bytes; |
||||
} |
||||
} |
@ -0,0 +1,96 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. All rights reserved.
|
||||
// http://code.google.com/p/protobuf/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// Author: bduff@google.com (Brian Duff)
|
||||
|
||||
#include <google/protobuf/compiler/javanano/javanano_extension.h> |
||||
#include <google/protobuf/compiler/javanano/javanano_helpers.h> |
||||
#include <google/protobuf/io/printer.h> |
||||
#include <google/protobuf/stubs/strutil.h> |
||||
#include <google/protobuf/wire_format.h> |
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
namespace compiler { |
||||
namespace javanano { |
||||
|
||||
using internal::WireFormat; |
||||
|
||||
void SetVariables(const FieldDescriptor* descriptor, const Params params, |
||||
map<string, string>* variables) { |
||||
(*variables)["name"] = UnderscoresToCamelCase(descriptor); |
||||
(*variables)["number"] = SimpleItoa(descriptor->number()); |
||||
(*variables)["extends"] = ClassName(params, descriptor->containing_type()); |
||||
|
||||
string type; |
||||
JavaType java_type = GetJavaType(descriptor->type()); |
||||
switch (java_type) { |
||||
case JAVATYPE_ENUM: |
||||
type = "java.lang.Integer"; |
||||
break; |
||||
case JAVATYPE_MESSAGE: |
||||
type = ClassName(params, descriptor->message_type()); |
||||
break; |
||||
default: |
||||
type = BoxedPrimitiveTypeName(java_type); |
||||
break; |
||||
} |
||||
(*variables)["type"] = type; |
||||
} |
||||
|
||||
ExtensionGenerator:: |
||||
ExtensionGenerator(const FieldDescriptor* descriptor, const Params& params) |
||||
: params_(params), descriptor_(descriptor) { |
||||
SetVariables(descriptor, params, &variables_); |
||||
} |
||||
|
||||
ExtensionGenerator::~ExtensionGenerator() {} |
||||
|
||||
void ExtensionGenerator::Generate(io::Printer* printer) const { |
||||
if (descriptor_->is_repeated()) { |
||||
printer->Print(variables_, |
||||
"// Extends $extends$\n" |
||||
"public static final com.google.protobuf.nano.Extension<java.util.List<$type$>> $name$ = \n" |
||||
" com.google.protobuf.nano.Extension.createRepeated($number$,\n" |
||||
" new com.google.protobuf.nano.Extension.TypeLiteral<java.util.List<$type$>>(){});\n"); |
||||
} else { |
||||
printer->Print(variables_, |
||||
"// Extends $extends$\n" |
||||
"public static final com.google.protobuf.nano.Extension<$type$> $name$ =\n" |
||||
" com.google.protobuf.nano.Extension.create($number$,\n" |
||||
" new com.google.protobuf.nano.Extension.TypeLiteral<$type$>(){});\n"); |
||||
} |
||||
} |
||||
|
||||
} // namespace javanano
|
||||
} // namespace compiler
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
|
@ -0,0 +1,74 @@ |
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. All rights reserved.
|
||||
// http://code.google.com/p/protobuf/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// Author: bduff@google.com (Brian Duff)
|
||||
// Based on original Protocol Buffers design by
|
||||
// Sanjay Ghemawat, Jeff Dean, and others.
|
||||
|
||||
#ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_EXTENSION_H_ |
||||
#define GOOGLE_PROTOBUF_COMPILER_JAVA_EXTENSION_H_ |
||||
|
||||
#include <google/protobuf/stubs/common.h> |
||||
#include <google/protobuf/compiler/javanano/javanano_params.h> |
||||
#include <google/protobuf/descriptor.pb.h> |
||||
|
||||
|
||||
namespace google { |
||||
namespace protobuf { |
||||
namespace io { |
||||
class Printer; // printer.h
|
||||
} |
||||
} |
||||
|
||||
namespace protobuf { |
||||
namespace compiler { |
||||
namespace javanano { |
||||
|
||||
class ExtensionGenerator { |
||||
public: |
||||
explicit ExtensionGenerator(const FieldDescriptor* descriptor, const Params& params); |
||||
~ExtensionGenerator(); |
||||
|
||||
void Generate(io::Printer* printer) const; |
||||
|
||||
private: |
||||
const Params& params_; |
||||
const FieldDescriptor* descriptor_; |
||||
map<string, string> variables_; |
||||
|
||||
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ExtensionGenerator); |
||||
}; |
||||
|
||||
} // namespace javanano
|
||||
} // namespace compiler
|
||||
} // namespace protobuf
|
||||
} // namespace google
|
||||
|
||||
#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_EXTENSION_H_
|
@ -0,0 +1,44 @@ |
||||
syntax = "proto2"; |
||||
|
||||
option java_outer_classname = "Extensions"; |
||||
option java_package = "com.google.protobuf.nano"; |
||||
|
||||
message ExtendableMessage { |
||||
optional int32 field = 1; |
||||
extensions 10 to max; |
||||
} |
||||
|
||||
enum AnEnum { |
||||
FIRST_VALUE = 1; |
||||
SECOND_VALUE = 2; |
||||
} |
||||
|
||||
message AnotherMessage { |
||||
optional string string = 1; |
||||
optional bool value = 2; |
||||
} |
||||
|
||||
extend ExtendableMessage { |
||||
optional string some_string = 10; |
||||
optional int32 some_int = 11; |
||||
optional int64 some_long = 12; |
||||
optional float some_float = 13; |
||||
optional double some_double = 14; |
||||
optional bool some_bool = 15; |
||||
optional AnEnum some_enum = 16; |
||||
optional AnotherMessage some_message = 17; |
||||
repeated string some_repeated_string = 18; |
||||
repeated int32 some_repeated_int = 19; |
||||
repeated int64 some_repeated_long = 20; |
||||
repeated float some_repeated_float = 21; |
||||
repeated double some_repeated_double = 22; |
||||
repeated bool some_repeated_bool = 23; |
||||
repeated AnEnum some_repeated_enum = 24; |
||||
repeated AnotherMessage some_repeated_message = 25; |
||||
} |
||||
|
||||
message ContainerMessage { |
||||
extend ExtendableMessage { |
||||
optional bool another_thing = 100; |
||||
} |
||||
} |
Loading…
Reference in new issue