THRIFT-2368 New option: reuse-objects for Java generator
Patch: adam-aph
diff --git a/lib/java/build.xml b/lib/java/build.xml
index 5545b8c..66ffb1d 100755
--- a/lib/java/build.xml
+++ b/lib/java/build.xml
@@ -47,6 +47,7 @@
 
   <property name="gen" location="gen-java"/>
   <property name="genbean" location="gen-javabean"/>
+  <property name="genreuse" location="gen-javareuse"/>
 
   <property name="jar.file" location="${build.dir}/${final.name}.jar"/>
   <property name="test.jar.file" location="${build.dir}/${final.name}-test.jar"/>
@@ -75,6 +76,7 @@
     <pathelement location="${build.test.dir}"/>
     <pathelement location="${jar.file}"/>
     <pathelement location="${test.jar.file}"/>
+
   </path>
 
   <!-- Tasks --> 
@@ -123,7 +125,21 @@
         <attribute name="Bundle-Description" value="Apache Thrift library"/>
         <attribute name="Bundle-License" value="${license}"/>
         <attribute name="Bundle-ActivationPolicy" value="lazy"/>
-        <attribute name="Export-Package" value="${thrift.groupid};version=${version}"/>
+        <attribute name="Export-Package" value="${thrift.groupid}.async;uses:=&quot;
+${thrift.groupid}.protocol,${thrift.groupid}.transport,org.slf4j,${thrift.groupid}&quot;;version=&quot;${version}&quot;,
+${thrift.groupid}.protocol;uses:=&quot;${thrift.groupid}.transport,${thrift.groupid},${thrift.groupid}.scheme&quot;
+;version=&quot;${version}&quot;,${thrift.groupid}.server;uses:=&quot;${thrift.groupid}.transport,${thrift.groupid}.protocol,
+${thrift.groupid},org.slf4j,javax.servlet,javax.servlet.http&quot;;version=&quot;${version}&quot;,${thrift.groupid}.transport;
+uses:=&quot;${thrift.groupid}.protocol,${thrift.groupid},org.apache.http.client,org.apache.http.params,org.apache.http.entity,
+org.apache.http.client.methods,org.apache.http,org.slf4j,javax.net.ssl,javax.net,javax.security.sasl,
+javax.security.auth.callback&quot;;version=&quot;${version}&quot;,${thrift.groupid};uses:=&quot;${thrift.groupid}.protocol,
+${thrift.groupid}.async,${thrift.groupid}.server,${thrift.groupid}.transport,org.slf4j,org.apache.log4j,
+${thrift.groupid}.scheme&quot;;version=&quot;${version}&quot;,${thrift.groupid}.meta_data;uses:=&quot;${thrift.groupid}&quot;
+;version=&quot;${version}&quot;,${thrift.groupid}.scheme;uses:=&quot;${thrift.groupid}.protocol,
+${thrift.groupid}&quot;;version=&quot;${version}&quot;"/>
+       <attribute name="Import-Package" value="javax.net,javax.net.ssl,javax.security.auth.callback,
+javax.security.sasl,javax.servlet;resolution:=optional,javax.servlet.http;resolution:=optional,
+resolution:=optional,org.slf4j;resolution:=optional;version=&quot;[1.4,2)&quot;"/>
       </manifest>
       <fileset dir="${build.dir}">
         <include name="org/apache/thrift/**/*.class"/>
@@ -150,6 +166,7 @@
     <delete dir="${build.dir}"/>
     <delete dir="${gen}"/>
     <delete dir="${genbean}"/>
+    <delete dir="${genreuse}"/>
   </target>
 
   <target name="optional-generate" unless="no-gen-thrift"> 
@@ -161,11 +178,15 @@
     <javac debug="true" srcdir="${gen}" destdir="${build.test.dir}" classpathref="test.classpath" includeantruntime="false" encoding="UTF-8">
       <compilerarg value="-Xlint:deprecation"/>
       <!--<compilerarg value="-Xlint:unchecked"/>-->
-    </javac>  
+    </javac>
     <javac debug="true" srcdir="${genbean}" destdir="${build.test.dir}" classpathref="test.classpath" includeantruntime="false" encoding="UTF-8">
       <compilerarg value="-Xlint:deprecation"/>
       <!--<compilerarg value="-Xlint:unchecked"/>-->
-    </javac>  
+    </javac>
+    <javac debug="true" srcdir="${genreuse}" destdir="${build.test.dir}" classpathref="test.classpath" includeantruntime="false" encoding="UTF-8">
+      <compilerarg value="-Xlint:deprecation"/>
+      <!--<compilerarg value="-Xlint:unchecked"/>-->
+    </javac>
     <javac debug="true" srcdir="${src.test}" destdir="${build.test.dir}" classpathref="test.classpath" includeantruntime="false" encoding="UTF-8">
       <compilerarg value="-Xlint:deprecation"/>
       <!--<compilerarg value="-Xlint:unchecked"/>-->
@@ -254,6 +275,12 @@
     <exec executable="../../compiler/cpp/thrift" failonerror="true">
       <arg line="--gen java:hashcode ${test.thrift.home}/ManyOptionals.thrift"/>
     </exec>
+    <exec executable="mkdir" failonerror="true">
+      <arg line="-p ${genreuse}"/>
+    </exec>
+    <exec executable="../../compiler/cpp/thrift" failonerror="true">
+      <arg line="--gen java:reuse-objects -out ${genreuse} ${test.thrift.home}/ReuseObjects.thrift"/>
+    </exec>
   </target>
 
   <target name="proxy" if="proxy.enabled">
diff --git a/lib/java/test/org/apache/thrift/TestReuse.java b/lib/java/test/org/apache/thrift/TestReuse.java
new file mode 100644
index 0000000..db16c74
--- /dev/null
+++ b/lib/java/test/org/apache/thrift/TestReuse.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.thrift;
+
+import java.util.HashSet;
+
+import junit.framework.TestCase;
+
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TType;
+
+import thrift.test.Reuse;
+
+// Tests reusing objects for deserialization.
+//
+public class TestReuse extends TestStruct {
+
+  public void testReuseObject() throws Exception {
+    TSerializer   binarySerializer   = new   TSerializer(new TBinaryProtocol.Factory());
+    TDeserializer binaryDeserializer = new TDeserializer(new TBinaryProtocol.Factory());
+
+    Reuse ru1 = new Reuse();
+    HashSet<String> hs1 = new HashSet<String>();
+    byte[] serBytes;    
+    String st1 = new String("string1");
+    String st2 = new String("string2");
+
+    ru1.setVal1(11);
+    ru1.setVal2(hs1);
+    ru1.addToVal2(st1);
+    
+    serBytes = binarySerializer.serialize(ru1);
+
+    // update hash set after serialization
+    hs1.add(st2);
+
+    binaryDeserializer.deserialize(ru1, serBytes);
+   
+    assertTrue( ru1.getVal2() == hs1 );
+    assertTrue( hs1.size() == 2 );
+  }
+
+}