You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by br...@apache.org on 2009/03/18 04:14:57 UTC

svn commit: r755464 - /incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc

Author: bryanduxbury
Date: Wed Mar 18 03:14:56 2009
New Revision: 755464

URL: http://svn.apache.org/viewvc?rev=755464&view=rev
Log:
THRIFT-379. java: Print enum value names

This patch adds a new generated map of enum values to their respective names to each generated enum class, and then consumes that in the toString method to print the names when possible.

Modified:
    incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc

Modified: incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc?rev=755464&r1=755463&r2=755464&view=diff
==============================================================================
--- incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc (original)
+++ incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc Wed Mar 18 03:14:56 2009
@@ -177,6 +177,7 @@
   std::string function_signature(t_function* tfunction, std::string prefix="");
   std::string argument_list(t_struct* tstruct);
   std::string type_to_enum(t_type* ttype);
+  std::string get_enum_class_name(t_type* type);
 
   bool type_can_be_null(t_type* ttype) {
     ttype = get_true_type(ttype);
@@ -318,7 +319,9 @@
     "import java.util.Set;\n" +
     "import java.util.HashSet;\n" +
     "import java.util.Collections;\n" +
-    "import org.apache.thrift.IntRangeSet;\n"<< endl;
+    "import org.apache.thrift.IntRangeSet;\n" +
+    "import java.util.Map;\n" + 
+    "import java.util.HashMap;\n" << endl;
 
   f_enum <<
     "public class " << tenum->get_name() << " ";
@@ -354,7 +357,19 @@
   indent_down();
   f_enum << ");" << endl;
 
+  indent(f_enum) << "public static final Map<Integer, String> VALUES_TO_NAMES = new HashMap<Integer, String>() {{" << endl;
+
+  indent_up();
+  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {    
+    indent(f_enum) << "put(" << (*c_iter)->get_name() << ", \"" << (*c_iter)->get_name() <<"\");" << endl;
+  }
+  indent_down();
+
+  
+  indent(f_enum) << "}};" << endl;
+
   scope_down(f_enum);
+  
   f_enum.close();
 }
 
@@ -1037,13 +1052,7 @@
     t_type* type = field->get_type();
     // if field is an enum, check that its value is valid
     if (type->is_enum()){
-      string package = "";
-      t_program* program = type->get_program();
-      if (program != NULL && program != program_) {
-        package = program->get_namespace("java") + ".";
-      }
-
-      indent(out) << "if (" << generate_isset_check(field) << " && !" << package << type->get_name() << ".VALID_VALUES.contains(" << field->get_name() << ")){" << endl;
+      indent(out) << "if (" << generate_isset_check(field) << " && !" << get_enum_class_name(type) << ".VALID_VALUES.contains(" << field->get_name() << ")){" << endl;
       indent_up();
       indent(out) << "throw new TProtocolException(\"The field '" << field->get_name() << "' has been assigned the invalid value \" + " << field->get_name() << ");" << endl;
       indent_down();
@@ -1485,6 +1494,16 @@
       indent(out) << "    sb.append(Integer.toHexString(this." << field->get_name() << "[i]).length() > 1 ? Integer.toHexString(this." << field->get_name() << "[i]).substring(Integer.toHexString(this." << field->get_name() << "[i]).length() - 2).toUpperCase() : \"0\" + Integer.toHexString(this." << field->get_name() << "[i]).toUpperCase());" <<endl;
       indent(out) << "  }" << endl;
       indent(out) << "  if (this." << field->get_name() << ".length > 128) sb.append(\" ...\");" << endl;
+    } else if(field->get_type()->is_enum()) {
+      indent(out) << "String " << field->get_name() << "_name = " << get_enum_class_name(field->get_type()) << ".VALUES_TO_NAMES.get(this." << (*f_iter)->get_name() << ");"<< endl;
+      indent(out) << "if (" << field->get_name() << "_name != null) {" << endl;
+      indent(out) << "  sb.append(" << field->get_name() << "_name);" << endl;
+      indent(out) << "  sb.append(\" (\");" << endl;
+      indent(out) << "}" << endl;
+      indent(out) << "sb.append(this." << field->get_name() << ");" << endl;
+      indent(out) << "if (" << field->get_name() << "_name != null) {" << endl;
+      indent(out) << "  sb.append(\")\");" << endl;
+      indent(out) << "}" << endl;
     } else {
       indent(out) << "sb.append(this." << (*f_iter)->get_name() << ");" << endl;
     }
@@ -2968,6 +2987,14 @@
   }
 }
 
+std::string t_java_generator::get_enum_class_name(t_type* type) {
+  string package = "";
+  t_program* program = type->get_program();
+  if (program != NULL && program != program_) {
+    package = program->get_namespace("java") + ".";
+  }
+  return package + type->get_name();
+}
 
 THRIFT_REGISTER_GENERATOR(java, "Java",
 "    beans:           Generate bean-style output files.\n"