Skip to content

Commit

Permalink
Fix lazy jfield, add test (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkitti authored Mar 21, 2022
1 parent 880188d commit 63026e4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ OrderedCollections.OrderedSet{String} with 2 elements:
"-Xmx1024M"
"-Xrs"

julia> JavaCall.init() # Optionally, explicitly initialize the JVM. Do not use this in package `__init__()` to allow other packages to add class paths or options.
julia> JavaCall.init() # Call before using `jcall` or `jfield`. Do not use this in package `__init__()` to allow other packages to add classpaths or options.

julia> jls = @jimport java.lang.System
JavaObject{Symbol("java.lang.System")}

julia> out = jfield(jls, "out", @jimport java.io.PrintStream)
julia> out = jfield(jls, "out", @jimport java.io.PrintStream) # Third arg is optional, but helps type stability.
JavaObject{Symbol("java.io.PrintStream")}(JavaCall.JavaLocalRef(Ptr{Nothing} @0x0000000003ecda38))

julia> jcall(out, "println", Nothing, (JString,), "Hello World")
Expand Down
2 changes: 1 addition & 1 deletion src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ function jfield(ref, field::AbstractString)
assertroottask_or_goodenv() && assertloaded()
field = listfields(ref, field)[]
fieldType = jimport(gettype(field))
jfieldID = get_field_id(ref, field, fieldType)
jfieldID = get_field_id(ref, field)
_jfield(_jcallable(ref), jfieldID, fieldType)
end

Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ end
t=JTest(())
t_fields = Dict(getname(f) => f for f in listfields(t))

lazy_out = jfield(System, "out") # Not type stable
@test jcall(System_out, "equals", jboolean, (JObject,), lazy_out) == 0x01

@testset "$ftype" for (name, ftype, valtest) in [ ("booleanField", jboolean, ==(true)) ,
("integerField", jint, ==(100)) ,
("stringField", JString, ==("A STRING")) ,
Expand Down

0 comments on commit 63026e4

Please sign in to comment.