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
diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc
index 37c0c59..52166bf 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_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 @@
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 @@
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 87f3971..85aac9e 100644
--- a/test/java/src/TestClient.java
+++ b/test/java/src/TestClient.java
@@ -13,9 +13,11 @@
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 @@
/**
* MAP TEST
*/
- HashMap<Integer,Integer> mapout = new HashMap<Integer,Integer>();
+ Map<Integer,Integer> mapout = new HashMap<Integer,Integer>();
for (int i = 0; i < 5; ++i) {
mapout.put(i, i-10);
}
@@ -185,7 +187,7 @@
System.out.print(key + " => " + mapout.get(key));
}
System.out.print("})");
- AbstractMap<Integer,Integer> mapin = testClient.testMap(mapout);
+ Map<Integer,Integer> mapin = testClient.testMap(mapout);
System.out.print(" = {");
first = true;
for (int key : mapin.keySet()) {
@@ -201,7 +203,7 @@
/**
* SET TEST
*/
- HashSet<Integer> setout = new HashSet<Integer>();
+ Set<Integer> setout = new HashSet<Integer>();
for (int i = -2; i < 3; ++i) {
setout.add(i);
}
@@ -216,7 +218,7 @@
System.out.print(elem);
}
System.out.print("})");
- HashSet<Integer> setin = testClient.testSet(setout);
+ Set<Integer> setin = testClient.testSet(setout);
System.out.print(" = {");
first = true;
for (int elem : setin) {
@@ -232,7 +234,7 @@
/**
* LIST TEST
*/
- ArrayList<Integer> listout = new ArrayList<Integer>();
+ List<Integer> listout = new ArrayList<Integer>();
for (int i = -2; i < 3; ++i) {
listout.add(i);
}
@@ -247,7 +249,7 @@
System.out.print(elem);
}
System.out.print("})");
- ArrayList<Integer> listin = testClient.testList(listout);
+ List<Integer> listin = testClient.testList(listout);
System.out.print(" = {");
first = true;
for (int elem : listin) {
@@ -294,12 +296,12 @@
* NESTED MAP TEST
*/
System.out.print("testMapMap(1)");
- AbstractMap<Integer,AbstractMap<Integer,Integer>> mm =
+ Map<Integer,Map<Integer,Integer>> mm =
testClient.testMapMap(1);
System.out.print(" = {");
for (int key : mm.keySet()) {
System.out.print(key + " => {");
- AbstractMap<Integer,Integer> m2 = mm.get(key);
+ Map<Integer,Integer> m2 = mm.get(key);
for (int k2 : m2.keySet()) {
System.out.print(k2 + " => " + m2.get(k2) + ", ");
}
@@ -321,17 +323,17 @@
insane.xtructs = new ArrayList<Xtruct>();
insane.xtructs.add(truck);
System.out.print("testInsanity()");
- AbstractMap<Long,AbstractMap<Integer,Insanity>> whoa =
+ Map<Long,Map<Integer,Insanity>> whoa =
testClient.testInsanity(insane);
System.out.print(" = {");
for (long key : whoa.keySet()) {
- AbstractMap<Integer,Insanity> val = whoa.get(key);
+ Map<Integer,Insanity> val = whoa.get(key);
System.out.print(key + " => {");
for (int k2 : val.keySet()) {
Insanity v2 = val.get(k2);
System.out.print(k2 + " => {");
- AbstractMap<Integer, Long> userMap = v2.userMap;
+ Map<Integer, Long> userMap = v2.userMap;
System.out.print("{");
if (userMap != null) {
for (int k3 : userMap.keySet()) {
@@ -340,7 +342,7 @@
}
System.out.print("}, ");
- ArrayList<Xtruct> xtructs = v2.xtructs;
+ List<Xtruct> 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 5a844a3..21003b1 100644
--- a/test/java/src/TestServer.java
+++ b/test/java/src/TestServer.java
@@ -14,9 +14,11 @@
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 @@
return nest;
}
- public AbstractMap<Integer,Integer> testMap(AbstractMap<Integer,Integer> thing) {
+ public Map<Integer,Integer> testMap(Map<Integer,Integer> thing) {
System.out.print("testMap({");
boolean first = true;
for (int key : thing.keySet()) {
@@ -90,7 +92,7 @@
return thing;
}
- public HashSet<Integer> testSet(HashSet<Integer> thing) {
+ public Set<Integer> testSet(Set<Integer> thing) {
System.out.print("testSet({");
boolean first = true;
for (int elem : thing) {
@@ -105,7 +107,7 @@
return thing;
}
- public ArrayList<Integer> testList(ArrayList<Integer> thing) {
+ public List<Integer> testList(List<Integer> thing) {
System.out.print("testList({");
boolean first = true;
for (int elem : thing) {
@@ -130,10 +132,10 @@
return thing;
}
- public AbstractMap<Integer,AbstractMap<Integer,Integer>> testMapMap(int hello) {
+ public Map<Integer,Map<Integer,Integer>> testMapMap(int hello) {
System.out.print("testMapMap(" + hello + ")\n");
- AbstractMap<Integer,AbstractMap<Integer,Integer>> mapmap =
- new HashMap<Integer,AbstractMap<Integer,Integer>>();
+ Map<Integer,Map<Integer,Integer>> mapmap =
+ new HashMap<Integer,Map<Integer,Integer>>();
HashMap<Integer,Integer> pos = new HashMap<Integer,Integer>();
HashMap<Integer,Integer> neg = new HashMap<Integer,Integer>();
@@ -148,7 +150,7 @@
return mapmap;
}
- public AbstractMap<Long, AbstractMap<Integer,Insanity>> testInsanity(Insanity argument) {
+ public Map<Long, Map<Integer,Insanity>> testInsanity(Insanity argument) {
System.out.print("testInsanity()\n");
Xtruct hello = new Xtruct();
@@ -182,15 +184,15 @@
second_map.put(Numberz.SIX, looney);
- AbstractMap<Long,AbstractMap<Integer,Insanity>> insane =
- new HashMap<Long, AbstractMap<Integer,Insanity>>();
+ Map<Long,Map<Integer,Insanity>> insane =
+ new HashMap<Long, Map<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, AbstractMap<Short,String> arg3, int arg4, long arg5) {
+ public Xtruct testMulti(byte arg0, int arg1, long arg2, Map<Short,String> arg3, int arg4, long arg5) {
System.out.print("testMulti()\n");
Xtruct hello = new Xtruct();;