Skip to content
Browse files

Convert JInterface to Meta class.

  • Loading branch information
Thrameos committed Jan 20, 2020
1 parent b19e984 commit d8323e7e7a5fe02582992055dabd5a18f182721c
@@ -5,6 +5,8 @@ This changelog *only* contains changes from the *first* pypi release ( o

- **Next version - unreleased**

- JInterface is now a meta class. Use isinstance(cls, JInterface)
to test for interfaces.

- **0.7.1 - 12-16-2019**

@@ -196,10 +196,6 @@ def mro(cls):
# Include the remaining that we still need to consider
parents.extend([b for b in prev if not b in parents])

# JavaObjects are not interfaces, so we need to remove the JavaInterface inheritance
if _JObject in out and JInterface in out:

return out

def __repr__(self):
@@ -250,51 +246,35 @@ def _JClassNew(arg, loader=None, initialize=True):
return cls

class JInterface(object):
"""Base class for all Java Interfaces.
class JInterfaceMeta(type):
def __instancecheck__(self, obj):
if not hasattr(obj, '__javaclass__'):
return False
return obj.__javaclass__.isInterface()

def __subclasscheck__(self, obj):
# This is included for compatiblity with JPype 0.7.x
if not hasattr(obj, '__javaclass__'):
return False
return obj.__javaclass__.isInterface()

``JInterface`` is serves as the base class for any java class that is
class JInterface(metaclass=JInterfaceMeta):
"""A meta class for all Java Interfaces.
``JInterface`` is serves as the base class for any Java class that is
a pure interface without implementation. It is not possible to create
a instance of a java interface. The ``mro`` is hacked such that
``JInterface`` does not appear in the tree of objects implement an
a instance of a Java interface.
.. code-block:: python
if issubclass(java.util.function.Function, jpype.JInterface):
if isinstance(java.util.function.Function, jpype.JInterface):
print("is interface")
Use ``isinstance(obj, jpype.JavaInterface)`` to test for a interface.
def class_(self):
return _JObject(self.__javaclass__)

def __new__(cls, *args, **kwargs):
return super(JInterface, cls).__new__(cls)

def __init__(self, *args, **kwargs):
if len(args) == 1 and isinstance(args[0], _jpype.PyJPValue):
object.__setattr__(self, '__javavalue__', args[0])
elif not hasattr(self, '__javavalue__'):
raise JClass("java.lang.InstantiationException")(
"`%s` is an interface." % str(self.class_.getName()))
super(JInterface, self).__init__()

def __str__(self):
return self.toString()

def __hash__(self):
return self.hashCode()

def __eq__(self):
return self.equals(o)

def __ne__(self):
return not self.equals(o)

def _JClassFactory(name, jc):
@@ -312,7 +292,7 @@ def _JClassFactory(name, jc):
elif bjc is not None:
elif bjc is None:
itf = jc.getInterfaces()
for ic in itf:
@@ -41,3 +41,6 @@ def __ge__(self, o):

def __le__(self, o):
return self.compareTo(o) <= 0

def __hash__(self):
return self.hashCode()
@@ -0,0 +1,45 @@
<project default="all" name="JPype - Native">

<!-- JAVA_VERSION must match the oldest supported Java version -->
<property name="JAVA_VERSION" value="8" />

<!-- src can't be "java" as it breaks nose tests -->
<property name="src" location="java"/>
<property name="build" location="build"/>

<target name="test" depends="compile">

<target name="compile">
<mkdir dir="${build}/classes"/>
<mkdir dir="${build}/lib"/>
<javac destdir="${build}/classes"
<src path="${src}"/>
<javac destdir="${build}/lib"
<src path="${src}"/>

<target name="jar" depends="compile">
<jar destfile="native/org.jpype.jar"
basedir="${build}/classes" />

<target name="all" depends="jar" />

<target name="clean">
<delete dir="${build}"/>

@@ -38,7 +38,6 @@
#include <pyjp_module.h>
#include <pyjp_array.h>
#include <pyjp_class.h>
#include <pyjp_field.h>
#include <pyjp_method.h>
#include <pyjp_module.h>
@@ -51,6 +50,12 @@ extern "C"

struct PyJPClass
JPClass* m_Class;
} ;

struct PyJPProxy
@@ -60,6 +65,8 @@ struct PyJPProxy
} ;

extern PyTypeObject* PyJPProxy_Type;
extern PyTypeObject* PyJPClass_Type;

#ifdef __cplusplus
@@ -68,4 +75,6 @@ extern PyTypeObject* PyJPProxy_Type;
#define ASSERT_JVM_RUNNING(X) JPEnv::assertJVMRunning(X, JP_STACKINFO())
#define PY_STANDARD_CATCH catch(...) { JPPythonEnv::rethrow(JP_STACKINFO()); }

JPPyObject PyJPClass_alloc(JPClass* cls);

#endif /* PYJP_H */

This file was deleted.

@@ -82,7 +82,7 @@ JPPyObject JPPythonEnv::newJavaClass(JPClass* javaClass)

JPPyTuple args(JPPyTuple::newTuple(1));
args.setItem(0, PyJPClass::alloc(javaClass).get());
args.setItem(0, PyJPClass_alloc(javaClass).get());

// calls jpype._jclass._getClassFor(_jpype.PyJPClass)
if (s_Resources->s_GetClassMethod.isNull())
@@ -112,12 +112,12 @@ JPValue* JPPythonEnv::getJavaValue(PyObject* obj)
JPClass* JPPythonEnv::getJavaClass(PyObject* obj)
JPPyObject vobj(JPPyRef::_use, obj);
if (Py_TYPE(obj) == &PyJPClass::Type)
if (Py_TYPE(obj) == PyJPClass_Type)
return ((PyJPClass*) obj)->m_Class;
if (!JPPyObject::hasAttrString(obj, __javaclass__))
return NULL;
JPPyObject self(JPPyObject::getAttrString(obj, __javaclass__));
if (Py_TYPE(self.get()) == &PyJPClass::Type)
if (Py_TYPE(self.get()) == PyJPClass_Type)
return ((PyJPClass*) self.get())->m_Class;

0 comments on commit d8323e7

Please sign in to comment.
You can’t perform that action at this time.