Skip to content

Commit 0701a5c

Browse files
committed
consolidate _jfiel w _jcall, cleanup args function
1 parent 988b0c1 commit 0701a5c

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

src/core.jl

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ function jnew(T::Symbol, argtypes::Tuple = () , args...)
332332
if jmethodId == C_NULL
333333
throw(JavaCallError("No constructor for $T with signature $sig"))
334334
end
335-
return _jnicall(metaclass(T), jmethodId, JNI.NewObjectA, JavaObject{T}, argtypes, args...)
335+
return _jcall(metaclass(T), jmethodId, JavaObject{T}, argtypes, args...; callmethod=JNI.NewObjectA)
336336
end
337337

338338
_jcallable(typ::Type{JavaObject{T}}) where T = metaclass(T)
@@ -411,23 +411,6 @@ end
411411
# JField invoke
412412
(f::JField)(obj) = jfield(obj, f)
413413

414-
function _jfield(obj, jfieldID::Ptr{Nothing}, fieldType::Type)
415-
result = _jnifield(obj, jfieldID, fieldType)
416-
result==C_NULL && geterror()
417-
return convert_result(fieldType, result)
418-
end
419-
420-
function _jcall(obj, jmethodId, callmethod, rettype, argtypes, args...)
421-
savedArgs, convertedArgs = convert_args(argtypes, args...)
422-
GC.@preserve savedArgs begin
423-
result = _jnicall(obj, jmethodId, rettype, argtypes, jvalue.(convertedArgs))
424-
end
425-
deleteref.(filter(x->isa(x,JavaObject),convertedArgs))
426-
result==C_NULL && geterror()
427-
return convert_result(rettype, result)
428-
end
429-
430-
#Generate these methods to satisfy ccall's compile time constant requirement
431414
for (x, name) in [(:Type, "Object"),
432415
(:(Type{jboolean}), "Boolean"),
433416
(:(Type{jchar}), "Char" ),
@@ -443,18 +426,29 @@ for (x, name) in [(:Type, "Object"),
443426
callmethod = :(JNI.$(Symbol(cstr)))
444427
fieldmethod = :(JNI.$(Symbol(fstr)))
445428
m = quote
446-
function _jnicall(obj::T, jmethodId::Ptr{Nothing}, rettype::$x,
447-
argtypes::Tuple, args) where T <: $t
448-
$callmethod(Ptr(obj), jmethodId, Array{JNI.jvalue}(args))
429+
function _jfield(obj::T, jfieldID::Ptr{Nothing}, fieldType::$x) where T <: $t
430+
result = $fieldmethod(Ptr(obj), jfieldID)
431+
result==C_NULL && geterror()
432+
return convert_result(fieldType, result)
449433
end
450-
function _jnifield(obj::T, jfieldID::Ptr{Nothing}, fieldType::$x) where T <: $t
451-
$fieldmethod(Ptr(obj), jfieldID)
434+
function _jcall(obj::T, jmethodId::Ptr{Nothing}, rettype::$x,
435+
argtypes::Tuple, args...; callmethod=$callmethod) where T <: $t
436+
savedArgs, convertedArgs = convert_args(argtypes, args...)
437+
GC.@preserve savedArgs begin
438+
result = callmethod(Ptr(obj), jmethodId, Array{JNI.jvalue}(jvalue.(convertedArgs)))
439+
end
440+
cleanup_arg.(convertedArgs)
441+
result==C_NULL && geterror()
442+
return convert_result(rettype, result)
452443
end
453444
end
454445
eval(m)
455446
end
456447
end
457448

449+
cleanup_arg(arg) = nothing
450+
cleanup_arg(arg::JavaObject) = deleteref(arg)
451+
458452
global const _jmc_cache = [ Dict{Symbol, JavaMetaClass}() ]
459453

460454
function _metaclass(class::Symbol)

0 commit comments

Comments
 (0)