Skip to content

Commit fc2829b

Browse files
committed
Use checknull macro
1 parent f917b8b commit fc2829b

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

src/core.jl

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,25 @@ true if the passed object is null else false
247247
"""
248248
isnull(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+
250269
const JClass = JavaObject{Symbol("java.lang.Class")}
251270
const JObject = JavaObject{Symbol("java.lang.Object")}
252271
const 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

261280
function JString(str::AbstractString)
262-
jstring = JNI.NewStringUTF(String(str))
263-
checknull(jstring)
281+
jstring = @checknull JNI.NewStringUTF(String(str))
264282
return JString(jstring)
265283
end
266284

@@ -324,10 +342,7 @@ isarray(juliaclass::String) = endswith(juliaclass, "[]")
324342

325343
function 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)
332347
end
333348

@@ -339,65 +354,65 @@ end
339354

340355
function 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...)
344359
end
345360

346361
function 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...)
352367
end
353368

354369
function 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"
357372
end
358373

359374
function 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)
361376
end
362377

363378
function 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)
365380
end
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

373388
function 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)
377392
end
378393

379394
function 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)
384399
end
385400

386401
function 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))
388403
end
389404

390405
function 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)
393408
end
394409

395410
function 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))
397412
end
398413

399414
function get_field_id(obj::JavaObject, field::JField, fieldType::Type)
400-
JNI.FromReflectedField(field)
415+
@checknull JNI.FromReflectedField(field)
401416
end
402417

403418
# JField invoke
@@ -447,7 +462,7 @@ global const _jmc_cache = [ Dict{Symbol, JavaMetaClass}() ]
447462

448463
function _metaclass(class::Symbol)
449464
jclass=javaclassname(class)
450-
jclassptr = checknull(JNI.FindClass(jclass))
465+
jclassptr = @checknull JNI.FindClass(jclass)
451466
return JavaMetaClass(class, jclassptr)
452467
end
453468

@@ -467,24 +482,6 @@ javaclassname(class::Symbol) = replace(string(class), "."=>"/")
467482
javaclassname(class::AbstractString) = replace(class, "."=>"/")
468483
javaclassname(::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

489486
function geterror()
490487
isexception = JNI.ExceptionCheck()

0 commit comments

Comments
 (0)