Skip to content

Commit

Permalink
Merge pull request #1082 from Thrameos/sanityCheck
Browse files Browse the repository at this point in the history
Sanity check for JImplements
  • Loading branch information
marscher authored Aug 25, 2022
2 parents f6ad8f4 + c371234 commit 82a7658
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions jpype/_jproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def _createJProxyDeferred(cls, *intf):
@JOverride notation on methods evaluated at first
instantiation.
"""
if not isinstance(cls, type):
raise TypeError("JImplements only applies to types, not %s" % (type(cls)))

def new(tp, *args, **kwargs):
# Attach a __jpype_interfaces__ attribute to this class if
# one doesn't already exist.
Expand All @@ -77,6 +80,9 @@ def _createJProxy(cls, *intf):
""" (internal) Create a proxy from a Python class with
@JOverride notation on methods evaluated at declaration.
"""
if not isinstance(cls, type):
raise TypeError("JImplements only applies to types, not %s" % (type(cls)))

actualIntf = _prepareInterfaces(cls, intf)

def new(tp, *args, **kwargs):
Expand Down
12 changes: 12 additions & 0 deletions test/jpypetest/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ def testMethod1(self):
def testProxyImplementsForm2(self):
itf1 = self.package.TestInterface1
itf2 = self.package.TestInterface2

@JImplements(itf1, itf2)
class MyImpl(object):
@JOverride
Expand Down Expand Up @@ -392,6 +393,7 @@ def equals(self, _obj):

def testUnwrap(self):
fixture = JClass("jpype.common.Fixture")()

@JImplements("java.io.Serializable")
class Q(object):
pass
Expand Down Expand Up @@ -429,6 +431,7 @@ class R(object):

def testMethods(self):
fixture = JClass("jpype.common.Fixture")()

@JImplements("java.io.Serializable")
class R(object):
pass
Expand Down Expand Up @@ -472,6 +475,7 @@ def run(self):

def testWeak(self):
hc = java.lang.System.identityHashCode

@JImplements("java.io.Serializable")
class MyObj(object):
pass
Expand All @@ -496,6 +500,14 @@ def testFunctionalLambda(self):
js = JObject(lambda x: 2 * x, "java.util.function.DoubleUnaryOperator")
self.assertEqual(js.applyAsDouble(1), 2.0)

def testBadImplements(self):
with self.assertRaises(TypeError):
@JImplements("java.lang.Runnable")
def MyImpl(object):
@JOverride
def run(self):
pass


@subrun.TestCase(individual=True)
class TestProxyDefinitionWithoutJVM(common.JPypeTestCase):
Expand Down

0 comments on commit 82a7658

Please sign in to comment.