Skip to content

Commit 63026e4

Browse files
authored
Fix lazy jfield, add test (#164)
1 parent 880188d commit 63026e4

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ OrderedCollections.OrderedSet{String} with 2 elements:
3939
"-Xmx1024M"
4040
"-Xrs"
4141

42-
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.
42+
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.
4343

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

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

5050
julia> jcall(out, "println", Nothing, (JString,), "Hello World")

src/core.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ function jfield(ref, field::AbstractString)
424424
assertroottask_or_goodenv() && assertloaded()
425425
field = listfields(ref, field)[]
426426
fieldType = jimport(gettype(field))
427-
jfieldID = get_field_id(ref, field, fieldType)
427+
jfieldID = get_field_id(ref, field)
428428
_jfield(_jcallable(ref), jfieldID, fieldType)
429429
end
430430

test/runtests.jl

+3
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ end
169169
t=JTest(())
170170
t_fields = Dict(getname(f) => f for f in listfields(t))
171171

172+
lazy_out = jfield(System, "out") # Not type stable
173+
@test jcall(System_out, "equals", jboolean, (JObject,), lazy_out) == 0x01
174+
172175
@testset "$ftype" for (name, ftype, valtest) in [ ("booleanField", jboolean, ==(true)) ,
173176
("integerField", jint, ==(100)) ,
174177
("stringField", JString, ==("A STRING")) ,

0 commit comments

Comments
 (0)