合理处理JSR223 Rhino抛出的ScriptException [英] reasonable handling of ScriptException thrown by JSR223 Rhino

查看:208
本文介绍了合理处理JSR223 Rhino抛出的ScriptException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm starting to run into the dirty little secrets of what is an otherwise very useful JSR223 scripting environment.

我正在使用内置版本的Rhino发货使用Java 6 SE,通过JSR223的 ScriptingEngine 等来访问它。

I'm using the builtin version of Rhino shipped with Java 6 SE, accessing it through JSR223's ScriptingEngine et al.

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

When I get an exception caused by a Java object I've exported into the Javascript environment, it is a ScriptingException that wraps a sun.org.mozilla.javascript.internal.WrappedException that wraps my real exception (e.g. UnsupportedOperationException or whatever)

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

The ScriptingException returns null for getFileName() and -1 for getLineNumber(). But when I look at the message and at the debugger, the WrappedException has the correct filename and line number, it's just not publishing it via the ScriptingException's getter methods.

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

Great. Now what do I do? I don't know how I'm going to use sun.org.mozilla.javascript.internal.wrappedException which isn't a public class anyway.

推荐答案

Argh。 Java 6的Rhino使用 sun.org.mozilla.javascript.internal.EvaluatorException 执行相同的操作(不通过ScriptingException的方法发布文件名/行号等)谁知道有多少其他例外。

Argh. Java 6's Rhino does the same thing (doesn't publish the file name / line number / etc via ScriptingException's methods) with sun.org.mozilla.javascript.internal.EvaluatorException and who knows how many other exceptions.

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

The only reasonable way I can think of to handle this is to use reflection. Here's my solution.

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> T getProperty(Object object, String propertyName, 
        Class<T> cl) {
    try {
        Object result = getGetterMethod(object, propertyName).invoke(object);
        return cl.cast(result);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

这篇关于合理处理JSR223 Rhino抛出的ScriptException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆