Commit 86595020 authored by Ulas Kirazci's avatar Ulas Kirazci Committed by Gerrit Code Review

Merge "Update nano to serialize java keywords properly."

parents b8f5dad1 d4bb971d
...@@ -2250,6 +2250,12 @@ public class NanoTest extends TestCase { ...@@ -2250,6 +2250,12 @@ public class NanoTest extends TestCase {
assertEquals(message.d, newMessage.d); assertEquals(message.d, newMessage.d);
} }
public void testJavaKeyword() throws Exception {
TestAllTypesNano msg = new TestAllTypesNano();
msg.synchronized_ = 123;
assertEquals(123, msg.synchronized_);
}
private <T> List<T> list(T first, T... remaining) { private <T> List<T> list(T first, T... remaining) {
List<T> list = new ArrayList<T>(); List<T> list = new ArrayList<T>();
list.add(first); list.add(first);
......
...@@ -82,7 +82,7 @@ void EnumGenerator::Generate(io::Printer* printer) { ...@@ -82,7 +82,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
} }
for (int i = 0; i < canonical_values_.size(); i++) { for (int i = 0; i < canonical_values_.size(); i++) {
map<string, string> vars; map<string, string> vars;
vars["name"] = canonical_values_[i]->name(); vars["name"] = RenameJavaKeywords(canonical_values_[i]->name());
vars["canonical_value"] = SimpleItoa(canonical_values_[i]->number()); vars["canonical_value"] = SimpleItoa(canonical_values_[i]->number());
printer->Print(vars, printer->Print(vars,
"public static final int $name$ = $canonical_value$;\n"); "public static final int $name$ = $canonical_value$;\n");
...@@ -92,7 +92,7 @@ void EnumGenerator::Generate(io::Printer* printer) { ...@@ -92,7 +92,7 @@ void EnumGenerator::Generate(io::Printer* printer) {
for (int i = 0; i < aliases_.size(); i++) { for (int i = 0; i < aliases_.size(); i++) {
map<string, string> vars; map<string, string> vars;
vars["name"] = aliases_[i].value->name(); vars["name"] = RenameJavaKeywords(aliases_[i].value->name());
vars["canonical_name"] = aliases_[i].canonical_value->name(); vars["canonical_name"] = aliases_[i].canonical_value->name();
printer->Print(vars, printer->Print(vars,
"public static final int $name$ = $canonical_name$;\n"); "public static final int $name$ = $canonical_name$;\n");
......
...@@ -54,9 +54,9 @@ namespace { ...@@ -54,9 +54,9 @@ namespace {
void SetEnumVariables(const Params& params, void SetEnumVariables(const Params& params,
const FieldDescriptor* descriptor, map<string, string>* variables) { const FieldDescriptor* descriptor, map<string, string>* variables) {
(*variables)["name"] = (*variables)["name"] =
UnderscoresToCamelCase(descriptor); RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
(*variables)["capitalized_name"] = (*variables)["capitalized_name"] =
UnderscoresToCapitalizedCamelCase(descriptor); RenameJavaKeywords(UnderscoresToCapitalizedCamelCase(descriptor));
(*variables)["number"] = SimpleItoa(descriptor->number()); (*variables)["number"] = SimpleItoa(descriptor->number());
(*variables)["type"] = "int"; (*variables)["type"] = "int";
(*variables)["default"] = DefaultValue(params, descriptor); (*variables)["default"] = DefaultValue(params, descriptor);
......
...@@ -45,7 +45,8 @@ using internal::WireFormat; ...@@ -45,7 +45,8 @@ using internal::WireFormat;
void SetVariables(const FieldDescriptor* descriptor, const Params params, void SetVariables(const FieldDescriptor* descriptor, const Params params,
map<string, string>* variables) { map<string, string>* variables) {
(*variables)["name"] = UnderscoresToCamelCase(descriptor); (*variables)["name"] =
RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
(*variables)["number"] = SimpleItoa(descriptor->number()); (*variables)["number"] = SimpleItoa(descriptor->number());
(*variables)["extends"] = ClassName(params, descriptor->containing_type()); (*variables)["extends"] = ClassName(params, descriptor->containing_type());
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include <google/protobuf/compiler/javanano/javanano_helpers.h> #include <google/protobuf/compiler/javanano/javanano_helpers.h>
#include <google/protobuf/compiler/javanano/javanano_params.h> #include <google/protobuf/compiler/javanano/javanano_params.h>
#include <google/protobuf/descriptor.pb.h> #include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/stubs/hash.h>
#include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/substitute.h> #include <google/protobuf/stubs/substitute.h>
...@@ -50,6 +51,48 @@ const char kThickSeparator[] = ...@@ -50,6 +51,48 @@ const char kThickSeparator[] =
const char kThinSeparator[] = const char kThinSeparator[] =
"// -------------------------------------------------------------------\n"; "// -------------------------------------------------------------------\n";
class RenameKeywords {
private:
hash_set<string> java_keywords_set_;
public:
RenameKeywords() {
static const char* kJavaKeywordsList[] = {
// Reserved Java Keywords
"abstract", "assert", "boolean", "break", "byte", "case", "catch",
"char", "class", "const", "continue", "default", "do", "double", "else",
"enum", "extends", "final", "finally", "float", "for", "goto", "if",
"implements", "import", "instanceof", "int", "interface", "long",
"native", "new", "package", "private", "protected", "public", "return",
"short", "static", "strictfp", "super", "switch", "synchronized",
"this", "throw", "throws", "transient", "try", "void", "volatile", "while",
// Reserved Keywords for Literals
"false", "null", "true"
};
for (int i = 0; i < GOOGLE_ARRAYSIZE(kJavaKeywordsList); i++) {
java_keywords_set_.insert(kJavaKeywordsList[i]);
}
}
// Used to rename the a field name if it's a java keyword. Specifically
// this is used to rename the ["name"] or ["capitalized_name"] field params.
// (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html)
string RenameJavaKeywordsImpl(const string& input) {
string result = input;
if (java_keywords_set_.find(result) != java_keywords_set_.end()) {
result += "_";
}
return result;
}
};
static RenameKeywords sRenameKeywords;
namespace { namespace {
const char* kDefaultPackage = ""; const char* kDefaultPackage = "";
...@@ -110,6 +153,10 @@ string UnderscoresToCamelCase(const MethodDescriptor* method) { ...@@ -110,6 +153,10 @@ string UnderscoresToCamelCase(const MethodDescriptor* method) {
return UnderscoresToCamelCaseImpl(method->name(), false); return UnderscoresToCamelCaseImpl(method->name(), false);
} }
string RenameJavaKeywords(const string& input) {
return sRenameKeywords.RenameJavaKeywordsImpl(input);
}
string StripProto(const string& filename) { string StripProto(const string& filename) {
if (HasSuffixString(filename, ".protodevel")) { if (HasSuffixString(filename, ".protodevel")) {
return StripSuffixString(filename, ".protodevel"); return StripSuffixString(filename, ".protodevel");
......
...@@ -55,6 +55,10 @@ extern const char kThinSeparator[]; ...@@ -55,6 +55,10 @@ extern const char kThinSeparator[];
string UnderscoresToCamelCase(const FieldDescriptor* field); string UnderscoresToCamelCase(const FieldDescriptor* field);
string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field); string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field);
// Appends an "_" to the end of a field where the name is a reserved java
// keyword. For example int32 public = 1 will generate int public_.
string RenameJavaKeywords(const string& input);
// Similar, but for method names. (Typically, this merely has the effect // Similar, but for method names. (Typically, this merely has the effect
// of lower-casing the first letter of the name.) // of lower-casing the first letter of the name.)
string UnderscoresToCamelCase(const MethodDescriptor* method); string UnderscoresToCamelCase(const MethodDescriptor* method);
......
...@@ -396,12 +396,12 @@ void MessageGenerator::GenerateClear(io::Printer* printer) { ...@@ -396,12 +396,12 @@ void MessageGenerator::GenerateClear(io::Printer* printer) {
// type. // type.
printer->Print( printer->Print(
"$name$ = $default$.clone();\n", "$name$ = $default$.clone();\n",
"name", UnderscoresToCamelCase(field), "name", RenameJavaKeywords(UnderscoresToCamelCase(field)),
"default", DefaultValue(params_, field)); "default", DefaultValue(params_, field));
} else { } else {
printer->Print( printer->Print(
"$name$ = $default$;\n", "$name$ = $default$;\n",
"name", UnderscoresToCamelCase(field), "name", RenameJavaKeywords(UnderscoresToCamelCase(field)),
"default", DefaultValue(params_, field)); "default", DefaultValue(params_, field));
} }
} }
......
...@@ -56,9 +56,9 @@ namespace { ...@@ -56,9 +56,9 @@ namespace {
void SetMessageVariables(const Params& params, void SetMessageVariables(const Params& params,
const FieldDescriptor* descriptor, map<string, string>* variables) { const FieldDescriptor* descriptor, map<string, string>* variables) {
(*variables)["name"] = (*variables)["name"] =
UnderscoresToCamelCase(descriptor); RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
(*variables)["capitalized_name"] = (*variables)["capitalized_name"] =
UnderscoresToCapitalizedCamelCase(descriptor); RenameJavaKeywords(UnderscoresToCapitalizedCamelCase(descriptor));
(*variables)["number"] = SimpleItoa(descriptor->number()); (*variables)["number"] = SimpleItoa(descriptor->number());
(*variables)["type"] = ClassName(params, descriptor->message_type()); (*variables)["type"] = ClassName(params, descriptor->message_type());
(*variables)["group_or_message"] = (*variables)["group_or_message"] =
......
...@@ -208,9 +208,9 @@ bool AllAscii(const string& text) { ...@@ -208,9 +208,9 @@ bool AllAscii(const string& text) {
void SetPrimitiveVariables(const FieldDescriptor* descriptor, const Params params, void SetPrimitiveVariables(const FieldDescriptor* descriptor, const Params params,
map<string, string>* variables) { map<string, string>* variables) {
(*variables)["name"] = (*variables)["name"] =
UnderscoresToCamelCase(descriptor); RenameJavaKeywords(UnderscoresToCamelCase(descriptor));
(*variables)["capitalized_name"] = (*variables)["capitalized_name"] =
UnderscoresToCapitalizedCamelCase(descriptor); RenameJavaKeywords(UnderscoresToCapitalizedCamelCase(descriptor));
(*variables)["number"] = SimpleItoa(descriptor->number()); (*variables)["number"] = SimpleItoa(descriptor->number());
(*variables)["type"] = PrimitiveTypeName(GetJavaType(descriptor)); (*variables)["type"] = PrimitiveTypeName(GetJavaType(descriptor));
(*variables)["default"] = DefaultValue(params, descriptor); (*variables)["default"] = DefaultValue(params, descriptor);
......
...@@ -157,6 +157,9 @@ message TestAllTypesNano { ...@@ -157,6 +157,9 @@ message TestAllTypesNano {
optional int32 tag = 93; optional int32 tag = 93;
optional int32 get_serialized_size = 94; optional int32 get_serialized_size = 94;
optional int32 write_to = 95; optional int32 write_to = 95;
// Try to fail with java reserved keywords
optional int32 synchronized = 96;
} }
message ForeignMessageNano { message ForeignMessageNano {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment