You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ns...@apache.org on 2016/01/02 18:59:10 UTC

[8/9] thrift git commit: THRIFT-3486 - Java generated `getFieldValue` is incompatible with `setFieldValue` for binary values Client: Java Patch: BCG

THRIFT-3486 - Java generated `getFieldValue` is incompatible with `setFieldValue` for binary values
Client: Java
Patch: BCG

This closes #743


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/2aaae8a6
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/2aaae8a6
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/2aaae8a6

Branch: refs/heads/master
Commit: 2aaae8a604c9a3f000926250a8446b7b850355c7
Parents: bf8f7b4
Author: BCG <bg...@users.noreply.github.com>
Authored: Sun Dec 13 16:05:13 2015 -0500
Committer: Nobuaki Sukegawa <ns...@apache.org>
Committed: Sun Jan 3 02:55:16 2016 +0900

----------------------------------------------------------------------
 compiler/cpp/src/generate/t_java_generator.cc   | 11 +++++++
 lib/java/test/org/apache/thrift/TestStruct.java | 30 +++++++++++++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/2aaae8a6/compiler/cpp/src/generate/t_java_generator.cc
----------------------------------------------------------------------
diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc
index 9080368..f711f2d 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -2064,12 +2064,23 @@ void t_java_generator::generate_reflection_setters(ostringstream& out,
                                                    t_type* type,
                                                    string field_name,
                                                    string cap_name) {
+  const bool is_binary = type->is_base_type() && ((t_base_type*)type)->is_binary();
   indent(out) << "case " << constant_name(field_name) << ":" << endl;
   indent_up();
   indent(out) << "if (value == null) {" << endl;
   indent(out) << "  unset" << get_cap_name(field_name) << "();" << endl;
   indent(out) << "} else {" << endl;
+  if (is_binary) {
+    indent_up();
+    indent(out) << "if (value instanceof byte[]) {" << endl;
+    indent(out) << "  set" << cap_name << "((byte[])value);" << endl;
+    indent(out) << "} else {" << endl;
+  }
   indent(out) << "  set" << cap_name << "((" << type_name(type, true, false) << ")value);" << endl;
+  if (is_binary) {
+    indent(out) << "}" << endl;
+    indent_down();
+  }
   indent(out) << "}" << endl;
   indent(out) << "break;" << endl << endl;
 

http://git-wip-us.apache.org/repos/asf/thrift/blob/2aaae8a6/lib/java/test/org/apache/thrift/TestStruct.java
----------------------------------------------------------------------
diff --git a/lib/java/test/org/apache/thrift/TestStruct.java b/lib/java/test/org/apache/thrift/TestStruct.java
index b0dffc8..3379ed1 100644
--- a/lib/java/test/org/apache/thrift/TestStruct.java
+++ b/lib/java/test/org/apache/thrift/TestStruct.java
@@ -308,12 +308,40 @@ public class TestStruct extends TestCase {
         object.toString());
   }
 
+  private static void assertArrayEquals(byte[] expected, byte[] actual) {
+    if (!java.util.Arrays.equals(expected, actual)) {
+      fail("Expected byte array did not match actual.");
+    }
+  }
+
   public void testBytesBufferFeatures() throws Exception {
-    JavaTestHelper o = new JavaTestHelper();
+    
+    final String testString = "testBytesBufferFeatures";
+    final JavaTestHelper o = new JavaTestHelper();
+
     o.setReq_bin((ByteBuffer)null);
     assertNull(o.getReq_bin());
+
+    o.setReq_bin(ByteBuffer.wrap(testString.getBytes()));
+    assertArrayEquals(testString.getBytes(), o.getReq_bin());
+
     o.setReq_bin((byte[])null);
     assertNull(o.getReq_bin());
+
+    o.setReq_bin(testString.getBytes());
+    assertArrayEquals(testString.getBytes(), o.getReq_bin());
+
+    o.setFieldValue(JavaTestHelper._Fields.REQ_BIN, null);
+    assertNull(o.getReq_bin());
+
+    o.setFieldValue(JavaTestHelper._Fields.REQ_BIN, testString.getBytes());
+    assertArrayEquals(testString.getBytes(), o.getReq_bin());
+
+    o.setFieldValue(JavaTestHelper._Fields.REQ_BIN, null);
+    assertNull(o.getReq_bin());
+
+    o.setFieldValue(JavaTestHelper._Fields.REQ_BIN, ByteBuffer.wrap(testString.getBytes()));
+    assertArrayEquals(testString.getBytes(), o.getReq_bin());
   }
 
   public void testJavaSerializable() throws Exception {