views:

62

answers:

1

Hello, I use Java's ScriptEngine to execute JavaScript Code. I use the Invocable Interface, so that I can use the Script Code as a normal Java Object implementing a Java Interface.

If the JavaScript Code

  • is invalid
  • does not follow the interface (missing methods, wrong return type, throws an exception etc.)

I get an internal Exception from Rhino or an UndeclaredThrowableException while executing the code. Both are RuntimeExceptions, which are not "allowed" to catch.

Is there a way to validate the code before executing? Or do I have to break the rule here and catch RuntimeExceptions? That would work for me, but what is the most elegant way?

A: 

Is there a way to validate the code before executing?

No. JavaScript is loosely typed, has no concept of interfaces and is interpreted on the fly.

Without designing your own validation framework, the best you can do is check it for syntax errors with JSLint.

Kevin