Skip to content

Commit f8f900f

Browse files
committed
Merge branch 'qistoph-master'
2 parents 1dc4427 + ffb3b5b commit f8f900f

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

javaobj/core.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
# Standard library
3737
import collections
38+
import functools
3839
import logging
3940
import os
4041
import struct
@@ -906,6 +907,9 @@ def do_object(self, parent=None, ident=0):
906907
and classdesc.flags & self.SC_WRITE_METHOD
907908
or classdesc.flags & self.SC_EXTERNALIZABLE
908909
and classdesc.flags & self.SC_BLOCK_DATA
910+
or classdesc.superclass is not None
911+
and classdesc.superclass.flags & self.SC_SERIALIZABLE
912+
and classdesc.superclass.flags & self.SC_WRITE_METHOD
909913
):
910914
# objectAnnotation
911915
log_debug(
@@ -1709,6 +1713,38 @@ def __extra_loading__(self, unmarshaller, ident=0):
17091713
# Lists have their content in there annotations
17101714
self.extend(self.annotations[1:])
17111715

1716+
@functools.total_ordering
1717+
class JavaPrimitiveClass(JavaObject):
1718+
"""
1719+
Parent of Java classes matching a primitive (Bool, Integer, Long, ...)
1720+
"""
1721+
def __init__(self, unmarshaller):
1722+
JavaObject.__init__(self)
1723+
self.value = None
1724+
1725+
def __str__(self):
1726+
return str(self.value)
1727+
1728+
def __repr__(self):
1729+
return repr(self.value)
1730+
1731+
def __hash__(self):
1732+
return hash(self.value)
1733+
1734+
def __eq__(self, other):
1735+
return self.value == other
1736+
1737+
def __lt__(self, other):
1738+
return self.value < other
1739+
1740+
class JavaBool(JavaPrimitiveClass):
1741+
def __bool__(self):
1742+
return self.value
1743+
1744+
class JavaInt(JavaPrimitiveClass):
1745+
def __int__(self):
1746+
return self.value
1747+
17121748
class JavaMap(dict, JavaObject):
17131749
"""
17141750
Python-Java dictionary/map bridge type
@@ -1955,6 +1991,9 @@ def do_period(self, unmarshaller, data):
19551991
"java.util.HashSet": JavaSet,
19561992
"java.util.TreeSet": JavaTreeSet,
19571993
"java.time.Ser": JavaTime,
1994+
"java.lang.Boolean": JavaBool,
1995+
"java.lang.Integer": JavaInt,
1996+
"java.lang.Long": JavaInt,
19581997
}
19591998

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

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import java.time.LocalTime;
1414
import java.time.ZoneId;
1515
import java.time.ZonedDateTime;
16+
import java.util.HashMap;
1617
import java.util.HashSet;
1718
import java.util.Hashtable;
1819
import java.util.LinkedHashSet;
20+
import java.util.Map;
1921
import java.util.Set;
2022
import java.util.TreeSet;
2123
import java.util.Vector;
@@ -326,6 +328,34 @@ public void testTime() throws Exception {
326328
oos.flush();
327329
}
328330

331+
/**
332+
* Tests th pull request #27 by @qistoph:
333+
* Add support for java.lang.Bool, Integer and Long classes
334+
*/
335+
@Test
336+
public void testBoolIntLong() throws Exception {
337+
Map<String, Object> hm1 = new HashMap<String, Object>();
338+
hm1.put("key1", "value1");
339+
hm1.put("key2", "value2");
340+
hm1.put("int", 9);
341+
hm1.put("int2", new Integer(10));
342+
hm1.put("bool", true);
343+
hm1.put("bool2", new Boolean(true));
344+
345+
oos.writeObject(hm1);
346+
oos.flush();
347+
348+
Map<String, Object> hm2 = new HashMap<String, Object>();
349+
hm2.put("subMap", hm1);
350+
351+
ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream(name.getMethodName() + "-2.ser"));
352+
try {
353+
oos2.writeObject(hm2);
354+
} finally {
355+
oos2.close();
356+
}
357+
}
358+
329359
@Test
330360
public void testSwingObject() throws Exception {
331361

tests/tests.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,35 @@ def test_jceks_issue_5(self):
391391
_logger.info(pobj)
392392
# self._try_marshalling(jobj, pobj)
393393

394+
def test_qistoph_pr_27(self):
395+
"""
396+
Tests support for Bool, Integer, Long classes (PR #27)
397+
"""
398+
# Load the basic map
399+
jobj = self.read_file("testBoolIntLong.ser")
400+
pobj = javaobj.loads(jobj)
401+
_logger.debug(pobj)
402+
403+
# Basic checking
404+
self.assertEqual(pobj["key1"], "value1")
405+
self.assertEqual(pobj["key2"], "value2")
406+
self.assertEqual(pobj["int"], 9)
407+
self.assertEqual(pobj["int2"], 10)
408+
self.assertEqual(pobj["bool"], True)
409+
self.assertEqual(pobj["bool2"], True)
410+
411+
# Load the parent map
412+
jobj2 = self.read_file("testBoolIntLong-2.ser")
413+
pobj2 = javaobj.loads(jobj2)
414+
_logger.debug(pobj2)
415+
416+
parent_map = pobj2["subMap"]
417+
for key, value in pobj.items():
418+
self.assertEqual(parent_map[key], value)
394419

395420
# ------------------------------------------------------------------------------
396421

422+
397423
if __name__ == "__main__":
398424
# Setup logging
399425
logging.basicConfig(level=logging.INFO)

0 commit comments

Comments
 (0)