合理处理JSR223 Rhino抛出的ScriptException

我开始遇到一个非常有用的JSR223脚本环境的肮脏小秘密。

我正在使用Java 6 SE附带的内置版Rhino,通过JSR223的ScriptingEngine等访问它。

当我得到一个由我导出到Javascript环境的Java对象引起的exception时,它是一个ScriptingException ,它包装了一个包装我真正exception的sun.org.mozilla.javascript.internal.WrappedException (例如UnsupportedOperationException或者其他)

ScriptingException为getFileName()返回null,为getLineNumber()返回-1。 但是当我查看消息和调试器时, WrappedException具有正确的文件名和行号,它只是不通过ScriptingException的getter方法发布它。

大。 现在我该怎么办? 我不知道我将如何使用sun.org.mozilla.javascript.internal.wrappedException,它不是公共类。

哎呀。 Java 6的Rhino使用sun.org.mozilla.javascript.internal.EvaluatorException做同样的事情(不通过ScriptingException的方法发布文件名/行号等),谁知道有多少其他exception。

我能想到的唯一合理的方法是使用reflection。 这是我的解决方案。

 void handleScriptingException(ScriptingException se) { final Throwable t1 = se.getCause(); String lineSource = null; String filename = null; Integer lineNumber = null; if (hasGetterMethod(t1, "sourceName")) { lineNumber = getProperty(t1, "lineNumber", Integer.class); filename = getProperty(t1, "sourceName", String.class); lineSource = getProperty(t1, "lineSource", String.class); } else { filename = se.getFileName(); lineNumber = se.getLineNumber(); } /* do something with this info */ } static private Method getGetterMethod(Object object, String propertyName) { String methodName = "get"+getBeanSuffix(propertyName); try { Class cl = object.getClass(); return cl.getMethod(methodName); } catch (NoSuchMethodException e) { return null; /* gulp */ } } static private String getBeanSuffix(String propertyName) { return propertyName.substring(0,1).toUpperCase() +propertyName.substring(1); } static private boolean hasGetterMethod(Object object, String propertyName) { return getGetterMethod(object, propertyName) != null; } static private  T getProperty(Object object, String propertyName, Class cl) { try { Object result = getGetterMethod(object, propertyName).invoke(object); return cl.cast(result); } catch (Exception e) { e.printStackTrace(); } return null; }