Module jamal.engine

Class JShellEngine

java.lang.Object
javax0.jamal.engine.JShellEngine
All Implemented Interfaces:
AutoCloseable, javax0.jamal.api.JShellEngine

public class JShellEngine
extends Object
implements javax0.jamal.api.JShellEngine
A JShell engine that helps the invocation of the JShell from the JDK.

The engine has two methods. One is define(String). The other one is evaluate(String).

The JShell interpreter is created only when the first call to evaluate(String) happens. This is to avoid unnecessary creation of the JShell engine, which is a costly operation. A Jamal file may include a set of definitions containing a lot of JShell and script macros. When the macro JShell is executed it calls define(String). It still may happen that the processed file does not use these macros. In that case creating a JShell engine (it runs in a separate process) would be a waste of resource and would significantly slow Jamal down.

For this reason the execution of the defines are postponed until the first invocation of evaluate(String).

  • Constructor Details

  • Method Details

    • evaluate

      public String evaluate​(String input) throws javax0.jamal.api.BadSyntax
      Evaluate the input string using the JShell interpreter.

      The method first checks if the JShell interpreter is already initialized. If it is not then it calls init(). This will create the JShell interpreter and execute all deferred defines.

      After that it resets the output byte buffer, evaluates the input and returns the bytes (as a String converted using UTF-8) emitted by the evaluation to the standard output.

      The output is usually the string that the snippet or snippets print to the System.out using System.out.print() or System.out.println() or some other way. If this string has zero length then the evaluation will return the value of the last evaluated snippet.

      If the JShell throws an exception or the evaluation status is some error then the method throws BadSyntax.

      Specified by:
      evaluate in interface javax0.jamal.api.JShellEngine
      Parameters:
      input - text to evaluate as JShell snippet
      Returns:
      the standard output of the snippet
      Throws:
      javax0.jamal.api.BadSyntax - if there was any error during the evaulation of the input or during the evaluation of the deferred definitions.
    • define

      public void define​(String input) throws javax0.jamal.api.BadSyntax
      Evaluate the input assuming that this code defines something for the JShell interpreter, like a method, class variable etc.

      If the JShell interpreter was not initialized yet then this definition will be stored in a deferred list and it will be evaluated only later when the first actual use of the JShell interpreter happens.

      The evaluation is accepted if there is no error or if there is some error that is recoverable. For example you can have a snippet that references some global variable that is going to be defined only later.

      Specified by:
      define in interface javax0.jamal.api.JShellEngine
      Parameters:
      input - the input that defines one single something, like a class, method, variable. It should not include multiple definitions.
      Throws:
      javax0.jamal.api.BadSyntax - when the evaluation results an exception or rejects the snippet
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable