timob / jnigi

Golang Java JNI library
BSD 2-Clause "Simplified" License
163 stars 44 forks source link

New `CallMethod` does not seem to base the call on a casted object ref #60

Closed oxisto closed 2 years ago

oxisto commented 2 years ago

I have a simple class hierarchy in my project, e.g., ObjectType which derives from Type. ObjectType has a function addGeneric which takes a Type as parameter and returns void.

I am trying to cast-call this method on an object ref, but it seems that the base class is chosen for the method call, rather than the casted class:

t.Cast("de/fraunhofer/aisec/cpg/graph/types/ObjectType").CallMethod(env, "addGeneric", nil, g.Cast("de/fraunhofer/aisec/cpg/graph/types/Type"))
java.lang.NoSuchMethodError: Lde/fraunhofer/aisec/cpg/graph/types/Type;.addGeneric(Lde/fraunhofer/aisec/cpg/graph/types/Type;)V
oxisto commented 2 years ago

I suspect, that this line here: https://github.com/timob/jnigi/blob/1481db6c0949c942d83600a594c1664f92b337b1/jnigi.go#L1119

should probably refer o.GetClassName() rather than o.className so the casted object ref can set the class.

timob commented 2 years ago

Yes that's a problem.

We can't just change o.className to o.GetClassName() on the above line because it will refer to the method defined on *ObjectRef.

I think the way to fix this is to just have:

func (o *ObjectRef) Cast(className string) *ObjectRef {
    return &ObjectRef{o.jobject, className, o.isArray}
}

And remove CastedObjectRef type all together.

oxisto commented 2 years ago

Yes that's a problem.

We can't just change o.className to o.GetClassName() on the above line because it will refer to the method defined on *ObjectRef.

I think the way to fix this is to just have:

func (o *ObjectRef) Cast(className string) *ObjectRef {
  return &ObjectRef{o.jobject, className, o.isArray}
}

And remove CastedObjectRef type all together.

Sounds like a good solution.