THRIFT-172. java: Improve toString output for binary fields

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@719720 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
David Reiss 2008-11-21 21:31:36 +00:00
parent 7f5e184785
commit 30a9360e3f
3 changed files with 92 additions and 1 deletions

View File

@ -1303,9 +1303,24 @@ void t_java_generator::generate_java_struct_tostring(ofstream& out,
indent_up();
}
t_field* field = (*f_iter);
indent(out) << "if (!first) sb.append(\", \");" << endl;
indent(out) << "sb.append(\"" << (*f_iter)->get_name() << ":\");" << endl;
indent(out) << "sb.append(this." << (*f_iter)->get_name() << ");" << endl;
if (field->get_type()->is_base_type() && ((t_base_type*)(field->get_type()))->is_binary()) {
indent(out) << "if (" << field->get_name() << " == null) { " << endl;
indent(out) << " sb.append(\"null\");" << endl;
indent(out) << "} else {" << endl;
indent(out) << " int __" << field->get_name() << "_size = Math.min(this." << field->get_name() << ".length, 128);" << endl;
indent(out) << " for (int i = 0; i < __" << field->get_name() << "_size; i++) {" << endl;
indent(out) << " if (i != 0) sb.append(\" \");" << endl;
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;
indent(out) << "}" << endl;
} else {
indent(out) << "sb.append(this." << (*f_iter)->get_name() << ");" << endl;
}
indent(out) << "first = false;" << endl;
if((*f_iter)->get_req() == t_field::T_OPTIONAL) {

View File

@ -44,6 +44,8 @@
classpath="${cpath}:${testjar}:${gen}" failonerror="true" />
<java classname="com.facebook.thrift.test.EqualityTest"
classpath="${cpath}:${testjar}:${gen}" failonerror="true" />
<java classname="com.facebook.thrift.test.ToStringTest"
classpath="${cpath}:${testjar}:${gen}" failonerror="true" />
</target>
<target name="clean">

View File

@ -0,0 +1,74 @@
package com.facebook.thrift.test;
import thrift.test.*;
/**
* @author David Reiss <dreiss@facebook.com>
*/
public class ToStringTest {
public static void main(String[] args) throws Exception {
JavaTestHelper object = new JavaTestHelper();
object.req_int = 0;
object.req_obj = "";
object.req_bin = new byte[] {
0, -1, 2, -3, 4, -5, 6, -7, 8, -9, 10, -11, 12, -13, 14, -15,
16, -17, 18, -19, 20, -21, 22, -23, 24, -25, 26, -27, 28, -29,
30, -31, 32, -33, 34, -35, 36, -37, 38, -39, 40, -41, 42, -43, 44,
-45, 46, -47, 48, -49, 50, -51, 52, -53, 54, -55, 56, -57, 58, -59,
60, -61, 62, -63, 64, -65, 66, -67, 68, -69, 70, -71, 72, -73, 74,
-75, 76, -77, 78, -79, 80, -81, 82, -83, 84, -85, 86, -87, 88, -89,
90, -91, 92, -93, 94, -95, 96, -97, 98, -99, 100, -101, 102, -103,
104, -105, 106, -107, 108, -109, 110, -111, 112, -113, 114, -115,
116, -117, 118, -119, 120, -121, 122, -123, 124, -125, 126, -127,
};
if (!object.toString().equals(
"JavaTestHelper(req_int:0, req_obj:, req_bin:"+
"00 FF 02 FD 04 FB 06 F9 08 F7 0A F5 0C F3 0E F1 10 EF 12 ED 14 "+
"EB 16 E9 18 E7 1A E5 1C E3 1E E1 20 DF 22 DD 24 DB 26 D9 28 D7 "+
"2A D5 2C D3 2E D1 30 CF 32 CD 34 CB 36 C9 38 C7 3A C5 3C C3 3E "+
"C1 40 BF 42 BD 44 BB 46 B9 48 B7 4A B5 4C B3 4E B1 50 AF 52 AD "+
"54 AB 56 A9 58 A7 5A A5 5C A3 5E A1 60 9F 62 9D 64 9B 66 99 68 "+
"97 6A 95 6C 93 6E 91 70 8F 72 8D 74 8B 76 89 78 87 7A 85 7C 83 "+
"7E 81)")) {
throw new RuntimeException();
}
object.req_bin = new byte[] {
0, -1, 2, -3, 4, -5, 6, -7, 8, -9, 10, -11, 12, -13, 14, -15,
16, -17, 18, -19, 20, -21, 22, -23, 24, -25, 26, -27, 28, -29,
30, -31, 32, -33, 34, -35, 36, -37, 38, -39, 40, -41, 42, -43, 44,
-45, 46, -47, 48, -49, 50, -51, 52, -53, 54, -55, 56, -57, 58, -59,
60, -61, 62, -63, 64, -65, 66, -67, 68, -69, 70, -71, 72, -73, 74,
-75, 76, -77, 78, -79, 80, -81, 82, -83, 84, -85, 86, -87, 88, -89,
90, -91, 92, -93, 94, -95, 96, -97, 98, -99, 100, -101, 102, -103,
104, -105, 106, -107, 108, -109, 110, -111, 112, -113, 114, -115,
116, -117, 118, -119, 120, -121, 122, -123, 124, -125, 126, -127,
0,
};
if (!object.toString().equals(
"JavaTestHelper(req_int:0, req_obj:, req_bin:"+
"00 FF 02 FD 04 FB 06 F9 08 F7 0A F5 0C F3 0E F1 10 EF 12 ED 14 "+
"EB 16 E9 18 E7 1A E5 1C E3 1E E1 20 DF 22 DD 24 DB 26 D9 28 D7 "+
"2A D5 2C D3 2E D1 30 CF 32 CD 34 CB 36 C9 38 C7 3A C5 3C C3 3E "+
"C1 40 BF 42 BD 44 BB 46 B9 48 B7 4A B5 4C B3 4E B1 50 AF 52 AD "+
"54 AB 56 A9 58 A7 5A A5 5C A3 5E A1 60 9F 62 9D 64 9B 66 99 68 "+
"97 6A 95 6C 93 6E 91 70 8F 72 8D 74 8B 76 89 78 87 7A 85 7C 83 "+
"7E 81 ...)")) {
throw new RuntimeException();
}
object.req_bin = new byte[] {};
object.__isset.opt_bin = true;
if (!object.toString().equals(
"JavaTestHelper(req_int:0, req_obj:, req_bin:, opt_bin:null)")) {
throw new RuntimeException();
}
}
}