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
diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc
index 0248cf7..93985f7 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -102,6 +102,7 @@
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 @@
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 @@
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 @@
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 @@
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 @@
// 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 @@
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 @@
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 @@
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 6459113..ee32cca 100644
--- a/lib/java/src/org/apache/thrift/TBase.java
+++ b/lib/java/src/org/apache/thrift/TBase.java
@@ -44,12 +44,9 @@
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 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 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 3f8d22a..6a2a0ed 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 @@
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 @@
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;
}