2015-05-01 21:57:22 +00:00
|
|
|
from cpython.version cimport PY_MAJOR_VERSION
|
2012-08-20 07:35:14 +00:00
|
|
|
|
|
|
|
def cast(destclass, obj):
|
|
|
|
cdef JavaClass jc
|
|
|
|
cdef JavaClass jobj = obj
|
2015-05-01 21:57:22 +00:00
|
|
|
from .reflect import autoclass
|
|
|
|
if (PY_MAJOR_VERSION < 3 and isinstance(destclass, basestring)) or \
|
|
|
|
(PY_MAJOR_VERSION >=3 and isinstance(destclass, str)):
|
2012-08-20 07:35:14 +00:00
|
|
|
jc = autoclass(destclass)(noinstance=True)
|
|
|
|
else:
|
|
|
|
jc = destclass(noinstance=True)
|
|
|
|
jc.instanciate_from(jobj.j_self)
|
|
|
|
return jc
|
|
|
|
|
2015-05-01 21:57:22 +00:00
|
|
|
def find_javaclass(namestr):
|
|
|
|
namestr = namestr.replace('.', '/')
|
|
|
|
cdef bytes name = str_for_c(namestr)
|
2013-03-13 20:38:10 +00:00
|
|
|
from .reflect import Class
|
2012-08-20 07:35:14 +00:00
|
|
|
cdef JavaClass cls
|
|
|
|
cdef jclass jc
|
|
|
|
cdef JNIEnv *j_env = get_jnienv()
|
|
|
|
|
|
|
|
jc = j_env[0].FindClass(j_env, name)
|
|
|
|
if jc == NULL:
|
|
|
|
raise JavaException('Class not found {0!r}'.format(name))
|
|
|
|
|
|
|
|
cls = Class(noinstance=True)
|
|
|
|
cls.instanciate_from(create_local_ref(j_env, jc))
|
2015-07-26 12:29:51 +00:00
|
|
|
j_env[0].DeleteLocalRef(j_env, jc)
|
2012-08-20 07:35:14 +00:00
|
|
|
return cls
|
|
|
|
|