You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by br...@apache.org on 2011/01/26 23:46:04 UTC

svn commit: r1063911 - in /thrift/branches/0.6.x: CHANGES compiler/cpp/src/generate/t_java_generator.cc lib/java/test/org/apache/thrift/TestStruct.java lib/java/test/org/apache/thrift/TestTUnion.java

Author: bryanduxbury
Date: Wed Jan 26 22:46:04 2011
New Revision: 1063911

URL: http://svn.apache.org/viewvc?rev=1063911&view=rev
Log:
THRIFT-1038. java: Generated Java code for structures containing binary fields (or collections thereof) are not serializable (in the Java sense) even though they implement java.io.Serializable

This patch causes Java Serialized Thrift structs to be serialized onto the stream via the Compact Protocol.

Patch: Mathias Herberts

Modified:
    thrift/branches/0.6.x/CHANGES
    thrift/branches/0.6.x/compiler/cpp/src/generate/t_java_generator.cc
    thrift/branches/0.6.x/lib/java/test/org/apache/thrift/TestStruct.java
    thrift/branches/0.6.x/lib/java/test/org/apache/thrift/TestTUnion.java

Modified: thrift/branches/0.6.x/CHANGES
URL: http://svn.apache.org/viewvc/thrift/branches/0.6.x/CHANGES?rev=1063911&r1=1063910&r2=1063911&view=diff
==============================================================================
--- thrift/branches/0.6.x/CHANGES (original)
+++ thrift/branches/0.6.x/CHANGES Wed Jan 26 22:46:04 2011
@@ -26,6 +26,7 @@ THRIFT-950    Haskell              Haske
 THRIFT-1009   Java                 TUnion does not correctly deep copy a ByteBuffer (Takashi Yonebayashi)
 THRIFT-1013   Java                 generated java code may have name clashes with thrift library (Peter Schuller)
 THRIFT-1015   Java                 TUnion does not handle ByteBuffer in toString (Takashi Yonebayashi)
+THRIFT-1038   Java                 Generated Java code for structures containing binary fields (or collections thereof) are not serializable (in the Java sense) even though they implement java.io.Serializable (Mathias Herberts)
 THRIFT-106    Java                 TSSLServerSocket (Nirmal Ranganathan)
 THRIFT-377    Java                 TFileTransport port in Java (Joydeep Sen Sarma)
 THRIFT-745    Java                 Make it easier to instantiate servers (Bryan Duxbury)

Modified: thrift/branches/0.6.x/compiler/cpp/src/generate/t_java_generator.cc
URL: http://svn.apache.org/viewvc/thrift/branches/0.6.x/compiler/cpp/src/generate/t_java_generator.cc?rev=1063911&r1=1063910&r2=1063911&view=diff
==============================================================================
--- thrift/branches/0.6.x/compiler/cpp/src/generate/t_java_generator.cc (original)
+++ thrift/branches/0.6.x/compiler/cpp/src/generate/t_java_generator.cc Wed Jan 26 22:46:04 2011
@@ -100,6 +100,8 @@ class t_java_generator : public t_oop_ge
   void generate_java_struct_writer(std::ofstream& out, t_struct* tstruct);
   void generate_java_struct_tostring(std::ofstream& out, t_struct* tstruct);
   void generate_java_struct_clear(std::ofstream& out, t_struct* tstruct);
+  void generate_java_struct_write_object(std::ofstream& out, t_struct* tstruct);
+  void generate_java_struct_read_object(std::ofstream& out, t_struct* tstruct);
   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);
@@ -725,6 +727,14 @@ void t_java_generator::generate_java_uni
 
   f_struct << endl;
 
+  generate_java_struct_write_object(f_struct, tstruct);
+
+  f_struct << endl;
+
+  generate_java_struct_read_object(f_struct, tstruct);
+
+  f_struct << endl;
+
   scope_down(f_struct);
 
   f_struct.close();
@@ -1261,6 +1271,10 @@ void t_java_generator::generate_java_str
   }
   generate_java_struct_tostring(out, tstruct);
   generate_java_validator(out, tstruct);
+
+  generate_java_struct_write_object(out, tstruct);
+  generate_java_struct_read_object(out, tstruct);
+
   scope_down(out);
   out << endl;
 }
@@ -3957,6 +3971,32 @@ void t_java_generator::generate_java_str
   indent(out) << "}" << endl << endl;
 }
 
+// generates java method to serialize (in the Java sense) the object
+void t_java_generator::generate_java_struct_write_object(ofstream& out, t_struct* tstruct) {
+  indent(out) << "private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {" << endl;
+  indent(out) << "  try {" << endl;
+  indent(out) << "    write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out)));" << endl;
+  indent(out) << "  } catch (org.apache.thrift.TException te) {" << endl;
+  indent(out) << "    throw new java.io.IOException(te);" << endl;
+  indent(out) << "  }" << endl;
+  indent(out) << "}" << endl << endl;
+}
+
+// generates java method to serialize (in the Java sense) the object
+void t_java_generator::generate_java_struct_read_object(ofstream& out, t_struct* tstruct) {
+  indent(out) << "private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {" << endl;
+  indent(out) << "  try {" << endl;
+  if (!tstruct->is_union() && has_bit_vector(tstruct)) {
+    indent(out) << "    // it doesn't seem like you should have to do this, but java serialization is wacky, and doesn't call the default constructor." << endl;
+    indent(out) << "    __isset_bit_vector = new BitSet(1);" << endl;
+  }
+  indent(out) << "    read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in)));" << endl;
+  indent(out) << "  } catch (org.apache.thrift.TException te) {" << endl;
+  indent(out) << "    throw new java.io.IOException(te);" << endl;
+  indent(out) << "  }" << endl;
+  indent(out) << "}" << endl << endl;
+}
+
 THRIFT_REGISTER_GENERATOR(java, "Java",
 "    beans:           Members will be private, and setter methods will return void.\n"
 "    private-members: Members will be private, but setter methods will return 'this' like usual.\n"

Modified: thrift/branches/0.6.x/lib/java/test/org/apache/thrift/TestStruct.java
URL: http://svn.apache.org/viewvc/thrift/branches/0.6.x/lib/java/test/org/apache/thrift/TestStruct.java?rev=1063911&r1=1063910&r2=1063911&view=diff
==============================================================================
--- thrift/branches/0.6.x/lib/java/test/org/apache/thrift/TestStruct.java (original)
+++ thrift/branches/0.6.x/lib/java/test/org/apache/thrift/TestStruct.java Wed Jan 26 22:46:04 2011
@@ -18,6 +18,11 @@
  */
 package org.apache.thrift;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
 import java.nio.ByteBuffer;
 import java.util.HashMap;
 import java.util.Map;
@@ -309,4 +314,22 @@ public class TestStruct extends TestCase
     o.setReq_bin((byte[])null);
     assertNull(o.getReq_bin());
   }
+
+  public void testJavaSerializable() throws Exception {
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    ObjectOutputStream oos = new ObjectOutputStream(baos);
+    
+    OneOfEach ooe = Fixtures.oneOfEach;
+
+    // Serialize ooe the Java way...
+    oos.writeObject(ooe);
+    byte[] serialized = baos.toByteArray();
+
+    // Attempt to deserialize it
+    ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
+    ObjectInputStream ois = new ObjectInputStream(bais);
+    OneOfEach ooe2 = (OneOfEach) ois.readObject();
+
+    assertEquals(ooe, ooe2);
+  }
 }

Modified: thrift/branches/0.6.x/lib/java/test/org/apache/thrift/TestTUnion.java
URL: http://svn.apache.org/viewvc/thrift/branches/0.6.x/lib/java/test/org/apache/thrift/TestTUnion.java?rev=1063911&r1=1063910&r2=1063911&view=diff
==============================================================================
--- thrift/branches/0.6.x/lib/java/test/org/apache/thrift/TestTUnion.java (original)
+++ thrift/branches/0.6.x/lib/java/test/org/apache/thrift/TestTUnion.java Wed Jan 26 22:46:04 2011
@@ -18,6 +18,10 @@
  */
 package org.apache.thrift;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -200,4 +204,22 @@ public class TestTUnion extends TestCase
     String expectedString = "<ComparableUnion binary_field:01 02 03>";
     assertEquals(expectedString, cu.toString());
   }
+
+  public void testJavaSerializable() throws Exception {
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    ObjectOutputStream oos = new ObjectOutputStream(baos);
+    
+    TestUnion tu = TestUnion.string_field("string");
+
+    // Serialize tu the Java way...
+    oos.writeObject(tu);
+    byte[] serialized = baos.toByteArray();
+
+    // Attempt to deserialize it
+    ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
+    ObjectInputStream ois = new ObjectInputStream(bais);
+    TestUnion tu2 = (TestUnion) ois.readObject();
+
+    assertEquals(tu, tu2);
+  }
 }