@@ -247,6 +247,25 @@ true if the passed object is null else false
247247"""
248248isnull (obj:: JavaMetaClass ) = Ptr (obj) == C_NULL
249249
250+ macro checknull (expr, msg= " " )
251+ if expr isa Expr && expr. head == :call
252+ return esc (:( checknull ($ expr, $ msg, $ (expr. args[1 ])) ))
253+ else
254+ return esc (:( checknull ($ expr, $ msg) ))
255+ end
256+ end
257+
258+ function checknull (ptr, msg= " Unexpected null pointer from Java Native Interface" , jnifun= nothing )
259+ if isnull (ptr) && geterror () === nothing
260+ if jnifun === nothing
261+ throw (JavaCallError (msg))
262+ else
263+ throw (JavaCallError (" JavaCall.JNI.$jnifun : $msg " ))
264+ end
265+ end
266+ ptr
267+ end
268+
250269const JClass = JavaObject{Symbol (" java.lang.Class" )}
251270const JObject = JavaObject{Symbol (" java.lang.Object" )}
252271const JMethod = JavaObject{Symbol (" java.lang.reflect.Method" )}
@@ -259,8 +278,7 @@ const JString = JavaObject{Symbol("java.lang.String")}
259278# JavaObject(ptr::Ptr{Nothing}) = ptr == C_NULL ? JavaObject(ptr) : JavaObject{Symbol(getclassname(getclass(ptr)))}(ptr)
260279
261280function JString (str:: AbstractString )
262- jstring = JNI. NewStringUTF (String (str))
263- checknull (jstring)
281+ jstring = @checknull JNI. NewStringUTF (String (str))
264282 return JString (jstring)
265283end
266284
@@ -324,10 +342,7 @@ isarray(juliaclass::String) = endswith(juliaclass, "[]")
324342
325343function jnew (T:: Symbol , argtypes:: Tuple = () , args... )
326344 assertroottask_or_goodenv () && assertloaded ()
327- jmethodId = checknull (
328- get_method_id (JNI. GetMethodID, Ptr (metaclass (T)), " <init>" , Nothing, argtypes),
329- " No constructor for $T with signature $sig "
330- )
345+ jmethodId = get_method_id (JNI. GetMethodID, Ptr (metaclass (T)), " <init>" , Nothing, argtypes)
331346 return _jcall (metaclass (T), jmethodId, JavaObject{T}, argtypes, args... ; callmethod= JNI. NewObjectA)
332347end
333348
@@ -339,65 +354,65 @@ end
339354
340355function jcall (ref, method:: AbstractString , rettype:: Type , argtypes:: Tuple = (), args... )
341356 assertroottask_or_goodenv () && assertloaded ()
342- jmethodId = checknull ( get_method_id (ref, method, rettype, argtypes) )
357+ jmethodId = get_method_id (ref, method, rettype, argtypes)
343358 _jcall (_jcallable (ref), jmethodId, rettype, argtypes, args... )
344359end
345360
346361function jcall (ref, method:: JMethod , args... )
347362 assertroottask_or_goodenv () && assertloaded ()
348- jmethodId = checknull ( get_method_id (method) )
363+ jmethodId = get_method_id (method)
349364 rettype = jimport (getreturntype (method))
350365 argtypes = Tuple (jimport .(getparametertypes (method)))
351366 _jcall (_jcallable (ref), jmethodId, rettype, argtypes, args... )
352367end
353368
354369function get_method_id (jnifun, ptr, method:: AbstractString , rettype:: Type , argtypes:: Tuple )
355370 sig = method_signature (rettype, argtypes... )
356- jnifun (ptr, String (method), sig)
371+ @checknull jnifun (ptr, String (method), sig) " $method "
357372end
358373
359374function get_method_id (typ:: Type{JavaObject{T}} , method:: AbstractString , rettype:: Type , argtypes:: Tuple ) where T
360- get_method_id (JNI. GetStaticMethodID, Ptr (metaclass (T)), method, rettype, argtypes)
375+ @checknull get_method_id (JNI. GetStaticMethodID, Ptr (metaclass (T)), method, rettype, argtypes)
361376end
362377
363378function get_method_id (obj:: JavaObject , method:: AbstractString , rettype:: Type , argtypes:: Tuple )
364- get_method_id (JNI. GetMethodID, Ptr (metaclass (obj)), method, rettype, argtypes)
379+ @checknull get_method_id (JNI. GetMethodID, Ptr (metaclass (obj)), method, rettype, argtypes)
365380end
366381
367- get_method_id (method:: JMethod ) = JNI. FromReflectedMethod (method)
382+ get_method_id (method:: JMethod ) = @checknull JNI. FromReflectedMethod (method)
368383
369384# JMethod invoke
370385(m:: JMethod )(obj, args... ) = jcall (obj, m, args... )
371386
372387
373388function jfield (ref, field, fieldType)
374389 assertroottask_or_goodenv () && assertloaded ()
375- jfieldID = checknull ( get_field_id (ref, field, fieldType) )
390+ jfieldID = get_field_id (ref, field, fieldType)
376391 _jfield (_jcallable (ref), jfieldID, fieldType)
377392end
378393
379394function jfield (ref, field)
380395 assertroottask_or_goodenv () && assertloaded ()
381396 fieldType = jimport (gettype (field))
382- jfieldID = checknull ( get_field_id (ref, field, fieldType) )
397+ jfieldID = get_field_id (ref, field, fieldType)
383398 _jfield (_jcallable (ref), jfieldID, fieldType)
384399end
385400
386401function get_field_id (typ:: Type{JavaObject{T}} , field:: AbstractString , fieldType:: Type ) where T
387- JNI. GetStaticFieldID (Ptr (metaclass (T)), String (field), signature (fieldType))
402+ @checknull JNI. GetStaticFieldID (Ptr (metaclass (T)), String (field), signature (fieldType))
388403end
389404
390405function get_field_id (obj:: Type{JavaObject{T}} , field:: JField ) where T
391406 fieldType = jimport (gettype (field))
392- JNI. FromReflectedField (field)
407+ @checknull JNI. FromReflectedField (field)
393408end
394409
395410function get_field_id (obj:: JavaObject , field:: AbstractString , fieldType:: Type )
396- JNI. GetFieldID (Ptr (metaclass (obj)), String (field), signature (fieldType))
411+ @checknull JNI. GetFieldID (Ptr (metaclass (obj)), String (field), signature (fieldType))
397412end
398413
399414function get_field_id (obj:: JavaObject , field:: JField , fieldType:: Type )
400- JNI. FromReflectedField (field)
415+ @checknull JNI. FromReflectedField (field)
401416end
402417
403418# JField invoke
@@ -447,7 +462,7 @@ global const _jmc_cache = [ Dict{Symbol, JavaMetaClass}() ]
447462
448463function _metaclass (class:: Symbol )
449464 jclass= javaclassname (class)
450- jclassptr = checknull ( JNI. FindClass (jclass) )
465+ jclassptr = @ checknull JNI. FindClass (jclass)
451466 return JavaMetaClass (class, jclassptr)
452467end
453468
@@ -467,24 +482,6 @@ javaclassname(class::Symbol) = replace(string(class), "."=>"/")
467482javaclassname (class:: AbstractString ) = replace (class, " ." => " /" )
468483javaclassname (:: Type{T} ) where T <: AbstractVector = JavaCall. signature (T)
469484
470- macro checknull (expr, msg= " " )
471- if expr isa Expr && expr. head == :call
472- :( checknull ($ expr, $ msg, $ (expr. args[1 ])) )
473- else
474- :( checknull ($ expr, $ msg) )
475- end
476- end
477-
478- function checknull (ptr, msg= " Unexpected null pointer from Java Native Interface" , jnifun= nothing )
479- if isnull (ptr) && geterror () === nothing
480- if jnifun === nothing
481- throw (JavaCallError (msg))
482- else
483- throw (JavaCallError (" JavaCall.JNI.$jnifun : $msg " ))
484- end
485- end
486- ptr
487- end
488485
489486function geterror ()
490487 isexception = JNI. ExceptionCheck ()
0 commit comments