Skip to content

Commit

Permalink
Merge pull request #291 from jtoledo1974/master
Browse files Browse the repository at this point in the history
Fixed mishandling of long arguments in multiple methods. Added test cases.
  • Loading branch information
tito committed Aug 21, 2017
2 parents 6ad8740 + a912e1a commit 6f8d846
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
15 changes: 13 additions & 2 deletions jnius/jnius_utils.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,19 @@ cdef int calculate_score(sign_args, args, is_varargs=False) except *:
score += 10
continue

if r == 'S' or r == 'I' or r == 'J':
if isinstance(arg, int):
if r == 'S' or r == 'I':
if isinstance(arg, int) or (
(isinstance(arg, long) and arg < 2147483648)):
score += 10
continue
elif isinstance(arg, float):
score += 5
continue
else:
return -1

if r == 'J':
if isinstance(arg, int) or isinstance(arg, long):
score += 10
continue
elif isinstance(arg, float):
Expand Down
4 changes: 4 additions & 0 deletions tests/java-src/org/jnius/MultipleMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ public static String resolve(String i, String j, int... integers) {
public static String resolve(int... integers) {
return "resolved varargs";
}

public static String resolve(int i, long j, String k) {
return "resolved one int, one long and a string";
}
}
9 changes: 9 additions & 0 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
import unittest
from jnius.reflect import autoclass

try:
long
except NameError:
# Python 3
long = int


class BasicsTest(unittest.TestCase):

def test_static_methods(self):
Expand Down Expand Up @@ -101,6 +108,8 @@ def test_instances_methods_params(self):
test = autoclass('org.jnius.BasicsTest')()
self.assertEquals(test.methodParamsZBCSIJFD(
True, 127, 'k', 32767, 2147483467, 9223372036854775807, 1.23456789, 1.23456789), True)
self.assertEquals(test.methodParamsZBCSIJFD(
True, long(127), 'k', long(32767), long(2147483467), 9223372036854775807, 1.23456789, 1.23456789), True)
self.assertEquals(test.methodParamsString('helloworld'), True)
self.assertEquals(test.methodParamsArrayI([1, 2, 3]), True)
self.assertEquals(test.methodParamsArrayString([
Expand Down
21 changes: 21 additions & 0 deletions tests/test_method_multiple_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
import unittest
from jnius.reflect import autoclass

try:
long
except NameError:
# Python 3
long = int


class MultipleSignature(unittest.TestCase):

def test_multiple_constructors(self):
Expand Down Expand Up @@ -44,6 +51,20 @@ def test_multiple_methods_varargs(self):
MultipleMethods = autoclass('org.jnius.MultipleMethods')
self.assertEqual(MultipleMethods.resolve(1, 2, 3), 'resolved varargs')

def test_multiple_methods_varargs_long(self):
MultipleMethods = autoclass('org.jnius.MultipleMethods')
self.assertEqual(MultipleMethods.resolve(long(1), long(2), long(3)), 'resolved varargs')

def test_multiple_methods_two_args_and_varargs(self):
MultipleMethods = autoclass('org.jnius.MultipleMethods')
self.assertEqual(MultipleMethods.resolve('one', 'two', 1, 2, 3), 'resolved two args and varargs')

def test_multiple_methods_one_int_one_small_long_and_a_string(self):
MultipleMethods = autoclass('org.jnius.MultipleMethods')
self.assertEqual(MultipleMethods.resolve(
1, long(1), "one"), "resolved one int, one long and a string")

def test_multiple_methods_one_int_one_actual_long_and_a_string(self):
MultipleMethods = autoclass('org.jnius.MultipleMethods')
self.assertEqual(MultipleMethods.resolve(
1, 2 ** 63 - 1, "one"), "resolved one int, one long and a string")

0 comments on commit 6f8d846

Please sign in to comment.