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

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

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

Interpreter.setClassLoader介绍

[英]Set an external class loader to be used as the base classloader for BeanShell. The base classloader is used for all classloading unless/until the addClasspath()/setClasspath()/reloadClasses() commands are called to modify the interpreter's classpath. At that time the new paths /updated paths are added on top of the base classloader.

BeanShell will use this at the same point it would otherwise use the plain Class.forName(). i.e. if no explicit classpath management is done from the script (addClassPath(), setClassPath(), reloadClasses()) then BeanShell will only use the supplied classloader. If additional classpath management is done then BeanShell will perform that in addition to the supplied external classloader. However BeanShell is not currently able to reload classes supplied through the external classloader.
[中]设置一个外部类加载器作为BeanShell的基本类加载器。基本类加载器用于所有类加载,除非调用addClasspath()/setClasspath()/reloadClasses()命令修改解释器的类路径。此时,新路径/更新路径被添加到基本类加载器的顶部。
BeanShell将在使用普通类的同时使用它。forName()。i、 e.如果没有从脚本(addClassPath()、setClassPath()、reloadClasses())执行显式类路径管理,则BeanShell将只使用提供的类加载器。如果完成了额外的类路径管理,那么BeanShell将在提供的外部类加载器之外执行该操作。但是,BeanShell目前无法重新加载通过外部类加载器提供的类。

代码示例

代码示例来源: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

interpreter.setClassLoader(classLoader);
Object result = interpreter.eval(scriptSource);
if (result != 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

/**
 * 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: org.springframework/spring-context

interpreter.setClassLoader(classLoader);
Object result = interpreter.eval(scriptSource);
if (result != null) {

代码示例来源: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: org.andromda/andromda-script-wrappers

/**
 * Initializes the interpreter.
 *
 * @param stub the stub class.
 * @param scriptPath the path to the script file.
 * @return the initialized interpreter.
 */
private final Interpreter initialize(
  Object stub,
  String scriptPath)
{
  final Interpreter interpreter = new Interpreter();
  interpreter.setClassLoader(stub.getClass().getClassLoader());
  return interpreter;
}

代码示例来源:origin: uk.org.mygrid.taverna.processors/taverna-beanshell-processor

public BeanshellTask(Processor p) throws ClassNotFoundException {
  this.proc = (BeanshellProcessor) p;
  ClassLoader classLoader = proc.findClassLoader();
  interpreter = new Interpreter();
  interpreter.setClassLoader(classLoader);
  logger.info("Beanshell using classloader " + classLoader);
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * 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: apache/servicemix-bundles

@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: apache/servicemix-bundles

interpreter.setClassLoader(classLoader);
Object result = interpreter.eval(scriptSource);
if (result != null) {

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

/**
 *
 */
public JRBshEvaluator(String bshScript) throws JRException
{
  super();
  
  this.bshScript = bshScript;
  interpreter = new Interpreter();
  
  interpreter.setClassLoader(Thread.currentThread().getContextClassLoader());
  try
  {
    interpreter.eval(new StringReader(bshScript));
  }
  catch(EvalError e)
  {
    throw new JRException(
      "Error evaluating report expressions BeanShell script."
      + "\nMessage : " + e.getMessage() 
      + "\nLine " + e.getErrorLineNumber() + " : " + extractLineContent(e) 
      );
  }
}

代码示例来源:origin: org.ofbiz/ofbcore-jira-share

} else {
  interpreter = new Interpreter();
  interpreter.setClassLoader(classLoader);

代码示例来源:origin: org.zkoss.zk/zk

public void init(Page owner, String zslang) {
  super.init(owner, zslang);
  _ip = new bsh.Interpreter();
  _ip.setClassLoader(Classes.getContextClassLoader(BSHInterpreter.class));
  _bshns = new GlobalNS(_ip.getClassManager(), "global");
  _ip.setNameSpace(_bshns);
}

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

try {
  Interpreter i = new Interpreter();
  i.setClassLoader(classLoader);
  i.set("addAdapter", this);
  i.set("picoContainer", pico);

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

try {
  Interpreter i = new Interpreter();
  i.setClassLoader(classLoader);
  i.set("addAdapter", this);
  i.set("picoContainer", pico);

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

try {
  classLoader = findClassLoader(json, workflowRunID);
  interpreter.setClassLoader(classLoader);
} catch (RuntimeException rex) {
  String message = "Unable to obtain the classloader for Beanshell service";

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

protected PicoContainer createContainerFromScript(PicoContainer parentContainer, Object assemblyScope) {
    Interpreter i = new Interpreter();
    try {
      i.set("parent", parentContainer);
      i.set("assemblyScope", assemblyScope);
      i.setClassLoader(this.getClassLoader());
      i.eval(getScriptReader(), i.getNameSpace(), "picocontainer.bsh");
      return (PicoContainer) i.get("pico");
    } catch (EvalError e) {
      throw new ScriptedPicoContainerMarkupException(e);
    } catch (IOException e) {
      throw new ScriptedPicoContainerMarkupException(e);
    }
  }
}

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

@Override
  protected PicoContainer createContainerFromScript(final PicoContainer parentContainer, final Object assemblyScope) {
    Interpreter i = new Interpreter();
    try {
      i.set("parent", parentContainer);
      i.set("assemblyScope", assemblyScope);
      i.setClassLoader(this.getClassLoader());
      i.eval(getScriptReader(), i.getNameSpace(), "picocontainer.bsh");
      return (PicoContainer) i.get("pico");
    } catch (EvalError e) {
      throw new ScriptedPicoContainerMarkupException(e);
    } catch (IOException e) {
      throw new ScriptedPicoContainerMarkupException(e);
    }
  }
}

相关文章