- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
好的。 ScriptEngine.eval(String string)
评估整个字符串,ScriptEngine.eval(Reader reader)
完整地评估来自 Reader
的输入。
因此,如果我有一个文件,我可以打开一个 FileInputStream,将一个 Reader 包裹在它周围,然后调用 scriptEngine.eval(reader)
。
如果我有一个完整的字符串形式的语句,我可以调用 scriptEngine.eval(string)
。
如果我需要实现交互式解释器,我该怎么办?我有一个用户正在交互式地输入多行语句,例如
function f() {
return 3;
}
如果我逐行读取输入,并使用 eval()
的字符串形式,我将最终传递给它不完整的语句,例如function f() {
并得到一个错误。
如果我传入一个 Reader,ScriptEngine
将永远等待输入完成,并且它不是交互式的。
帮助!
澄清一下:这里的问题是我只能通过 ScriptEngine.eval()
完整的语句,作为 ScriptEngine 的客户,我不知道没有输入行何时完成来自 ScriptEngine 本身的一些帮助。
Rhino 的交互式 shell 使用 Rhino 的 Context.stringIsCompilableUnit()
(参见 LXR 的 usage 和 implementation)。
最佳答案
我使用类似于 Rhino 交互式 shell 的相当简单的方法实现了与 Java SE 6 Rhino (Javascript) 和 Jython 1.5.2 (Python) 兼容的东西(请参阅我在问题末尾的评论):
我不想要发生的是:
这是我编写的实现我的方法的辅助类:
import java.lang.reflect.Method;
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
public class ScriptEngineInterpreter
{
private static final boolean DEBUG = false;
final private ScriptEngine engine;
final private Bindings bindings;
final private StringBuilder sb;
private int lineNumber;
private int pendingLineCount;
private boolean expectingMoreInput;
/**
* @param engine ScriptingEngine to use in this interpreter
* @param bindings Bindings to use in this interpreter
*/
public ScriptEngineInterpreter(ScriptEngine engine, Bindings bindings)
{
this.engine = engine;
this.bindings = bindings;
this.sb = new StringBuilder();
this.lineNumber = 0;
reset();
}
/** @return ScriptEngine used by this interpreter */
public ScriptEngine getEngine() { return this.engine; }
protected void reset() {
this.sb.setLength(0);
this.pendingLineCount = 0;
setExpectingMoreInput(false);
}
/** @return whether the interpreter is ready for a brand new statement. */
public boolean isReady() { return this.sb.length() == 0; }
/**
* @return whether the interpreter expects more input
*
* A true value means there is definitely more input needed.
* A false value means no more input is needed, but it may not yet
* be appropriate to evaluate all the pending lines.
* (there's some ambiguity depending on the language)
*/
public boolean isExpectingMoreInput() { return this.expectingMoreInput; }
protected void setExpectingMoreInput(boolean b) { this.expectingMoreInput = b; }
/**
* @return number of lines pending execution
*/
protected int getPendingLineCount() { return this.pendingLineCount; }
/**
* @param lineIsEmpty whether the last line is empty
* @return whether we should evaluate the pending input
* The default behavior is to evaluate if we only have one line of input,
* or if the user enters a blank line.
* This behavior should be overridden where appropriate.
*/
protected boolean shouldEvaluatePendingInput(boolean lineIsEmpty)
{
if (isExpectingMoreInput())
return false;
else
return (getPendingLineCount() == 1) || lineIsEmpty;
}
/**
* @param line line to interpret
* @return value of the line (or null if there is still pending input)
* @throws ScriptException in case of an exception
*/
public Object interpret(String line) throws ScriptException
{
++this.lineNumber;
if (line.isEmpty())
{
if (!shouldEvaluatePendingInput(true))
return null;
}
++this.pendingLineCount;
this.sb.append(line);
this.sb.append("\n");
CompiledScript cs = tryCompiling(this.sb.toString(), getPendingLineCount(), line.length());
if (cs == null)
{
return null;
}
else if (shouldEvaluatePendingInput(line.isEmpty()))
{
try
{
Object result = cs.eval(this.bindings);
return result;
}
finally
{
reset();
}
}
else
{
return null;
}
}
private CompiledScript tryCompiling(String string, int lineCount, int lastLineLength)
throws ScriptException
{
CompiledScript result = null;
try
{
Compilable c = (Compilable)this.engine;
result = c.compile(string);
}
catch (ScriptException se) {
boolean rethrow = true;
if (se.getCause() != null)
{
Integer col = columnNumber(se);
Integer line = lineNumber(se);
/* swallow the exception if it occurs at the last character
* of the input (we may need to wait for more lines)
*/
if (col != null
&& line != null
&& line.intValue() == lineCount
&& col.intValue() == lastLineLength)
{
rethrow = false;
}
else if (DEBUG)
{
String msg = se.getCause().getMessage();
System.err.println("L"+line+" C"+col+"("+lineCount+","+lastLineLength+"): "+msg);
System.err.println("in '"+string+"'");
}
}
if (rethrow)
{
reset();
throw se;
}
}
setExpectingMoreInput(result == null);
return result;
}
private Integer columnNumber(ScriptException se)
{
if (se.getColumnNumber() >= 0)
return se.getColumnNumber();
return callMethod(se.getCause(), "columnNumber", Integer.class);
}
private Integer lineNumber(ScriptException se)
{
if (se.getLineNumber() >= 0)
return se.getLineNumber();
return callMethod(se.getCause(), "lineNumber", Integer.class);
}
static private Method getMethod(Object object, String methodName)
{
try {
return object.getClass().getMethod(methodName);
}
catch (NoSuchMethodException e) {
return null;
/* gulp */
}
}
static private <T> T callMethod(Object object, String methodName, Class<T> cl) {
try {
Method m = getMethod(object, methodName);
if (m != null)
{
Object result = m.invoke(object);
return cl.cast(result);
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
关于java - jsr223 + 编写脚本解释器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5584674/
我正在从类文件中反编译java代码。有一个java语句为: localObject2 = this.current_result_set;jsr 56;jsr 63;return (ResultSet
JSR-223 之间的主要区别是什么?和 JSR-241 ? 如果我理解正确的话: JSR -223:介绍了 Java 脚本语言引擎,仅此而已!您可以自由使用任何您想要的脚本语言,例如:Groovy、
Eclipse 的 null 分析现在支持 JSR-308,即您现在可以将 @Nonull 等放在泛型类型参数上。 但是,广泛使用的 com.google.code.findbugs:jsr305:3
我通过外部服务收到一条 XML 消息,我可以使用 MOXy 作为我的 JAXB 提供程序将其解码为 POJO。我目前能够使用 JPA (Hibernate) 将对象保存到数据库中,并且当遇到无效数据时
在 Java 门户上,您可以拥有包含其他应用程序提供的数据的 portlet。我们想用 Django 应用程序替换我们现有的 Java 门户,这意味着复制 Java 门户显示 portlet 的能力。
我刚开始使用 Java Bean 验证 (JSR-303/JSR-349/Hibernate Validator),并且了解一般概念。但是,我不确定如何验证组合类型的内容与类型本身。 例如: @Not
我是 BlackBerry OS 开发的新手,我的代码试图在传递 Criteria 参数后使用 LocationProvider 获取城市名称。我是根据这个 link 关注的我从 RIM 本身尝试了“
我有一个关于 JSR 交叉文件验证的问题。我有一个基于休息的获取和发布服务。所以我有类似的东西 @GET ItemOfferId getItem(String) 另外一个是 @Post boolean
在提到 Java、JSP 和 JSF 的各个方面时,我经常看到经常使用“JSR-XXX”标题。用于 CDI 的 JSR-299?或用于某些注释的 JSR-303。 通过“JSR”名称来学习这些概念是否
最近我选择了一个非常有用的网络服务框架,Jersey (JAX-RS 又名 jsr-311;及其摇滚启动实现),以及一个漂亮的验证库 Hibernate Validator (“Bean 验证 API
在我的场景中,我尝试创建注释来验证一个字段是否已填充(如果另一个字段具有某种值)。 界面如下所示: @Target({ElementType.FIELD, ElementType.METHOD}) @
谁能解释一下这两者之间的区别:评估规范与构建实现规范对于任何可用的 JSR 流程,例如,JSR 299 . 有时这是有值(value)的信息来源,但对于作为开发人员的我来说,我应该下载和阅读哪一个?令
我正在查看一些 Java 代码,我注意到以下内容: if (!foo(bar, baz, qux)) { i = 0; jsr 433; } javac 对此感到窒息,说这不是一个语句,并且在
我使用 Hibernate Validator 作为 JSR-303 实现。 假设我有: class Form { @Valid private Owner mainOwner;
第 10.6 章中的规范说: Implementation-specific loader The batch runtime implementation must provide an imple
我有一个具有 JSR 168 规范的 portlet,它曾经在 UPortal(一些基于旧门户的 java 框架)内运行。但是,我需要将此 portlet 从门户中删除,并将其作为独立的应用程序。现在
我正在使用 JSR-330 注释编写一些代码,并且我想针对(或使用)各种实现来测试它。 目前我已经完成了 Dagger Dagger 2 hk2 Spring 还有其他实现吗? 请注意,我不是在谈论
我的主要课程是 public class UserAddressesForm { @NotEmpty private String firstName; @NotEmpty
亲爱的 Spring 社区, 我要实现的是以下内容: 我希望每个 Controller 有一个自定义验证器 (via @InitBinder ) 我希望 Spring 调用 validator.val
阅读有关 osgi 的 wiki 页面 https://en.wikipedia.org/wiki/OSGi我看到没有关于OSGi的JSR只有OSGi相关的JSR。这是否意味着 OSGi 规范已超出
我是一名优秀的程序员,十分优秀!