JuliaInterop / JavaCall.jl

Call Java from Julia
http://juliainterop.github.io/JavaCall.jl
Other
118 stars 53 forks source link

handling of Enum types #159

Closed MatFi closed 2 years ago

MatFi commented 2 years ago

I am correct with the assumption that there is currently no way to handle Java Enum types. If I am wrong here, can someone provide me an example please?

In my case the specific problems appear when working with this Java object.

aviks commented 2 years ago

No, we do not have any specific support for it as far as I know. I imagine you can get the id and name fields of the Enum and work with that?

For full support, we probably would want to write some convert methods here

Also, separately, I guess you have seen Taro.jl?

mkitti commented 2 years ago

We may have some low level support in https://github.com/JuliaInterop/JavaCall.jl/blob/master/src/JNI.jl . Essentially the question comes down to how you would work with Enum types via JNI. If you can link that documentation, we can build up from there.

MatFi commented 2 years ago

No, we do not have any specific support for it as far as I know. I imagine you can get the id and name fields of the Enum and work with that?

Sadly this seems not sufficient to me as I can not get set the fields this way (not an Java expert).

Also, separately, I guess you have seen Taro.jl?

Thanks, in fact Taro.jl is what I want to extend with some formatting features, this is why I came across this

mkitti commented 2 years ago

This look informative: https://stackoverflow.com/questions/11225261/how-to-return-enum-from-jni

MatFi commented 2 years ago

This look informative: https://stackoverflow.com/questions/11225261/how-to-return-enum-from-jni

Worked for me! Thanks a lot!!

class = Symbol("org.apache.poi.ss.usermodel.ConditionalFormattingThreshold\$RangeType")
jclass = JavaCall.javaclassname(class)
jclassptr = JavaCall.JNI.FindClass(jclass)
jfieldID = JavaCall.JNI.GetStaticFieldID(jclassptr,"PERCENT",JavaCall.signature(JavaObject{class}))
jenum = JavaCall.JNI.GetStaticObjectField(jclassptr,jfieldID)
ret = JavaCall.convert_result(JavaObject{class},jenum)
MatFi commented 2 years ago

After some investigation I noticed that it is also possible to simply use the jfield function. so this JavaCall.jl actually supports enums to some extend

enum = @jimport "org.apache.poi.ss.usermodel.ConditionalFormattingThreshold\$RangeType"
ret = jfield(enum,"MAX",enum)
mkitti commented 2 years ago

Could you do

enum = @jimport raw"org.apache.poi.ss.usermodel.ConditionalFormattingThreshold$RangeType"
ret = jfield(enum,"MAX",enum)
mkitti commented 2 years ago

Perhaps another approach would be to use listfields(enum, "MAX")