Commit 4c4d6390 authored by Max Cai's avatar Max Cai

Per-file java_multiple_files flag.

Imported source files may have different values for the 'java_multiple_files'
option to the main source file's. Whether the fully qualified Java name of an
entity should include the outer class name depends on the flag value in the
file defining the referenced entity, not the main file. This CL loads the
flag values from the main and all transitively imported files into the params,
and generates the fully qualified Java names accordingly.

If the generator option 'java_multiple_files' is set, its value overrides any
in-file values in all source/imported files. This is because this generator
option is typically used on either none or all source files.

Change-Id: Id6a4a42426d68961dc669487d38f35530deb7d8e
parent 09400156
...@@ -35,6 +35,8 @@ import com.google.protobuf.nano.Extensions; ...@@ -35,6 +35,8 @@ import com.google.protobuf.nano.Extensions;
import com.google.protobuf.nano.Extensions.AnotherMessage; import com.google.protobuf.nano.Extensions.AnotherMessage;
import com.google.protobuf.nano.InternalNano; import com.google.protobuf.nano.InternalNano;
import com.google.protobuf.nano.MessageNano; import com.google.protobuf.nano.MessageNano;
import com.google.protobuf.nano.MultipleImportingNonMultipleNano1;
import com.google.protobuf.nano.MultipleImportingNonMultipleNano2;
import com.google.protobuf.nano.NanoOuterClass; import com.google.protobuf.nano.NanoOuterClass;
import com.google.protobuf.nano.NanoOuterClass.TestAllTypesNano; import com.google.protobuf.nano.NanoOuterClass.TestAllTypesNano;
import com.google.protobuf.nano.RecursiveMessageNano; import com.google.protobuf.nano.RecursiveMessageNano;
...@@ -2039,6 +2041,20 @@ public class NanoTest extends TestCase { ...@@ -2039,6 +2041,20 @@ public class NanoTest extends TestCase {
assertEquals(nestedMsg2.bb, newMsg.repeatedNestedMessage[2].bb); assertEquals(nestedMsg2.bb, newMsg.repeatedNestedMessage[2].bb);
} }
/**
* Tests that code generation with mixed values of the java_multiple_files
* options between the main source file and the imported source files would
* generate correct references. Any error would cause this method to fail
* compilation.
*/
public void testNanoMultipleImportingNonMultiple() throws Exception {
UnittestImportNano.ImportMessageNano importMsg = new UnittestImportNano.ImportMessageNano();
MultipleImportingNonMultipleNano1 nano1 = new MultipleImportingNonMultipleNano1();
nano1.field = importMsg;
MultipleImportingNonMultipleNano2 nano2 = new MultipleImportingNonMultipleNano2();
nano2.nano1 = nano1;
}
public void testNanoDefaults() throws Exception { public void testNanoDefaults() throws Exception {
TestAllTypesNano msg = new TestAllTypesNano(); TestAllTypesNano msg = new TestAllTypesNano();
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
......
...@@ -69,9 +69,10 @@ EnumGenerator::~EnumGenerator() {} ...@@ -69,9 +69,10 @@ EnumGenerator::~EnumGenerator() {}
void EnumGenerator::Generate(io::Printer* printer) { void EnumGenerator::Generate(io::Printer* printer) {
printer->Print("// enum $classname$\n", "classname", descriptor_->name()); printer->Print("// enum $classname$\n", "classname", descriptor_->name());
bool is_own_file = params_.java_multiple_files() || const string& file_name = descriptor_->file()->name();
bool is_own_file = params_.java_multiple_files(file_name) ||
((descriptor_->containing_type() == NULL) && ((descriptor_->containing_type() == NULL) &&
!params_.has_java_outer_classname(descriptor_->file()->name())); !params_.has_java_outer_classname(file_name));
if (is_own_file) { if (is_own_file) {
printer->Print("public final class $classname$ {\n", "classname", printer->Print("public final class $classname$ {\n", "classname",
......
...@@ -186,7 +186,7 @@ void FileGenerator::Generate(io::Printer* printer) { ...@@ -186,7 +186,7 @@ void FileGenerator::Generate(io::Printer* printer) {
ExtensionGenerator(file_->extension(i), params_).Generate(printer); ExtensionGenerator(file_->extension(i), params_).Generate(printer);
} }
if (!params_.java_multiple_files()) { if (!params_.java_multiple_files(file_->name())) {
for (int i = 0; i < file_->enum_type_count(); i++) { for (int i = 0; i < file_->enum_type_count(); i++) {
EnumGenerator(file_->enum_type(i), params_).Generate(printer); EnumGenerator(file_->enum_type(i), params_).Generate(printer);
} }
...@@ -238,7 +238,7 @@ static void GenerateSibling(const string& package_dir, ...@@ -238,7 +238,7 @@ static void GenerateSibling(const string& package_dir,
void FileGenerator::GenerateSiblings(const string& package_dir, void FileGenerator::GenerateSiblings(const string& package_dir,
OutputDirectory* output_directory, OutputDirectory* output_directory,
vector<string>* file_list) { vector<string>* file_list) {
if (params_.java_multiple_files()) { if (params_.java_multiple_files(file_->name())) {
for (int i = 0; i < file_->enum_type_count(); i++) { for (int i = 0; i < file_->enum_type_count(); i++) {
GenerateSibling<EnumGenerator>(package_dir, java_package_, GenerateSibling<EnumGenerator>(package_dir, java_package_,
file_->enum_type(i), file_->enum_type(i),
......
...@@ -57,6 +57,10 @@ void UpdateParamsRecursively(Params& params, ...@@ -57,6 +57,10 @@ void UpdateParamsRecursively(Params& params,
params.set_java_package( params.set_java_package(
file->name(), file->options().java_package()); file->name(), file->options().java_package());
} }
if (file->options().has_java_multiple_files()) {
params.set_java_multiple_files(
file->name(), file->options().java_multiple_files());
}
// Loop through all dependent files recursively // Loop through all dependent files recursively
// adding dep // adding dep
...@@ -84,11 +88,6 @@ bool JavaNanoGenerator::Generate(const FileDescriptor* file, ...@@ -84,11 +88,6 @@ bool JavaNanoGenerator::Generate(const FileDescriptor* file,
string output_list_file; string output_list_file;
Params params(file->name()); Params params(file->name());
// Get options from the proto file
if (file->options().has_java_multiple_files()) {
params.set_java_multiple_files(file->options().java_multiple_files());
}
// Update per file params // Update per file params
UpdateParamsRecursively(params, file); UpdateParamsRecursively(params, file);
...@@ -118,7 +117,7 @@ bool JavaNanoGenerator::Generate(const FileDescriptor* file, ...@@ -118,7 +117,7 @@ bool JavaNanoGenerator::Generate(const FileDescriptor* file,
} else if (options[i].first == "store_unknown_fields") { } else if (options[i].first == "store_unknown_fields") {
params.set_store_unknown_fields(options[i].second == "true"); params.set_store_unknown_fields(options[i].second == "true");
} else if (options[i].first == "java_multiple_files") { } else if (options[i].first == "java_multiple_files") {
params.set_java_multiple_files(options[i].second == "true"); params.set_override_java_multiple_files(options[i].second == "true");
} else { } else {
*error = "Ignore unknown javanano generator option: " + options[i].first; *error = "Ignore unknown javanano generator option: " + options[i].first;
} }
......
...@@ -210,7 +210,7 @@ string FileJavaPackage(const Params& params, const FileDescriptor* file) { ...@@ -210,7 +210,7 @@ string FileJavaPackage(const Params& params, const FileDescriptor* file) {
string ToJavaName(const Params& params, const string& full_name, string ToJavaName(const Params& params, const string& full_name,
const FileDescriptor* file) { const FileDescriptor* file) {
string result; string result;
if (params.java_multiple_files()) { if (params.java_multiple_files(file->name())) {
result = FileJavaPackage(params, file); result = FileJavaPackage(params, file);
} else { } else {
result = ClassName(params, file); result = ClassName(params, file);
...@@ -283,7 +283,7 @@ string ClassName(const Params& params, const EnumDescriptor* descriptor) { ...@@ -283,7 +283,7 @@ string ClassName(const Params& params, const EnumDescriptor* descriptor) {
// If the java_multiple_files option is present, we will generate enums into separate // If the java_multiple_files option is present, we will generate enums into separate
// classes, each named after the original enum type. This takes precedence over // classes, each named after the original enum type. This takes precedence over
// any outer_classname. // any outer_classname.
if (params.java_multiple_files() && last_dot_in_name != string::npos) { if (params.java_multiple_files(file_name) && last_dot_in_name != string::npos) {
string enum_simple_name = full_name.substr(last_dot_in_name + 1); string enum_simple_name = full_name.substr(last_dot_in_name + 1);
if (!result.empty()) { if (!result.empty()) {
result += "."; result += ".";
......
...@@ -121,9 +121,11 @@ void MessageGenerator::GenerateStaticVariableInitializers( ...@@ -121,9 +121,11 @@ void MessageGenerator::GenerateStaticVariableInitializers(
} }
void MessageGenerator::Generate(io::Printer* printer) { void MessageGenerator::Generate(io::Printer* printer) {
const string& file_name = descriptor_->file()->name();
bool is_own_file = bool is_own_file =
params_.java_multiple_files() || ((descriptor_->containing_type() == NULL) params_.java_multiple_files(file_name)
&& !params_.has_java_outer_classname(descriptor_->file()->name())); || ((descriptor_->containing_type() == NULL)
&& !params_.has_java_outer_classname(file_name));
#if 0 #if 0
GOOGLE_LOG(INFO) << "is_own_file=" << is_own_file; GOOGLE_LOG(INFO) << "is_own_file=" << is_own_file;
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#define PROTOBUF_COMPILER_JAVANANO_JAVANANO_PARAMS_H_ #define PROTOBUF_COMPILER_JAVANANO_JAVANANO_PARAMS_H_
#include <map> #include <map>
#include <set>
#include <google/protobuf/stubs/strutil.h> #include <google/protobuf/stubs/strutil.h>
namespace google { namespace google {
...@@ -41,24 +42,28 @@ namespace protobuf { ...@@ -41,24 +42,28 @@ namespace protobuf {
namespace compiler { namespace compiler {
namespace javanano { namespace javanano {
enum eMultipleFiles { JAVANANO_MUL_UNSET, JAVANANO_MUL_FALSE, JAVANANO_MUL_TRUE };
// Parameters for used by the generators // Parameters for used by the generators
class Params { class Params {
public: public:
typedef map<string, string> NameMap; typedef map<string, string> NameMap;
typedef set<string> NameSet;
private: private:
string empty_; string empty_;
string base_name_; string base_name_;
bool java_multiple_files_; eMultipleFiles override_java_multiple_files_;
bool store_unknown_fields_; bool store_unknown_fields_;
NameMap java_packages_; NameMap java_packages_;
NameMap java_outer_classnames_; NameMap java_outer_classnames_;
NameSet java_multiple_files_;
public: public:
Params(const string & base_name) : Params(const string & base_name) :
empty_(""), empty_(""),
base_name_(base_name), base_name_(base_name),
store_unknown_fields_(false), override_java_multiple_files_(JAVANANO_MUL_UNSET),
java_multiple_files_(false) { store_unknown_fields_(false) {
} }
const string& base_name() const { const string& base_name() const {
...@@ -109,6 +114,36 @@ class Params { ...@@ -109,6 +114,36 @@ class Params {
return java_outer_classnames_; return java_outer_classnames_;
} }
void set_override_java_multiple_files(bool java_multiple_files) {
if (java_multiple_files) {
override_java_multiple_files_ = JAVANANO_MUL_TRUE;
} else {
override_java_multiple_files_ = JAVANANO_MUL_FALSE;
}
}
void clear_override_java_multiple_files() {
override_java_multiple_files_ = JAVANANO_MUL_UNSET;
}
void set_java_multiple_files(const string& file_name, bool value) {
if (value) {
java_multiple_files_.insert(file_name);
} else {
java_multiple_files_.erase(file_name);
}
}
bool java_multiple_files(const string& file_name) const {
switch (override_java_multiple_files_) {
case JAVANANO_MUL_FALSE:
return false;
case JAVANANO_MUL_TRUE:
return true;
default:
return java_multiple_files_.find(file_name)
!= java_multiple_files_.end();
}
}
void set_store_unknown_fields(bool value) { void set_store_unknown_fields(bool value) {
store_unknown_fields_ = value; store_unknown_fields_ = value;
} }
...@@ -116,13 +151,6 @@ class Params { ...@@ -116,13 +151,6 @@ class Params {
return store_unknown_fields_; return store_unknown_fields_;
} }
void set_java_multiple_files(bool value) {
java_multiple_files_ = value;
}
bool java_multiple_files() const {
return java_multiple_files_;
}
}; };
} // namespace javanano } // namespace javanano
......
// 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: maxtroy@google.com (Max Cai)
package protobuf_unittest_import;
import "google/protobuf/unittest_import_nano.proto";
option java_package = "com.google.protobuf.nano";
option java_outer_classname = "NanoMultipleImportingNonMultiple";
option java_multiple_files = true;
message MultipleImportingNonMultipleNano1 {
optional ImportMessageNano field = 1;
}
message MultipleImportingNonMultipleNano2 {
optional MultipleImportingNonMultipleNano1 nano1 = 1;
}
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