Search code examples
scalajpype

modify Java/Scala class variables with JPype?


I've been trying to use JPype to interface with Scala, and it's been going well, but I've been having trouble modifying class/object variables. To get the value out of them, you have to invoke them as a function (e.g. myObj.var() might give 5). I haven't been able to figure out how to reassign something to myObj.var, since it is of some JavaBoundMethod type or something like that. Is this possible with JPype?


Solution

  • If the member has really been declared as a var (and not as a val), there should be a method named myObj.var_$eq() which you can invoke.

    This code ...

    class Foo {
      var i: Int = 0
    }
    

    ... ends up like this in the bytecode:

    scala> :javap -s Foo
    Compiled from "<console>"
    public class Foo {
      public int i();
      public void i_$eq(int);
      public Foo();
    }