Can you extend the abstract Java classes from JavaScript?

I am working on a project that uses javax.script packages very much. *. I have a situation where I would like to create JavaScript objects that extend an abstract Java class, similar to how you can use Invocable.getInterface to create JavaScript objects that implement Java interfaces. Is it possible? And if so, how do you do it?

+4
source share
2 answers

If you do not want to follow the path of generating bytecode at runtime (using BCEL , as shown below), then no. You can do this using interfaces using proxy classes , but there is no equivalent for abstract classes.

If you really want to try BCEL, your best strategy is this:

  • Write a method that BCEL uses to generate byte[] bytecode for a new class that extends the abstract class and delegates each abstract JavaScript method.
  • Define a naming convention that binds abstract classes with a wrapper, for example. foo.MyAbstractClass corresponds to foo.MyAbstractClassDynamicLangWrapper .
  • Move a ClassLoader that implements findClass to recognize this naming convention and generate class bytes and defineClass calls
  • Make sure your scripting language uses your custom class loader to resolve class names in scripts. I think in Rhino you are using setApplicationClassLoader , but I'm not sure.
0
source

Yes, you can; The previous poster is incorrect. See the documentation for the JavaAdapter .

+2
source

All Articles