Skip to content

Commit 92766e0

Browse files
committed
Added early support for sets
Supports HashSet and TreeSet. LinkedHashSet is causing too much trouble. See #25 for details
1 parent 726611a commit 92766e0

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

javaobj/core.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,13 @@ def __repr__(self):
318318
name = self.classdesc.name
319319
return "<javaobj:{0}>".format(name)
320320

321+
def __hash__(self):
322+
"""
323+
Each JavaObject we load must have a hash method to be accepted in sets
324+
and alike. The default hash is the memory address of the object.
325+
"""
326+
return id(self)
327+
321328
def __eq__(self, other):
322329
"""
323330
Equality test between two Java classes
@@ -1679,12 +1686,39 @@ def __extra_loading__(self, unmarshaller, ident=0):
16791686
if opid != 0:
16801687
raise ValueError("Should find 0x0, got {0:x}".format(opid))
16811688

1689+
class JavaSet(set, JavaObject):
1690+
"""
1691+
Python-Java set bridge type
1692+
"""
1693+
def __init__(self, unmarshaller):
1694+
# type: (JavaObjectUnmarshaller) -> None
1695+
set.__init__(self)
1696+
JavaObject.__init__(self)
1697+
1698+
def __extra_loading__(self, unmarshaller, ident=0):
1699+
# type: (JavaObjectUnmarshaller, int) -> None
1700+
"""
1701+
Loads the content of the map, written with a custom implementation
1702+
"""
1703+
self.update(self.annotations[1:])
1704+
1705+
class JavaTreeSet(JavaSet):
1706+
def __extra_loading__(self, unmarshaller, ident=0):
1707+
# type: (JavaObjectUnmarshaller, int) -> None
1708+
"""
1709+
Loads the content of the map, written with a custom implementation
1710+
"""
1711+
# Annotation[1] == size of the set
1712+
self.update(self.annotations[2:])
1713+
16821714
TYPE_MAPPER = {
16831715
"java.util.ArrayList": JavaList,
16841716
"java.util.LinkedList": JavaList,
16851717
"java.util.HashMap": JavaMap,
16861718
"java.util.LinkedHashMap": JavaLinkedHashMap,
16871719
"java.util.TreeMap": JavaMap,
1720+
"java.util.HashSet": JavaSet,
1721+
"java.util.TreeSet": JavaTreeSet,
16881722
}
16891723

16901724
def create(self, classdesc, unmarshaller=None):

tests/java/src/test/java/OneTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
import java.io.ObjectInputStream;
77
import java.io.ObjectOutputStream;
88
import java.io.Serializable;
9+
import java.util.HashSet;
910
import java.util.Hashtable;
11+
import java.util.LinkedHashSet;
12+
import java.util.Set;
13+
import java.util.TreeSet;
1014
import java.util.Vector;
1115

1216
import javax.swing.JScrollPane;
@@ -268,6 +272,39 @@ public void testSuper() throws Exception {
268272
oos.flush();
269273
}
270274

275+
@Test
276+
public void testHashSet() throws Exception {
277+
final Set<Integer> set = new HashSet<Integer>();
278+
set.add(1);
279+
set.add(2);
280+
set.add(1);
281+
set.add(42);
282+
oos.writeObject(set);
283+
oos.flush();
284+
}
285+
286+
@Test
287+
public void testLinkedHashSet() throws Exception {
288+
final Set<Integer> set = new LinkedHashSet<Integer>();
289+
set.add(1);
290+
set.add(2);
291+
set.add(1);
292+
set.add(42);
293+
oos.writeObject(set);
294+
oos.flush();
295+
}
296+
297+
@Test
298+
public void testTreeSet() throws Exception {
299+
final Set<Integer> set = new TreeSet<Integer>();
300+
set.add(1);
301+
set.add(2);
302+
set.add(1);
303+
set.add(42);
304+
oos.writeObject(set);
305+
oos.flush();
306+
}
307+
271308
@Test
272309
public void testSwingObject() throws Exception {
273310

tests/tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,15 @@ def test_enums(self):
317317

318318
# self._try_marshalling(jobj, pobj)
319319

320+
def test_sets(self):
321+
for filename in ("testHashSet.ser", "testTreeSet.ser"):
322+
print("Loading", filename)
323+
jobj = self.read_file(filename)
324+
pobj = javaobj.loads(jobj)
325+
_logger.debug(pobj)
326+
self.assertIsInstance(pobj, set)
327+
self.assertSetEqual({i.value for i in pobj}, {1, 2, 42})
328+
320329
# def test_exception(self):
321330
# jobj = self.read_file("objException.ser")
322331
# pobj = javaobj.loads(jobj)

0 commit comments

Comments
 (0)