diff --git a/jpype/_jproxy.py b/jpype/_jproxy.py index 2efa79df4..8c484cae7 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("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. @@ -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): 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):