Thrift generation for Java

Summary: Java works, benchmark roundtrip at around 3ms, so right in between C++ and PHP


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664775 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/test/java/build.xml b/test/java/build.xml
index 80a73d7..d7d712a 100644
--- a/test/java/build.xml
+++ b/test/java/build.xml
@@ -14,7 +14,7 @@
 
   <target name="generate">
     <exec executable="thrift">
-      <arg line="-java ../ThriftTest.thrift" />
+      <arg line="--java ../ThriftTest.thrift" />
     </exec>
   </target>
 
@@ -23,6 +23,11 @@
     <javac srcdir="${src}" destdir="${build}" classpath="${cpath}:${gen}" />
   </target>
 
+  <target name="compileonly">
+    <javac srcdir="${gen}" destdir="${build}" classpath="${cpath}" />
+    <javac srcdir="${src}" destdir="${build}" classpath="${cpath}:${gen}" />
+  </target>
+
   <target name="test" depends="compile">
     <jar jarfile="thrifttest.jar" basedir="${build}"/>
   </target>
diff --git a/test/java/src/TestClient.java b/test/java/src/TestClient.java
index cbcd802..686920c 100644
--- a/test/java/src/TestClient.java
+++ b/test/java/src/TestClient.java
@@ -1,11 +1,11 @@
 package com.facebook.thrift.test;
 
-import ThriftTest.*;
-import com.facebook.thrift.types.*;
+// Generated code
+import thrift.test.*;
+
 import com.facebook.thrift.transport.TSocket;
 import com.facebook.thrift.transport.TTransportException;
 import com.facebook.thrift.protocol.TBinaryProtocol;
-import com.facebook.thrift.protocol.TString;
 
 import java.util.HashMap;
 import java.util.HashSet;
@@ -39,8 +39,8 @@
         new TSocket(host, port);
       TBinaryProtocol binaryProtocol =
         new TBinaryProtocol();
-      ThriftTestClient testClient =
-        new ThriftTestClient(tSocket, binaryProtocol);
+      ThriftTest.Client testClient =
+        new ThriftTest.Client(tSocket, binaryProtocol);
 
       for (int test = 0; test < numTests; ++test) {
 
@@ -69,154 +69,154 @@
          * STRING TEST
          */
         System.out.print("testString(\"Test\")");
-        TString s = testClient.testString(new TString("Test"));
-        System.out.print(" = \"" + s.value + "\"\n");
-   
+        String s = testClient.testString("Test");
+        System.out.print(" = \"" + s + "\"\n");
+
         /**
          * BYTE TEST
          */
         System.out.print("testByte(1)");
-        UInt8 u8 = testClient.testByte(new UInt8((short)1));
-        System.out.print(" = " + u8.get() + "\n");
+        byte i8 = testClient.testByte((byte)1);
+        System.out.print(" = " + i8 + "\n");
   
         /**
          * I32 TEST
          */
         System.out.print("testI32(-1)");
-        Int32 i32 = testClient.testI32(new Int32(-1));
-        System.out.print(" = " + i32.get() + "\n");
+        int i32 = testClient.testI32(-1);
+        System.out.print(" = " + i32 + "\n");
 
         /**
          * I64 TEST
          */
         System.out.print("testI64(-34359738368)");
-        Int64 i64 = testClient.testI64(new Int64(-34359738368L));
-        System.out.print(" = " + i64.get() + "\n");
+        long i64 = testClient.testI64(-34359738368L);
+        System.out.print(" = " + i64 + "\n");
 
         /**
          * STRUCT TEST
          */
         System.out.print("testStruct({\"Zero\", 1, -3, -5})");
         Xtruct out = new Xtruct();
-        out.string_thing.value = "Zero";
-        out.byte_thing.set((short)1);
-        out.i32_thing.set(-3);
-        out.i64_thing.set(-5);
+        out.string_thing = "Zero";
+        out.byte_thing = (byte) 1;
+        out.i32_thing = -3;
+        out.i64_thing = -5;
         Xtruct in = testClient.testStruct(out);
         System.out.print(" = {" +
-                         "\"" + in.string_thing.value + "\", " +
-                         in.byte_thing.get() + ", " +
-                         in.i32_thing.get() + ", " +
-                         in.i64_thing.get() + "}\n");
+                         "\"" + in.string_thing + "\", " +
+                         in.byte_thing + ", " +
+                         in.i32_thing + ", " +
+                         in.i64_thing + "}\n");
 
         /**
          * NESTED STRUCT TEST
          */
         System.out.print("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
         Xtruct2 out2 = new Xtruct2();
-        out2.byte_thing.set((short)1);
+        out2.byte_thing = (short)1;
         out2.struct_thing = out;
-        out2.i32_thing.set(5);
+        out2.i32_thing = 5;
         Xtruct2 in2 = testClient.testNest(out2);
         in = in2.struct_thing;
         System.out.print(" = {" +
-                         in2.byte_thing.get() + ", {" +
-                         "\"" + in.string_thing.value + "\", " +
-                         in.byte_thing.get() + ", " +
-                         in.i32_thing.get() + ", " +
-                         in.i64_thing.get() + "}, " +
-                         in2.i32_thing.get() + "}\n");
+                         in2.byte_thing + ", {" +
+                         "\"" + in.string_thing + "\", " +
+                         in.byte_thing + ", " +
+                         in.i32_thing + ", " +
+                         in.i64_thing + "}, " +
+                         in2.i32_thing + "}\n");
 
         /**
          * MAP TEST
          */
-        HashMap<Int32,Int32> mapout = new HashMap<Int32,Int32>();
+        HashMap<Integer,Integer> mapout = new HashMap<Integer,Integer>();
         for (int i = 0; i < 5; ++i) {
-          mapout.put(new Int32(i), new Int32(i-10));
+          mapout.put(i, i-10);
         }
         System.out.print("testMap({");
         boolean first = true;
-        for (Int32 key : mapout.keySet()) {
+        for (int key : mapout.keySet()) {
           if (first) {
             first = false;
           } else {
             System.out.print(", ");
           }
-          System.out.print(key.get() + " => " + mapout.get(key).get());
+          System.out.print(key + " => " + mapout.get(key));
         }
         System.out.print("})");
-        HashMap<Int32,Int32> mapin = testClient.testMap(mapout);
+        HashMap<Integer,Integer> mapin = testClient.testMap(mapout);
         System.out.print(" = {");
         first = true;
-        for (Int32 key : mapin.keySet()) {
+        for (int key : mapin.keySet()) {
           if (first) {
             first = false;
           } else {
             System.out.print(", ");
           }
-          System.out.print(key.get() + " => " + mapout.get(key).get());
+          System.out.print(key + " => " + mapout.get(key));
         }
         System.out.print("}\n");
 
         /**
          * SET TEST
          */
-        HashSet<Int32> setout = new HashSet<Int32>();
+        HashSet<Integer> setout = new HashSet<Integer>();
         for (int i = -2; i < 3; ++i) {
-          setout.add(new Int32(i));
+          setout.add(i);
         }
         System.out.print("testSet({");
         first = true;
-        for (Int32 elem : setout) {
+        for (int elem : setout) {
           if (first) {
             first = false;
           } else {
             System.out.print(", ");
           }
-          System.out.print(elem.get());
+          System.out.print(elem);
         }
         System.out.print("})");
-        HashSet<Int32> setin = testClient.testSet(setout);
+        HashSet<Integer> setin = testClient.testSet(setout);
         System.out.print(" = {");
         first = true;
-        for (Int32 elem : setin) {
+        for (int elem : setin) {
           if (first) {
             first = false;
           } else {
             System.out.print(", ");
           }
-          System.out.print(elem.get());
+          System.out.print(elem);
         }
         System.out.print("}\n");
 
         /**
          * LIST TEST
          */
-        ArrayList<Int32> listout = new ArrayList<Int32>();
+        ArrayList<Integer> listout = new ArrayList<Integer>();
         for (int i = -2; i < 3; ++i) {
-          listout.add(new Int32(i));
+          listout.add(i);
         }
         System.out.print("testList({");
         first = true;
-        for (Int32 elem : listout) {
+        for (int elem : listout) {
           if (first) {
             first = false;
           } else {
             System.out.print(", ");
           }
-          System.out.print(elem.get());
+          System.out.print(elem);
         }
         System.out.print("})");
-        ArrayList<Int32> listin = testClient.testList(listout);
+        ArrayList<Integer> listin = testClient.testList(listout);
         System.out.print(" = {");
         first = true;
-        for (Int32 elem : listin) {
+        for (int elem : listin) {
           if (first) {
             first = false;
           } else {
             System.out.print(", ");
           }
-          System.out.print(elem.get());
+          System.out.print(elem);
         }
         System.out.print("}\n");
 
@@ -224,44 +224,44 @@
          * ENUM TEST
          */
         System.out.print("testEnum(ONE)");
-        Int32 ret = testClient.testEnum(Numberz.ONE);
-        System.out.print(" = " + ret.get() + "\n");
+        int ret = testClient.testEnum(Numberz.ONE);
+        System.out.print(" = " + ret + "\n");
 
         System.out.print("testEnum(TWO)");
         ret = testClient.testEnum(Numberz.TWO);
-        System.out.print(" = " + ret.get() + "\n");
+        System.out.print(" = " + ret + "\n");
 
         System.out.print("testEnum(THREE)");
         ret = testClient.testEnum(Numberz.THREE);
-        System.out.print(" = " + ret.get() + "\n");
+        System.out.print(" = " + ret + "\n");
 
         System.out.print("testEnum(FIVE)");
         ret = testClient.testEnum(Numberz.FIVE);
-        System.out.print(" = " + ret.get() + "\n");
+        System.out.print(" = " + ret + "\n");
 
         System.out.print("testEnum(EIGHT)");
         ret = testClient.testEnum(Numberz.EIGHT);
-        System.out.print(" = " + ret.get() + "\n");
+        System.out.print(" = " + ret + "\n");
 
         /**
          * TYPEDEF TEST
          */
         System.out.print("testTypedef(309858235082523)");
-        Int64 uid = testClient.testTypedef(new Int64(309858235082523L));
-        System.out.print(" = " + uid.get() + "\n");
+        long uid = testClient.testTypedef(309858235082523L);
+        System.out.print(" = " + uid + "\n");
 
         /**
          * NESTED MAP TEST
          */
         System.out.print("testMapMap(1)");
-        HashMap<Int32,HashMap<Int32,Int32>> mm =
-          testClient.testMapMap(new Int32(1));
+        HashMap<Integer,HashMap<Integer,Integer>> mm =
+          testClient.testMapMap(1);
         System.out.print(" = {");
-        for (Int32 key : mm.keySet()) {
-          System.out.print(key.get() + " => {");
-          HashMap<Int32,Int32> m2 = mm.get(key);
-          for (Int32 k2 : m2.keySet()) {
-            System.out.print(k2.get() + " => " + m2.get(k2).get() + ", ");
+        for (int key : mm.keySet()) {
+          System.out.print(key + " => {");
+          HashMap<Integer,Integer> m2 = mm.get(key);
+          for (int k2 : m2.keySet()) {
+            System.out.print(k2 + " => " + m2.get(k2) + ", ");
           }
           System.out.print("}, ");
         }
@@ -271,29 +271,29 @@
          * INSANITY TEST
          */
         Insanity insane = new Insanity();
-        insane.userMap.put(Numberz.FIVE, new Int64(5000));
+        insane.userMap.put(Numberz.FIVE, (long)5000);
         Xtruct truck = new Xtruct();
-        truck.string_thing.value = "Truck";
-        truck.byte_thing.set((short)8);
-        truck.i32_thing.set(8);
-        truck.i64_thing.set(8);
+        truck.string_thing = "Truck";
+        truck.byte_thing = (byte)8;
+        truck.i32_thing = 8;
+        truck.i64_thing = 8;
         insane.xtructs.add(truck);
         System.out.print("testInsanity()");
-        HashMap<Int64,HashMap<Int32,Insanity>> whoa =
+        HashMap<Long,HashMap<Integer,Insanity>> whoa =
           testClient.testInsanity(insane);
         System.out.print(" = {");
-        for (Int64 key : whoa.keySet()) {
-          HashMap<Int32,Insanity> val = whoa.get(key);
-          System.out.print(key.get() + " => {");
+        for (long key : whoa.keySet()) {
+          HashMap<Integer,Insanity> val = whoa.get(key);
+          System.out.print(key + " => {");
 
-          for (Int32 k2 : val.keySet()) {
+          for (int k2 : val.keySet()) {
             Insanity v2 = val.get(k2);
-            System.out.print(k2.get() + " => {");
-            HashMap<Int32, Int64> userMap = v2.userMap;
+            System.out.print(k2 + " => {");
+            HashMap<Integer, Long> userMap = v2.userMap;
             System.out.print("{");
-            for (Int32 k3 : userMap.keySet()) {
-              System.out.print(k3.get() + " => " +
-                               userMap.get(k3).get() + ", ");
+            for (int k3 : userMap.keySet()) {
+              System.out.print(k3 + " => " +
+                               userMap.get(k3) + ", ");
             }
             System.out.print("}, ");
 
@@ -301,10 +301,10 @@
             System.out.print("{");
             for (Xtruct x : xtructs) {
               System.out.print("{" +
-                               "\"" + x.string_thing.value + "\", " +
-                               x.byte_thing.get() + ", " +
-                               x.i32_thing.get() + ", "+
-                               x.i64_thing.get() + "}, ");
+                               "\"" + x.string_thing + "\", " +
+                               x.byte_thing + ", " +
+                               x.i32_thing + ", "+
+                               x.i64_thing + "}, ");
             }
             System.out.print("}");
 
diff --git a/test/java/src/TestServer.java b/test/java/src/TestServer.java
index 0acc8a2..4e415ae 100644
--- a/test/java/src/TestServer.java
+++ b/test/java/src/TestServer.java
@@ -1,22 +1,22 @@
 package com.facebook.thrift.test;
 
-import com.facebook.thrift.types.*;
+import com.facebook.thrift.TException;
 import com.facebook.thrift.protocol.TBinaryProtocol;
 import com.facebook.thrift.protocol.TProtocol;
-import com.facebook.thrift.protocol.TString;
 import com.facebook.thrift.server.TServer;
 import com.facebook.thrift.server.TSimpleServer;
 import com.facebook.thrift.transport.TServerSocket;
 import com.facebook.thrift.transport.TServerTransport;
 
-import ThriftTest.*;
+// Generated code
+import thrift.test.*;
 
 import java.net.ServerSocket;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 
-public class TestServer extends ThriftTestServerIf {
+public class TestServer extends ThriftTest.Server {
   public TestServer(TProtocol prot) {
     super(prot);
   }
@@ -25,159 +25,199 @@
     System.out.print("testVoid()\n");
   }
 
-  public TString testString(TString thing) {
-    System.out.print("testString(\"" + thing.value + "\")\n");
+  public String testString(String thing) {
+    System.out.print("testString(\"" + thing + "\")\n");
     return thing;
   }
 
-  public UInt8 testByte(UInt8 thing) {
-    System.out.print("testByte(" + thing.get() + ")\n");
+  public byte testByte(byte thing) {
+    System.out.print("testByte(" + thing + ")\n");
     return thing;
   }
 
-  public Int32 testI32(Int32 thing) {
-    System.out.print("testI32(" + thing.get() + ")\n");
+  public int testI32(int thing) {
+    System.out.print("testI32(" + thing + ")\n");
     return thing;
   }
 
-  public Int64 testI64(Int64 thing) {
-    System.out.print("testI64(" + thing.get() + ")\n");
+  public long testI64(long thing) {
+    System.out.print("testI64(" + thing + ")\n");
     return thing;
   }
   
   public Xtruct testStruct(Xtruct thing) {
     System.out.print("testStruct({" +
-                     "\"" + thing.string_thing.value + "\", " +
-                     thing.byte_thing.get() + ", " +
-                     thing.i32_thing.get() + ", " +
-                     thing.i64_thing.get() + "})\n");
+                     "\"" + thing.string_thing + "\", " +
+                     thing.byte_thing + ", " +
+                     thing.i32_thing + ", " +
+                     thing.i64_thing + "})\n");
     return thing;
   }
   
   public Xtruct2 testNest(Xtruct2 nest) {
     Xtruct thing = nest.struct_thing;
     System.out.print("testNest({" +
-                     nest.byte_thing.get() + ", {" +
-                     "\"" + thing.string_thing.value + "\", " +
-                     thing.byte_thing.get() + ", " +
-                     thing.i32_thing.get() + ", " +
-                     thing.i64_thing.get() + "}, " +
-                     nest.i32_thing.get() + "})\n");
+                     nest.byte_thing + ", {" +
+                     "\"" + thing.string_thing + "\", " +
+                     thing.byte_thing + ", " +
+                     thing.i32_thing + ", " +
+                     thing.i64_thing + "}, " +
+                     nest.i32_thing + "})\n");
     return nest;
   }
   
-  public HashMap<Int32,Int32> testMap(HashMap<Int32,Int32> thing) {
+  public HashMap<Integer,Integer> testMap(HashMap<Integer,Integer> thing) {
     System.out.print("testMap({");
     boolean first = true;
-    for (Int32 key : thing.keySet()) {
+    for (int key : thing.keySet()) {
       if (first) {
         first = false;
       } else {
         System.out.print(", ");
       }
-      System.out.print(key.get() + " => " + thing.get(key).get());
+      System.out.print(key + " => " + thing.get(key));
     }
     System.out.print("})\n");
     return thing;
   }
 
-  public HashSet<Int32> testSet(HashSet<Int32> thing) {
+  public HashSet<Integer> testSet(HashSet<Integer> thing) {
     System.out.print("testSet({");
     boolean first = true;
-    for (Int32 elem : thing) {
+    for (int elem : thing) {
       if (first) {
         first = false;
       } else {
         System.out.print(", ");
       }
-      System.out.print(elem.get());
+      System.out.print(elem);
     }
     System.out.print("})\n");
     return thing;
   }
 
-  public ArrayList<Int32> testList(ArrayList<Int32> thing) {
+  public ArrayList<Integer> testList(ArrayList<Integer> thing) {
     System.out.print("testList({");
     boolean first = true;
-    for (Int32 elem : thing) {
+    for (int elem : thing) {
       if (first) {
         first = false;
       } else {
         System.out.print(", ");
       }
-      System.out.print(elem.get());
+      System.out.print(elem);
     }
     System.out.print("})\n");
     return thing;
   }
 
-  public Int32 testEnum(Int32 thing) {
-    System.out.print("testEnum(" + thing.get() + ")\n");
+  public int testEnum(int thing) {
+    System.out.print("testEnum(" + thing + ")\n");
     return thing;
   }
 
-  public Int64 testTypedef(Int64 thing) {
-    System.out.print("testTypedef(" + thing.get() + ")\n");
+  public long testTypedef(long thing) {
+    System.out.print("testTypedef(" + thing + ")\n");
     return thing;
   }
 
-  public HashMap<Int32,HashMap<Int32,Int32>> testMapMap(Int32 hello) {
-    System.out.print("testMapMap(" + hello.get() + ")\n");
-    HashMap<Int32,HashMap<Int32,Int32>> mapmap =
-      new HashMap<Int32,HashMap<Int32,Int32>>();
+  public HashMap<Integer,HashMap<Integer,Integer>> testMapMap(int hello) {
+    System.out.print("testMapMap(" + hello + ")\n");
+    HashMap<Integer,HashMap<Integer,Integer>> mapmap =
+      new HashMap<Integer,HashMap<Integer,Integer>>();
 
-    HashMap<Int32,Int32> pos = new HashMap<Int32,Int32>();
-    HashMap<Int32,Int32> neg = new HashMap<Int32,Int32>();
+    HashMap<Integer,Integer> pos = new HashMap<Integer,Integer>();
+    HashMap<Integer,Integer> neg = new HashMap<Integer,Integer>();
     for (int i = 1; i < 5; i++) {
-      pos.put(new Int32(i), new Int32(i));
-      neg.put(new Int32(-i), new Int32(-i));
+      pos.put(i, i);
+      neg.put(-i, -i);
     }
 
-    mapmap.put(new Int32(4), pos);
-    mapmap.put(new Int32(-4), neg);
+    mapmap.put(4, pos);
+    mapmap.put(-4, neg);
 
     return mapmap;
   }
 
-  public HashMap<Int64, HashMap<Int32,Insanity>> testInsanity(Insanity argument) {
+  public HashMap<Long, HashMap<Integer,Insanity>> testInsanity(Insanity argument) {
     System.out.print("testInsanity()\n");
     
     Xtruct hello = new Xtruct();
-    hello.string_thing.value = "Hello2";
-    hello.byte_thing.set((short)2);
-    hello.i32_thing.set(2);
-    hello.i64_thing.set(2);
+    hello.string_thing = "Hello2";
+    hello.byte_thing = 2;
+    hello.i32_thing = 2;
+    hello.i64_thing = 2;
 
     Xtruct goodbye = new Xtruct();
-    goodbye.string_thing.value = "Goodbye4";
-    goodbye.byte_thing.set((short)4);
-    goodbye.i32_thing.set(4);
-    goodbye.i64_thing.set(4);
+    goodbye.string_thing = "Goodbye4";
+    goodbye.byte_thing = (byte)4;
+    goodbye.i32_thing = 4;
+    goodbye.i64_thing = (long)4;
 
     Insanity crazy = new Insanity();
-    crazy.userMap.put(Numberz.EIGHT, new Int64(8));
+    crazy.userMap.put(Numberz.EIGHT, (long)8);
     crazy.xtructs.add(goodbye);
 
     Insanity looney = new Insanity();
-    crazy.userMap.put(Numberz.FIVE, new Int64(5));
+    crazy.userMap.put(Numberz.FIVE, (long)5);
     crazy.xtructs.add(hello);
 
-    HashMap<Int32,Insanity> first_map = new HashMap<Int32, Insanity>();
-    HashMap<Int32,Insanity> second_map = new HashMap<Int32, Insanity>();;
+    HashMap<Integer,Insanity> first_map = new HashMap<Integer, Insanity>();
+    HashMap<Integer,Insanity> second_map = new HashMap<Integer, Insanity>();;
 
     first_map.put(Numberz.TWO, crazy);
     first_map.put(Numberz.THREE, crazy);
 
     second_map.put(Numberz.SIX, looney);
 
-    HashMap<Int64,HashMap<Int32,Insanity>> insane =
-      new HashMap<Int64, HashMap<Int32,Insanity>>();
-    insane.put(new Int64(1), first_map);
-    insane.put(new Int64(2), second_map);
+    HashMap<Long,HashMap<Integer,Insanity>> insane =
+      new HashMap<Long, HashMap<Integer,Insanity>>();
+    insane.put((long)1, first_map);
+    insane.put((long)2, second_map);
 
     return insane;
   }
 
+  public Xtruct testMulti(byte arg0, int arg1, long arg2, HashMap<Short,String> arg3, int arg4, long arg5) {
+    System.out.print("testMulti()\n");
+    
+    Xtruct hello = new Xtruct();;
+    hello.string_thing = "Hello2";
+    hello.byte_thing = arg0;
+    hello.i32_thing = arg1;
+    hello.i64_thing = arg2;
+    return hello;
+  }
+
+  public void testException(String arg) throws Xception {
+    System.out.print("testException("+arg+")\n");
+    if (arg.equals("Xception")) {
+      Xception x = new Xception();
+      x.errorCode = 1001;
+      x.message = "This is an Xception";
+      throw x;
+    }
+    return;
+  }
+
+  public Xtruct testMultiException(String arg0, String arg1) throws Xception, Xception2 {
+    System.out.print("testMultiException(" + arg0 + ", " + arg1 + ")\n");
+    if (arg0.equals("Xception")) {
+      Xception x = new Xception();
+      x.errorCode = 1001;
+      x.message = "This is an Xception";
+    } else if (arg0.equals("Xception2")) {
+      Xception2 x = new Xception2();
+      x.errorCode = 2002;
+      x.struct_thing.string_thing = "This is an Xception2";
+    }
+     
+    Xtruct result = new Xtruct();
+    result.string_thing = arg1;
+    return result;
+  }
+
+
   public static void main(String [] args) {
     try {
       int port = 9090;