【问题标题】:JSHELL JDK API : Reject snippet when exception is thrownJSHELL JDK API:抛出异常时拒绝片段
【发布时间】:2020-10-28 09:00:50
【问题描述】:

我正在为学生创建一个简单的 jshell 来技术 java,我想使用 JUNIT 的断言,但是当抛出异常时,sn-ps 不会被拒绝,我看到它可能需要有一个 custom execution engine 但它也是很多。

这是我的代码,它将在 IDE 上运行 REPL,但不会拒绝 throw new Exception("Exception message");

package simple.repl;

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

public class Main {

    public static void main(String[] args) throws IOException {

        try (JShell js = JShell.builder().in(System.in).out(System.out).err(System.out).build()) {

            for (final String str : System.getProperty("java.class.path").split(File.pathSeparator)) {
                js.addToClasspath(str);
            }

            do {
                //input System.out.println("hello");
                //int as = 1;
                //throw new Exception("Exception message");
                System.out.print("Enter some Java code: ");
                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                String input= reader.readLine();

                if (input == null) {
                    break;
                }
                List<SnippetEvent> events = js.eval(input);
                for (SnippetEvent e : events) {
                    StringBuilder sb = new StringBuilder();
                    if (e.causeSnippet() == null) {
                        //  We have a snippet creation event
                        switch (e.status()) {
                            case VALID:
                                sb.append("Successful ");
                                break;
                            case RECOVERABLE_DEFINED:
                                sb.append("With unresolved references ");
                                break;
                            case RECOVERABLE_NOT_DEFINED:
                                sb.append("Possibly reparable, failed  ");
                                break;
                            case REJECTED:
                                sb.append("Failed ");
                                break;
                        }
                        if (e.previousStatus() == Status.NONEXISTENT) {
                            sb.append("addition");
                        } else {
                            sb.append("modification");
                        }
                        sb.append(" of ");
                        sb.append(e.snippet().source());
                        System.out.println(sb);
                        if (e.value() != null) {
                            System.out.printf("Value is: %s\n", e.value());
                        }
                        System.out.flush();
                    }
                }
            } while (true);
        }
        System.out.println("\nGoodbye");
    }
}

【问题讨论】:

    标签: java exception junit assertion jshell


    【解决方案1】:

    更新:

    如果 java 代码语法正确,即使在运行时出现异常/错误,Snippet 也会被标记为 Status.VALID,以检查 sn-p 异常的使用 snippetEvent.exception() != null

    if (snippetEvent.exception() != null) {
    System.out.println("Exception : " + snippetEvent.exception().getMessage());
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-11
      • 2013-05-24
      • 1970-01-01
      • 2015-10-11
      • 2012-10-19
      • 2018-06-24
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多