java - 使用Rhino,我遇到了 "ReferenceError"异常

标签 java javascript rhino

我在使用 Rhino(17R4) 时遇到错误。

我的目标是使用 jsbeautifier.js 缩进 javascript 源代码.

这是我的代码:

import java.io.InputStream;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

public class JSBeautifier {
public String beautify(String jsSource) {
    Context context = Context.enter();
    Scriptable globalScope = context.initStandardObjects();
    String beautify = getFileContents(JSBeautifier.class.getResourceAsStream("beautify.js"));
    context.evaluateString(globalScope, beautify, "beautify", 1, null);
    globalScope.put("source", globalScope, jsSource);

    context.evaluateString(globalScope, "result = js_beautify(source);", "beautify", 1, null);
    Object result = globalScope.get("result", globalScope);

    return (String)result;
}

private String getFileContents(InputStream stream) {
    StringBuffer contents = new StringBuffer("");
    try {
        byte[] readBytes = new byte[1024];
        int i = 0;
        while ((i = stream.read(readBytes)) > 0)
            contents.append(new String(readBytes, 0, i));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return contents.toString();
}
}

当我执行此代码时,发生了以下异常。

org.mozilla.javascript.EcmaError: ReferenceError: "js_beautify" is not defined. (beautify#1)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)
at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3750)
at org.mozilla.javascript.ScriptRuntime.getNameFunctionAndThis(ScriptRuntime.java:2176)
at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:61)
at org.mozilla.javascript.gen.beautify_2._c_script_0(beautify:1)
at org.mozilla.javascript.gen.beautify_2.call(beautify)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
at org.mozilla.javascript.gen.beautify_2.call(beautify)
at org.mozilla.javascript.gen.beautify_2.exec(beautify)
at org.mozilla.javascript.Context.evaluateString(Context.java:1079)
at test.util.jsconverter.JSBeautifier.beautify(JSBeautifier.java:20)

如何解决这个问题?

最佳答案

问题是 beautify.js正在将其函数分配给全局范围内的变量

if (typeof define === "function") {
     // Add support for require.js
    define(function(require, exports, module) {
        exports.js_beautify = js_beautify;
    });
} else if (typeof exports !== "undefined") {
    // Add support for CommonJS. Just put this file somewhere on your require.paths
    // and you will be able to `var js_beautify = require("beautify").js_beautify`.
    exports.js_beautify = js_beautify;
} else if (typeof window !== "undefined") {
    // If we're running a web page and don't have either of the above, add our one global
    window.js_beautify = js_beautify;
} else if (typeof global !== "undefined") {
    // If we don't even have window, try global.
    global.js_beautify = js_beautify;
}

所以,你必须创建一个全局变量,它可以将其功能分配给

context.evaluateString(globalScope, "var global = {};", "global", 1, null);
context.evaluateString(globalScope, beautify, "beautify", 1, null);
globalScope.put("source", globalScope, jsSource);
context.evaluateString(globalScope, "result = global.js_beautify(source);", "beautify", 1, null);

关于java - 使用Rhino,我遇到了 "ReferenceError"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16337496/

相关文章:

java - 编写java脚本: Create a class in a script file

javascript - 使用 Javascript 将 HTML 字符串加载到 DOM 树中

java - 如何将 Intent 过滤器添加到 Flex android 项目 list 中?

java - 如何在java中定义一个有两列的arrayList?

javascript - S3 使用 aws-sdk v3 为 PutObject 命令预签名 url 提供 SignatureDoesNotMatch 错误

javascript - 下拉菜单停止工作

java - Applet 和动态类加载问题

java - 如何在 Eclipse 中拆分字符串并将项目添加到 ListView(用于循环/迭代)?

javascript - 如何将 rowGroup 与 DataTables 一起使用,以便 rowGroup 分隔符右对齐?

java - 无法使用 javascript 从 Java 中解析完整的 javascript if 语句