Skip to content

Commit

Permalink
THRIFT-663. java: JavaBean code generator produces incorrect setter m…
Browse files Browse the repository at this point in the history
…ethods

This patch causes the beans option to suppress the builder-style setter methods. It also adds a new 'private-members' option that leaves the builder-style methods, but makes the actual instance variables private.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@930474 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Bryan Duxbury committed Apr 3, 2010
1 parent 9c56b71 commit e31c3f0
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions compiler/cpp/src/generate/t_java_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class t_java_generator : public t_oop_generator {
iter = parsed_options.find("beans");
bean_style_ = (iter != parsed_options.end());

iter = parsed_options.find("private-members");
private_members_ = (iter != parsed_options.end());

iter = parsed_options.find("nocamel");
nocamel_style_ = (iter != parsed_options.end());

Expand Down Expand Up @@ -243,6 +246,7 @@ class t_java_generator : public t_oop_generator {
std::string package_dir_;

bool bean_style_;
bool private_members_;
bool nocamel_style_;
bool gen_hash_code_;

Expand Down Expand Up @@ -1089,7 +1093,7 @@ void t_java_generator::generate_java_struct_definition(ofstream &out,
out << endl;

for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
if (bean_style_) {
if (bean_style_ || private_members_) {
indent(out) << "private ";
} else {
generate_java_doc(out, *m_iter);
Expand Down Expand Up @@ -1865,13 +1869,20 @@ void t_java_generator::generate_java_bean_boilerplate(ofstream& out,

// Simple setter
generate_java_doc(out, field);
indent(out) << "public " << type_name(tstruct) << " set" << cap_name << "(" << type_name(type) <<
" " << field_name << ") {" << endl;
indent(out) << "public ";
if (bean_style_) {
out << "void";
} else {
out << type_name(tstruct);
}
out << " set" << cap_name << "(" << type_name(type) << " " << field_name << ") {" << endl;
indent_up();
indent(out) << "this." << field_name << " = " << field_name << ";" <<
endl;
generate_isset_set(out, field);
indent(out) << "return this;" << endl;
if (!bean_style_) {
indent(out) << "return this;" << endl;
}

indent_down();
indent(out) << "}" << endl << endl;
Expand Down Expand Up @@ -3608,7 +3619,8 @@ bool t_java_generator::has_bit_vector(t_struct* tstruct) {
}

THRIFT_REGISTER_GENERATOR(java, "Java",
" beans: Generate bean-style output files.\n"
" beans: Members will be private, and setter methods will return void.\n"
" private-members: Members will be private, but setter methods will return 'this' like usual.\n"
" nocamel: Do not use CamelCase field accessors with beans.\n"
" hashcode: Generate quality hashCode methods.\n"
);
Expand Down

0 comments on commit e31c3f0

Please sign in to comment.