From 22360b2f30ae980bd78d114f1acb73a03053951e Mon Sep 17 00:00:00 2001 From: Mark Slee Date: Sat, 9 Feb 2008 00:18:32 +0000 Subject: [PATCH] Use interfaces, not classes for Java gen Summary: Not AbstractMap or HashSet, etc. use Map, List, Set Reviewed By: dreiss Test Plan: Generate Java code and build java tests Other Notes: Submitted by Seth Falcon git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665470 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_java_generator.cc | 22 +++++++++++---- test/java/src/TestClient.java | 28 ++++++++++--------- test/java/src/TestServer.java | 24 ++++++++-------- 3 files changed, 45 insertions(+), 29 deletions(-) diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc index 37c0c597..52166bf4 100644 --- a/compiler/cpp/src/generate/t_java_generator.cc +++ b/compiler/cpp/src/generate/t_java_generator.cc @@ -58,9 +58,11 @@ string t_java_generator::java_package() { string t_java_generator::java_type_imports() { return string() + + "import java.util.List;\n" + "import java.util.ArrayList;\n" + - "import java.util.AbstractMap;\n" + + "import java.util.Map;\n" + "import java.util.HashMap;\n" + + "import java.util.Set;\n" + "import java.util.HashSet;\n" + "import com.facebook.thrift.*;\n\n"; } @@ -1775,6 +1777,7 @@ void t_java_generator::generate_serialize_list_element(ofstream& out, string t_java_generator::type_name(t_type* ttype, bool in_container, bool in_init) { // In Java typedefs are just resolved to their real type ttype = get_true_type(ttype); + string prefix; if (ttype->is_base_type()) { return base_type_name((t_base_type*)ttype, in_container); @@ -1782,21 +1785,30 @@ string t_java_generator::type_name(t_type* ttype, bool in_container, bool in_ini return (in_container ? "Integer" : "int"); } else if (ttype->is_map()) { t_map* tmap = (t_map*) ttype; - string prefix; if (in_init) { prefix = "HashMap"; } else { - prefix = "AbstractMap"; + prefix = "Map"; } return prefix + "<" + type_name(tmap->get_key_type(), true) + "," + type_name(tmap->get_val_type(), true) + ">"; } else if (ttype->is_set()) { t_set* tset = (t_set*) ttype; - return "HashSet<" + type_name(tset->get_elem_type(), true) + ">"; + if (in_init) { + prefix = "HashSet<"; + } else { + prefix = "Set<"; + } + return prefix + type_name(tset->get_elem_type(), true) + ">"; } else if (ttype->is_list()) { t_list* tlist = (t_list*) ttype; - return "ArrayList<" + type_name(tlist->get_elem_type(), true) + ">"; + if (in_init) { + prefix = "ArrayList<"; + } else { + prefix = "List<"; + } + return prefix + type_name(tlist->get_elem_type(), true) + ">"; } // Check for namespacing diff --git a/test/java/src/TestClient.java b/test/java/src/TestClient.java index 87f3971e..85aac9ec 100644 --- a/test/java/src/TestClient.java +++ b/test/java/src/TestClient.java @@ -13,9 +13,11 @@ import com.facebook.thrift.transport.TTransportException; import com.facebook.thrift.protocol.TBinaryProtocol; import com.facebook.thrift.protocol.TJSONProtocol; -import java.util.AbstractMap; +import java.util.Map; import java.util.HashMap; +import java.util.Set; import java.util.HashSet; +import java.util.List; import java.util.ArrayList; /** @@ -170,7 +172,7 @@ public class TestClient { /** * MAP TEST */ - HashMap mapout = new HashMap(); + Map mapout = new HashMap(); for (int i = 0; i < 5; ++i) { mapout.put(i, i-10); } @@ -185,7 +187,7 @@ public class TestClient { System.out.print(key + " => " + mapout.get(key)); } System.out.print("})"); - AbstractMap mapin = testClient.testMap(mapout); + Map mapin = testClient.testMap(mapout); System.out.print(" = {"); first = true; for (int key : mapin.keySet()) { @@ -201,7 +203,7 @@ public class TestClient { /** * SET TEST */ - HashSet setout = new HashSet(); + Set setout = new HashSet(); for (int i = -2; i < 3; ++i) { setout.add(i); } @@ -216,7 +218,7 @@ public class TestClient { System.out.print(elem); } System.out.print("})"); - HashSet setin = testClient.testSet(setout); + Set setin = testClient.testSet(setout); System.out.print(" = {"); first = true; for (int elem : setin) { @@ -232,7 +234,7 @@ public class TestClient { /** * LIST TEST */ - ArrayList listout = new ArrayList(); + List listout = new ArrayList(); for (int i = -2; i < 3; ++i) { listout.add(i); } @@ -247,7 +249,7 @@ public class TestClient { System.out.print(elem); } System.out.print("})"); - ArrayList listin = testClient.testList(listout); + List listin = testClient.testList(listout); System.out.print(" = {"); first = true; for (int elem : listin) { @@ -294,12 +296,12 @@ public class TestClient { * NESTED MAP TEST */ System.out.print("testMapMap(1)"); - AbstractMap> mm = + Map> mm = testClient.testMapMap(1); System.out.print(" = {"); for (int key : mm.keySet()) { System.out.print(key + " => {"); - AbstractMap m2 = mm.get(key); + Map m2 = mm.get(key); for (int k2 : m2.keySet()) { System.out.print(k2 + " => " + m2.get(k2) + ", "); } @@ -321,17 +323,17 @@ public class TestClient { insane.xtructs = new ArrayList(); insane.xtructs.add(truck); System.out.print("testInsanity()"); - AbstractMap> whoa = + Map> whoa = testClient.testInsanity(insane); System.out.print(" = {"); for (long key : whoa.keySet()) { - AbstractMap val = whoa.get(key); + Map val = whoa.get(key); System.out.print(key + " => {"); for (int k2 : val.keySet()) { Insanity v2 = val.get(k2); System.out.print(k2 + " => {"); - AbstractMap userMap = v2.userMap; + Map userMap = v2.userMap; System.out.print("{"); if (userMap != null) { for (int k3 : userMap.keySet()) { @@ -340,7 +342,7 @@ public class TestClient { } System.out.print("}, "); - ArrayList xtructs = v2.xtructs; + List xtructs = v2.xtructs; System.out.print("{"); if (xtructs != null) { for (Xtruct x : xtructs) { diff --git a/test/java/src/TestServer.java b/test/java/src/TestServer.java index 5a844a38..21003b10 100644 --- a/test/java/src/TestServer.java +++ b/test/java/src/TestServer.java @@ -14,9 +14,11 @@ import com.facebook.thrift.transport.TServerTransport; import thrift.test.*; import java.net.ServerSocket; +import java.util.List; import java.util.ArrayList; -import java.util.AbstractMap; +import java.util.Map; import java.util.HashMap; +import java.util.Set; import java.util.HashSet; public class TestServer { @@ -75,7 +77,7 @@ public class TestServer { return nest; } - public AbstractMap testMap(AbstractMap thing) { + public Map testMap(Map thing) { System.out.print("testMap({"); boolean first = true; for (int key : thing.keySet()) { @@ -90,7 +92,7 @@ public class TestServer { return thing; } - public HashSet testSet(HashSet thing) { + public Set testSet(Set thing) { System.out.print("testSet({"); boolean first = true; for (int elem : thing) { @@ -105,7 +107,7 @@ public class TestServer { return thing; } - public ArrayList testList(ArrayList thing) { + public List testList(List thing) { System.out.print("testList({"); boolean first = true; for (int elem : thing) { @@ -130,10 +132,10 @@ public class TestServer { return thing; } - public AbstractMap> testMapMap(int hello) { + public Map> testMapMap(int hello) { System.out.print("testMapMap(" + hello + ")\n"); - AbstractMap> mapmap = - new HashMap>(); + Map> mapmap = + new HashMap>(); HashMap pos = new HashMap(); HashMap neg = new HashMap(); @@ -148,7 +150,7 @@ public class TestServer { return mapmap; } - public AbstractMap> testInsanity(Insanity argument) { + public Map> testInsanity(Insanity argument) { System.out.print("testInsanity()\n"); Xtruct hello = new Xtruct(); @@ -182,15 +184,15 @@ public class TestServer { second_map.put(Numberz.SIX, looney); - AbstractMap> insane = - new HashMap>(); + Map> insane = + new HashMap>(); insane.put((long)1, first_map); insane.put((long)2, second_map); return insane; } - public Xtruct testMulti(byte arg0, int arg1, long arg2, AbstractMap arg3, int arg4, long arg5) { + public Xtruct testMulti(byte arg0, int arg1, long arg2, Map arg3, int arg4, long arg5) { System.out.print("testMulti()\n"); Xtruct hello = new Xtruct();; -- 2.17.1