THRIFT-1008. java: byte[] accessors throw NPE on unset field
Further extra null check in accessors that use TBaseHelper.rightSize().
git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1038850 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 b622322..490e8de 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -807,7 +807,8 @@
if (type->is_base_type() && ((t_base_type*)type)->is_binary()) {
indent(out) << "public byte[] get" << cap_name << "() {" << endl;
indent(out) << " set" << cap_name << "(TBaseHelper.rightSize(buffer" << get_cap_name("for") << cap_name << "()));" << endl;
- indent(out) << " return buffer" << get_cap_name("for") << cap_name << "().array();" << endl;
+ indent(out) << " ByteBuffer b = buffer" << get_cap_name("for") << cap_name << "();" << endl;
+ indent(out) << " return b == null ? null : b.array();" << endl;
indent(out) << "}" << endl;
out << endl;
@@ -1889,7 +1890,7 @@
if (type->is_base_type() && ((t_base_type*)type)->is_binary()) {
indent(out) << "public byte[] get" << cap_name << "() {" << endl;
indent(out) << " set" << cap_name << "(TBaseHelper.rightSize(" << field_name << "));" << endl;
- indent(out) << " return " << field_name << ".array();" << endl;
+ indent(out) << " return " << field_name << " == null ? null : " << field_name << ".array();" << endl;
indent(out) << "}" << endl << endl;
indent(out) << "public ByteBuffer buffer" << get_cap_name("for") << cap_name << "() {" << endl;
@@ -1920,7 +1921,7 @@
out << type_name(tstruct);
}
out << " set" << cap_name << "(byte[] " << field_name << ") {" << endl;
- indent(out) << " set" << cap_name << "(ByteBuffer.wrap(" << field_name << "));" << endl;
+ indent(out) << " set" << cap_name << "(" << field_name << " == null ? (ByteBuffer)null : ByteBuffer.wrap(" << field_name << "));" << endl;
if (!bean_style_) {
indent(out) << " return this;" << endl;
}
diff --git a/lib/java/test/org/apache/thrift/TestStruct.java b/lib/java/test/org/apache/thrift/TestStruct.java
index 61b384d..52e4017 100644
--- a/lib/java/test/org/apache/thrift/TestStruct.java
+++ b/lib/java/test/org/apache/thrift/TestStruct.java
@@ -301,4 +301,12 @@
assertEquals("JavaTestHelper(req_int:0, req_obj:, req_bin:)",
object.toString());
}
+
+ public void testBytesBufferFeatures() throws Exception {
+ JavaTestHelper o = new JavaTestHelper();
+ o.setReq_bin((ByteBuffer)null);
+ assertNull(o.getReq_bin());
+ o.setReq_bin((byte[])null);
+ assertNull(o.getReq_bin());
+ }
}