Skip to content

Commit ba0caad

Browse files
author
Rafal Mucha
committed
Issue-374 Add unit tests and improve solution
1 parent 01b18e4 commit ba0caad

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

py4j-java/src/test/java/py4j/examples/ExampleClass.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.nio.channels.Channels;
3838
import java.nio.channels.ReadableByteChannel;
3939
import java.util.ArrayList;
40+
import java.util.HashSet;
4041
import java.util.List;
4142

4243
public class ExampleClass {
@@ -174,6 +175,20 @@ public BigInteger method11(BigInteger bi) {
174175
return bi.add(new BigInteger("1"));
175176
}
176177

178+
public int method12(HashSet<Object> set) {
179+
Object element = set.stream().findAny().get();
180+
if (element instanceof Long) {
181+
return 4;
182+
}
183+
if (element instanceof Integer) {
184+
return 1;
185+
}
186+
if (element instanceof String) {
187+
return 2;
188+
}
189+
return 3;
190+
}
191+
177192
@SuppressWarnings("unused")
178193
private int private_method() {
179194
return 0;

py4j-python/src/py4j/protocol.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
from base64 import standard_b64encode, standard_b64decode
2222

2323
from decimal import Decimal
24-
from enum import Enum
25-
from collections import namedtuple
2624

2725
from py4j.compat import (
2826
long, basestring, unicode, bytearray2,
@@ -72,11 +70,10 @@
7270
ITERATOR_TYPE = "g"
7371
PYTHON_PROXY_TYPE = "f"
7472

75-
class JavaType(Enum):
76-
PRIMITIVE_INT = INTEGER_TYPE
77-
PRIMITIVE_LONG = LONG_TYPE
78-
79-
TypeInt = namedtuple('TypeInt', ['value', 'java_type'])
73+
class TypeHint:
74+
def __init__(self, value, java_type):
75+
self.value = value
76+
self.java_type = java_type
8077

8178
# Protocol
8279
END = "e"
@@ -281,12 +278,12 @@ def get_command_part(parameter, python_proxy_pool=None):
281278

282279
if parameter is None:
283280
command_part = NULL_TYPE
281+
elif isinstance(parameter, TypeHint):
282+
command_part = parameter.java_type + smart_decode(parameter.value)
284283
elif isinstance(parameter, bool):
285284
command_part = BOOLEAN_TYPE + smart_decode(parameter)
286285
elif isinstance(parameter, Decimal):
287286
command_part = DECIMAL_TYPE + smart_decode(parameter)
288-
elif isinstance(parameter, TypeInt):
289-
command_part = parameter.java_type.value + smart_decode(parameter.value)
290287
elif isinstance(parameter, int) and parameter <= JAVA_MAX_INT\
291288
and parameter >= JAVA_MIN_INT:
292289
command_part = INTEGER_TYPE + smart_decode(parameter)

py4j-python/src/py4j/tests/java_gateway_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
set_default_callback_accept_timeout, GatewayConnectionGuard,
3434
get_java_class)
3535
from py4j.protocol import (
36-
Py4JError, Py4JJavaError, Py4JNetworkError, decode_bytearray,
37-
encode_bytearray, escape_new_line, unescape_new_line, smart_decode)
36+
Py4JError, Py4JJavaError, Py4JNetworkError, TypeHint, LONG_TYPE,
37+
decode_bytearray, encode_bytearray, escape_new_line, unescape_new_line, smart_decode)
3838

3939

4040
SERVER_PORT = 25333
@@ -607,7 +607,7 @@ def internal():
607607
class TypeConversionTest(unittest.TestCase):
608608
def setUp(self):
609609
self.p = start_example_app_process()
610-
self.gateway = JavaGateway()
610+
self.gateway = JavaGateway(auto_convert=True)
611611

612612
def tearDown(self):
613613
safe_shutdown(self)
@@ -619,6 +619,8 @@ def testLongInt(self):
619619
self.assertEqual(4, ex.method7(2147483648))
620620
self.assertEqual(4, ex.method7(-2147483649))
621621
self.assertEqual(4, ex.method7(long(2147483648)))
622+
self.assertEqual(4, ex.method7(TypeHint(1, LONG_TYPE)))
623+
self.assertEqual(4, ex.method12({TypeHint(1, LONG_TYPE)}))
622624
self.assertEqual(long(4), ex.method8(3))
623625
self.assertEqual(4, ex.method8(3))
624626
self.assertEqual(long(4), ex.method8(long(3)))

0 commit comments

Comments
 (0)