3

I have a class such the one below and I would like to override both methods using Javascript Rhino, any suggestions?

Thanks in advance!!

class MyClass() {
  public void method1(); 
  public void method2(); 

  MyClass() {
    method1();
    method2();
  } 
}
biquillo
  • 6,469
  • 7
  • 37
  • 40

2 Answers2

2

The last time I used it, the JDK bundled Rhino didn't support extending classes (only implementing SAM interfaces), but Mozilla's Rhino does support overriding classes. Check out the JavaAdapter: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scripting_Java#The_JavaAdapter_Constructor

xverges
  • 4,608
  • 1
  • 39
  • 60
gustafc
  • 28,465
  • 7
  • 73
  • 99
1

While I've not tried this in an Android environment, Rhino does support a JavaAdapter construct - as seen at: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scripting_Java#The_JavaAdapter_Constructor which allows you to create a JS object defining the correct methods, and then to create effectively a wrapper between a single superclass, and/or one or more interfaces. In this case, something like:

var o = {
  method1: function() {
    print('method1');
  },
  method2: function() {
    print('method2');
  }
}

//This line should instantiate the child class, with
//method1 + method2 overridden called within the constructor
var instanceThatIsAMyClass = new JavaAdapter(com.example.MyClass, o);
xverges
  • 4,608
  • 1
  • 39
  • 60
stevestorey
  • 448
  • 3
  • 7