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,189
  • 7
  • 32
  • 38

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,318
  • 1
  • 36
  • 58
gustafc
  • 27,774
  • 7
  • 71
  • 98
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,318
  • 1
  • 36
  • 58
stevestorey
  • 344
  • 2
  • 4