From 66467a7779b980bf8e338ab3e9407fea64a6743f Mon Sep 17 00:00:00 2001 From: Bryan Duxbury Date: Sat, 21 Aug 2010 17:48:18 +0000 Subject: [PATCH] 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. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@987805 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_java_generator.cc | 35 +++++++++---------- lib/java/src/org/apache/thrift/TBase.java | 25 ++----------- .../org/apache/thrift/test/JavaBeansTest.java | 6 ++-- 3 files changed, 22 insertions(+), 44 deletions(-) diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc index 0248cf78..93985f7e 100644 --- a/compiler/cpp/src/generate/t_java_generator.cc +++ b/compiler/cpp/src/generate/t_java_generator.cc @@ -102,6 +102,7 @@ class t_java_generator : public t_oop_generator { 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_union(t_struct* tstruct) { 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_struct_definition(ofstream &out, 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_struct_definition(ofstream &out, 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_struct_result_writer(ofstream& out, 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_field_getters_setters(std::ofstream& out // 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_field_getters_setters(std::ofstream& out 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_isset_method(std::ofstream& out, t_struc 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_isset_method(std::ofstream& out, t_struc 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; } /** diff --git a/lib/java/src/org/apache/thrift/TBase.java b/lib/java/src/org/apache/thrift/TBase.java index 64591131..ee32ccaa 100644 --- a/lib/java/src/org/apache/thrift/TBase.java +++ b/lib/java/src/org/apache/thrift/TBase.java @@ -44,12 +44,9 @@ public interface TBase extends Comparab 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. @@ -58,15 +55,6 @@ public interface TBase extends Comparab */ 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. @@ -75,15 +63,6 @@ public interface TBase extends Comparab */ 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. diff --git a/lib/java/test/org/apache/thrift/test/JavaBeansTest.java b/lib/java/test/org/apache/thrift/test/JavaBeansTest.java index 3f8d22a0..6a2a0ed0 100644 --- a/lib/java/test/org/apache/thrift/test/JavaBeansTest.java +++ b/lib/java/test/org/apache/thrift/test/JavaBeansTest.java @@ -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; } -- 2.17.1