From a1cce94a8b943f180c77c67839ba0c3b633a7c7f Mon Sep 17 00:00:00 2001 From: "Karl E. Nelson" Date: Mon, 11 Jul 2022 13:11:54 -0700 Subject: [PATCH 1/3] Sanity check for JImplements --- jpype/_jproxy.py | 5 +++++ test/jpypetest/test_proxy.py | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/jpype/_jproxy.py b/jpype/_jproxy.py index 2efa79df4..79804d459 100644 --- a/jpype/_jproxy.py +++ b/jpype/_jproxy.py @@ -59,6 +59,9 @@ def _createJProxyDeferred(cls, *intf): @JOverride notation on methods evaluated at first instantiation. """ + if not isinstance(cls, type): + raise TypeError("InstanceOf 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. @@ -77,6 +80,8 @@ 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("InstanceOf only applies to types, not %s" % (type(cls))) actualIntf = _prepareInterfaces(cls, intf) def new(tp, *args, **kwargs): diff --git a/test/jpypetest/test_proxy.py b/test/jpypetest/test_proxy.py index 8cba2006e..0cba57b52 100644 --- a/test/jpypetest/test_proxy.py +++ b/test/jpypetest/test_proxy.py @@ -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 @@ -392,6 +393,7 @@ def equals(self, _obj): def testUnwrap(self): fixture = JClass("jpype.common.Fixture")() + @JImplements("java.io.Serializable") class Q(object): pass @@ -429,6 +431,7 @@ class R(object): def testMethods(self): fixture = JClass("jpype.common.Fixture")() + @JImplements("java.io.Serializable") class R(object): pass @@ -472,6 +475,7 @@ def run(self): def testWeak(self): hc = java.lang.System.identityHashCode + @JImplements("java.io.Serializable") class MyObj(object): pass @@ -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): From ce7ca9f87c2bf332c92099220d2682aed5cea5be Mon Sep 17 00:00:00 2001 From: "Karl E. Nelson" Date: Mon, 11 Jul 2022 13:13:26 -0700 Subject: [PATCH 2/3] Remove extra line From c371234250749f90940c1f89dfe9fa4f6e98d0c9 Mon Sep 17 00:00:00 2001 From: "Karl E. Nelson" Date: Mon, 11 Jul 2022 13:18:07 -0700 Subject: [PATCH 3/3] Fix typo --- jpype/_jproxy.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/jpype/_jproxy.py b/jpype/_jproxy.py index 79804d459..8c484cae7 100644 --- a/jpype/_jproxy.py +++ b/jpype/_jproxy.py @@ -60,7 +60,7 @@ def _createJProxyDeferred(cls, *intf): instantiation. """ if not isinstance(cls, type): - raise TypeError("InstanceOf only applies to types, not %s" % (type(cls))) + 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 @@ -81,7 +81,8 @@ def _createJProxy(cls, *intf): @JOverride notation on methods evaluated at declaration. """ if not isinstance(cls, type): - raise TypeError("InstanceOf only applies to types, not %s" % (type(cls))) + raise TypeError("JImplements only applies to types, not %s" % (type(cls))) + actualIntf = _prepareInterfaces(cls, intf) def new(tp, *args, **kwargs):