Skip to content

Commit 53a85f5

Browse files
committed
Focus macro checknull on a single get_method_id method
1 parent fc2829b commit 53a85f5

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/core.jl

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,22 @@ isnull(obj::JavaMetaClass) = Ptr(obj) == C_NULL
249249

250250
macro checknull(expr, msg="")
251251
if expr isa Expr && expr.head == :call
252-
return esc(:( checknull($expr, $msg, $(expr.args[1])) ))
252+
jnifun = "$(expr.args[1])"
253+
quote
254+
local ptr = $(esc(expr))
255+
if isnull(ptr) && geterror() === nothing
256+
throw(JavaCallError("JavaCall."*$jnifun*": "*$(esc(msg))))
257+
end
258+
ptr
259+
end
253260
else
254-
return esc(:( checknull($expr, $msg) ))
261+
quote
262+
local ptr = $(esc(expr))
263+
if isnull(ptr) && geterror() === nothing
264+
throw(JavaCallError($(esc(msg))))
265+
end
266+
ptr
267+
end
255268
end
256269
end
257270

@@ -342,7 +355,7 @@ isarray(juliaclass::String) = endswith(juliaclass, "[]")
342355

343356
function jnew(T::Symbol, argtypes::Tuple = () , args...)
344357
assertroottask_or_goodenv() && assertloaded()
345-
jmethodId = get_method_id(JNI.GetMethodID, Ptr(metaclass(T)), "<init>", Nothing, argtypes)
358+
jmethodId = get_method_id(JNI.GetMethodID, T, "<init>", Nothing, argtypes)
346359
return _jcall(metaclass(T), jmethodId, JavaObject{T}, argtypes, args...; callmethod=JNI.NewObjectA)
347360
end
348361

@@ -366,17 +379,18 @@ function jcall(ref, method::JMethod, args...)
366379
_jcall(_jcallable(ref), jmethodId, rettype, argtypes, args...)
367380
end
368381

369-
function get_method_id(jnifun, ptr, method::AbstractString, rettype::Type, argtypes::Tuple)
382+
function get_method_id(jnifun::Function, obj, method::AbstractString, rettype::Type, argtypes::Tuple)
370383
sig = method_signature(rettype, argtypes...)
371-
@checknull jnifun(ptr, String(method), sig) "$method"
384+
ptr = Ptr(metaclass(obj))
385+
@checknull jnifun(ptr, String(method), sig) "Problem getting method id for $obj.$method with signature $sig"
372386
end
373387

374388
function get_method_id(typ::Type{JavaObject{T}}, method::AbstractString, rettype::Type, argtypes::Tuple) where T
375-
@checknull get_method_id(JNI.GetStaticMethodID, Ptr(metaclass(T)), method, rettype, argtypes)
389+
get_method_id(JNI.GetStaticMethodID, T, method, rettype, argtypes)
376390
end
377391

378392
function get_method_id(obj::JavaObject, method::AbstractString, rettype::Type, argtypes::Tuple)
379-
@checknull get_method_id(JNI.GetMethodID, Ptr(metaclass(obj)), method, rettype, argtypes)
393+
get_method_id(JNI.GetMethodID, obj, method, rettype, argtypes)
380394
end
381395

382396
get_method_id(method::JMethod) = @checknull JNI.FromReflectedMethod(method)

0 commit comments

Comments
 (0)