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 2010/08/21 19:48:18 UTC

svn commit: r987805 - in /incubator/thrift/trunk: compiler/cpp/src/generate/t_java_generator.cc lib/java/src/org/apache/thrift/TBase.java lib/java/test/org/apache/thrift/test/JavaBeansTest.java

Author: bryanduxbury
Date: Sat Aug 21 17:48:18 2010
New Revision: 987805

URL: http://svn.apache.org/viewvc?rev=987805&view=rev
Log:
THRIFT-693. java: Thrift compiler generated java code that throws compiler warnings about deprecated methods.

This patch removes the deprecated methods from TBase and makes some generator changes to accommodate.

Modified:
    incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc
    incubator/thrift/trunk/lib/java/src/org/apache/thrift/TBase.java
    incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/JavaBeansTest.java

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=987805&r1=987804&r2=987805&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 Sat Aug 21 17:48:18 2010
@@ -102,6 +102,7 @@ class t_java_generator : public t_oop_ge
   void generate_java_meta_data_map(std::ofstream& out, t_struct* tstruct);
   void generate_field_value_meta_data(std::ofstream& out, t_type* type);
   std::string get_java_type_string(t_type* type);
+  void generate_java_struct_field_by_id(ofstream& out, t_struct* tstruct);
   void generate_reflection_setters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
   void generate_reflection_getters(std::ostringstream& out, t_type* type, std::string field_name, std::string cap_name);
   void generate_generic_field_getters_setters(std::ofstream& out, t_struct* tstruct);
@@ -734,6 +735,10 @@ void t_java_generator::generate_java_uni
 
   f_struct << endl;
 
+  generate_java_struct_field_by_id(f_struct, tstruct);
+
+  f_struct << endl;
+
   generate_union_getters_and_setters(f_struct, tstruct);
   
   f_struct << endl;
@@ -1225,11 +1230,6 @@ void t_java_generator::generate_java_str
   indent(out) << "  return new " << tstruct->get_name() << "(this);" << endl;
   indent(out) << "}" << endl << endl;
 
-  indent(out) << "@Deprecated" << endl;
-  indent(out) << "public " << tstruct->get_name() << " clone() {" << endl;
-  indent(out) << "  return new " << tstruct->get_name() << "(this);" << endl;
-  indent(out) << "}" << endl << endl;
-
   generate_java_struct_clear(out, tstruct);
 
   generate_java_bean_boilerplate(out, tstruct);
@@ -1238,6 +1238,7 @@ void t_java_generator::generate_java_str
 
   generate_java_struct_equality(out, tstruct);
   generate_java_struct_compare_to(out, tstruct);
+  generate_java_struct_field_by_id(out, tstruct);
 
   generate_java_struct_reader(out, tstruct);
   if (is_result) {
@@ -1662,6 +1663,12 @@ void t_java_generator::generate_java_str
     endl;
 }
 
+void t_java_generator::generate_java_struct_field_by_id(ofstream& out, t_struct* tstruct) {
+  indent(out) << "public _Fields fieldForId(int fieldId) {" << endl;
+  indent(out) << "  return _Fields.findByThriftId(fieldId);" << endl;
+  indent(out) << "}" << endl << endl;
+}
+
 void t_java_generator::generate_reflection_getters(ostringstream& out, t_type* type, string field_name, string cap_name) {
   indent(out) << "case " << constant_name(field_name) << ":" << endl;
   indent_up();
@@ -1712,17 +1719,13 @@ void t_java_generator::generate_generic_
 
 
   // create the setter
-  
+
   indent(out) << "public void setFieldValue(_Fields field, Object value) {" << endl;
   indent(out) << "  switch (field) {" << endl;
   out << setter_stream.str();
   indent(out) << "  }" << endl;
   indent(out) << "}" << endl << endl;
 
-  indent(out) << "public void setFieldValue(int fieldID, Object value) {" << endl;
-  indent(out) << "  setFieldValue(_Fields.findByThriftIdOrThrow(fieldID), value);" << endl;
-  indent(out) << "}" << endl << endl;
-
   // create the getter
   indent(out) << "public Object getFieldValue(_Fields field) {" << endl;
   indent_up();
@@ -1732,10 +1735,6 @@ void t_java_generator::generate_generic_
   indent(out) << "throw new IllegalStateException();" << endl;
   indent_down();
   indent(out) << "}" << endl << endl;
-
-  indent(out) << "public Object getFieldValue(int fieldId) {" << endl;
-  indent(out) << "  return getFieldValue(_Fields.findByThriftIdOrThrow(fieldId));" << endl;
-  indent(out) << "}" << endl << endl;
 }
 
 // Creates a generic isSet method that takes the field number as argument
@@ -1747,6 +1746,10 @@ void t_java_generator::generate_generic_
   indent(out) << "/** Returns true if field corresponding to fieldID is set (has been asigned a value) and false otherwise */" << endl;
   indent(out) << "public boolean isSet(_Fields field) {" << endl;
   indent_up();
+  indent(out) << "if (field == null) {" << endl;
+  indent(out) << "  throw new IllegalArgumentException();" << endl;
+  indent(out) << "}" << endl << endl;
+
   indent(out) << "switch (field) {" << endl;
 
   for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
@@ -1761,10 +1764,6 @@ void t_java_generator::generate_generic_
   indent(out) << "throw new IllegalStateException();" << endl;
   indent_down();
   indent(out) << "}" << endl << endl;
-
-  indent(out) << "public boolean isSet(int fieldID) {" << endl;
-  indent(out) << "  return isSet(_Fields.findByThriftIdOrThrow(fieldID));" << endl;
-  indent(out) << "}" << endl << endl;
 }
 
 /**

Modified: incubator/thrift/trunk/lib/java/src/org/apache/thrift/TBase.java
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/src/org/apache/thrift/TBase.java?rev=987805&r1=987804&r2=987805&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/java/src/org/apache/thrift/TBase.java (original)
+++ incubator/thrift/trunk/lib/java/src/org/apache/thrift/TBase.java Sat Aug 21 17:48:18 2010
@@ -44,12 +44,9 @@ public interface TBase<T extends TBase, 
   public void write(TProtocol oprot) throws TException;
 
   /**
-   * Check if a field is currently set or unset.
-   *
-   * @param fieldId The field's id tag as found in the IDL.
+   * Get the F instance that corresponds to fieldId.
    */
-  @Deprecated
-  public boolean isSet(int fieldId);
+  public F fieldForId(int fieldId);
 
   /**
    * Check if a field is currently set or unset.
@@ -59,15 +56,6 @@ public interface TBase<T extends TBase, 
   public boolean isSet(F field);
 
   /**
-   * Get a field's value by id. Primitive types will be wrapped in the 
-   * appropriate "boxed" types.
-   *
-   * @param fieldId The field's id tag as found in the IDL.
-   */
-  @Deprecated
-  public Object getFieldValue(int fieldId);
-
-  /**
    * Get a field's value by field variable. Primitive types will be wrapped in 
    * the appropriate "boxed" types.
    *
@@ -76,15 +64,6 @@ public interface TBase<T extends TBase, 
   public Object getFieldValue(F field);
 
   /**
-   * Set a field's value by id. Primitive types must be "boxed" in the 
-   * appropriate object wrapper type.
-   *
-   * @param fieldId The field's id tag as found in the IDL.
-   */
-  @Deprecated
-  public void setFieldValue(int fieldId, Object value);
-
-  /**
    * Set a field's value by field variable. Primitive types must be "boxed" in
    * the appropriate object wrapper type.
    *

Modified: incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/JavaBeansTest.java
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/JavaBeansTest.java?rev=987805&r1=987804&r2=987805&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/JavaBeansTest.java (original)
+++ incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/JavaBeansTest.java Sat Aug 21 17:48:18 2010
@@ -54,7 +54,7 @@ public class JavaBeansTest {
       throw new RuntimeException("isSet method error: unset field returned as set!");
 
     for (int i = 1; i < 12; i++){
-      if (ooe.isSet(i))
+      if (ooe.isSet(ooe.fieldForId(i)))
         throw new RuntimeException("isSet method error: unset field " + i + " returned as set!");
     }
 
@@ -95,14 +95,14 @@ public class JavaBeansTest {
       throw new RuntimeException("isSet method error: set field returned as unset!");
 
     for (int i = 1; i < 12; i++){
-      if (!ooe.isSet(i))
+      if (!ooe.isSet(ooe.fieldForId(i)))
         throw new RuntimeException("isSet method error: set field " + i + " returned as unset!");
     }
 
     // Should throw exception when field doesn't exist
     boolean exceptionThrown = false;
     try{
-      if (ooe.isSet(100));
+      if (ooe.isSet(ooe.fieldForId(100)));
     } catch (IllegalArgumentException e){
       exceptionThrown = true;
     }