Fix field dereference when multiple instances of a class exist. Fixes #77

This commit is contained in:
Michał Zieliński 2013-09-16 22:04:56 +02:00
parent 693275fe48
commit 51889323a3
3 changed files with 19 additions and 12 deletions

View File

@ -121,7 +121,7 @@ class MetaJavaClass(type):
jf = value
if not jf.is_static:
continue
jf.set_resolve_info(j_env, jcs.j_cls, None,
jf.set_resolve_info(j_env, jcs.j_cls,
name, __javaclass__)
@ -257,7 +257,7 @@ cdef class JavaClass(object):
jf = value
if jf.is_static:
continue
jf.set_resolve_info(j_env, self.j_cls, self.j_self,
jf.set_resolve_info(j_env, self.j_cls,
name, self.__javaclass__)
def __repr__(self):
@ -272,7 +272,6 @@ cdef class JavaField(object):
cdef jfieldID j_field
cdef JNIEnv *j_env
cdef jclass j_cls
cdef LocalRef j_self
cdef bytes definition
cdef object is_static
cdef bytes name
@ -281,20 +280,18 @@ cdef class JavaField(object):
def __cinit__(self, definition, **kwargs):
self.j_field = NULL
self.j_cls = NULL
self.j_self = None
def __init__(self, definition, **kwargs):
super(JavaField, self).__init__()
self.definition = definition
self.is_static = kwargs.get('static', False)
cdef void set_resolve_info(self, JNIEnv *j_env, jclass j_cls, LocalRef j_self,
cdef void set_resolve_info(self, JNIEnv *j_env, jclass j_cls,
bytes name, bytes classname):
j_env = get_jnienv()
self.name = name
self.classname = classname
self.j_cls = j_cls
self.j_self = j_self
cdef void ensure_field(self) except *:
cdef JNIEnv *j_env = get_jnienv()
@ -312,12 +309,16 @@ cdef class JavaField(object):
raise JavaException('Unable to found the field {0}'.format(self.name))
def __get__(self, obj, objtype):
cdef jobject j_self
self.ensure_field()
if obj is None:
return self.read_static_field()
return self.read_field()
cdef read_field(self):
j_self = (<JavaClass?>obj).j_self.obj
return self.read_field(j_self)
cdef read_field(self, jobject j_self):
cdef jboolean j_boolean
cdef jbyte j_byte
cdef jchar j_char
@ -332,7 +333,6 @@ cdef class JavaField(object):
cdef object ret = None
cdef JavaObject ret_jobject
cdef JavaClass ret_jc
cdef jobject j_self = self.j_self.obj
cdef JNIEnv *j_env = get_jnienv()
# return type of the java method
@ -837,5 +837,3 @@ class JavaStaticField(JavaField):
def __init__(self, definition, **kwargs):
kwargs['static'] = True
super(JavaStaticField, self).__init__(definition, **kwargs)

View File

@ -43,6 +43,11 @@ public class BasicsTest {
public double fieldD = 1.23456789;
public String fieldString = new String("helloworld");
public BasicsTest() {}
public BasicsTest(byte fieldBVal) {
fieldB = fieldBVal;
}
public boolean[] methodArrayZ() {
boolean[] x = new boolean[3];
x[0] = x[1] = x[2] = true;
@ -93,7 +98,7 @@ public class BasicsTest {
public boolean methodParamsZBCSIJFD(boolean x1, byte x2, char x3, short x4,
int x5, long x6, float x7, double x8) {
// ADD float / double, but dunno how to do with approx
return (x1 == true && x2 == 127 && x3 == 'k' && x4 == 32767 &&
return (x1 == true && x2 == 127 && x3 == 'k' && x4 == 32767 &&
x5 == 2147483467 && x6 == 2147483467);
}

View File

@ -50,6 +50,10 @@ class BasicsTest(unittest.TestCase):
self.assertAlmostEquals(test.fieldF, 1.23456789)
self.assertEquals(test.fieldD, 1.23456789)
self.assertEquals(test.fieldString, 'helloworld')
test2 = autoclass('org.jnius.BasicsTest')(10)
self.assertEquals(test2.fieldB, 10)
self.assertEquals(test.fieldB, 127)
self.assertEquals(test2.fieldB, 10)
def test_instances_methods_array(self):
test = autoclass('org.jnius.BasicsTest')()