What are the different status states of REPL in Java 9?

JavaObject Oriented ProgrammingProgramming

REPL stands for Read-Evaluate-Print-Loop. It holds some states, and each statement in JShell has a state. This state denies the execution status of snippets and variables. It can be determined by the results of the eval() method of JShell instance, which evaluates the code.

There are seven different status states listed below.

  • DROPPED: The snippet is inactive.
  • NONEXISTENT: The snippet is inactive because it does not yet exist.
  • OVERWRITTEN: The snippet is inactive because it has been replaced by a new snippet.
  • RECOVERABLE_DEFINED: The snippet is a declaration snippet with potentially recoverable unresolved references or other issues in its body.
  • RECOVERABLE_NOT_DEFINED: The snippet is a declaration snippet with potentially recoverable unresolved references or other issues.
  • REJECTED: The snippet is inactive because the compilation failed upon initial evaluation and it is not capable of becoming valid with further changes to the JShell state.
  • VALID: The snippet is a valid snippet.

Example

import java.util.List;
import jdk.jshell.*;
import jdk.jshell.Snippet.Status;

public class JShellTest {
   public static void main(String args[]) {
      JShell shell = JShell.create();
      List<SnippetEvent> events = shell.eval("int a, b, sum; " + "a = 12; b = 11; sum = a + b; " +
                                             "System.out.println(sum);" );
      for(SnippetEvent event : events) {
         Snippet snippet = event.snippet();
         Snippet.Status snippetstatus = shell.status(snippet);
         if(snippetstatus == Status.VALID) {
            System.out.println("Successfully executed");
         }
      }
   }
}

Output

Successfully executed
Successfully executed
Successfully executed  
raja
Published on 02-Apr-2020 12:50:55
Advertisements