gpt4 book ai didi

java - 有没有办法删除 JShell 中的历史记录?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:58 26 4
gpt4 key购买 nike

在 JShell 中,是否有任何方法可以删除您执行的语句,就像它从未发生过一样?

例如,假设我们有以下代码。

 JShell shell = JShell.create();
shell.eval("int x = 5;");
shell.eval("x = 7;");

JShell 中是否有可以返回一步的东西?也就是说,类似于 goBackOneStep() 方法,在执行它之后,将 x 设置回 5。

 shell.goBackOneStep();
shell.eval("x") // x is evaluated to 5

我在 JShell API 中注意到有一个名为 drop(Snippet) 的方法,它似乎从历史记录中删除了 Snippet,但它没有删除 Snippet 的任何影响在状态。

最佳答案

jshell drop jshell drop 意味着将代码片段的状态更改为 DROPPED 并使它们处于非 Activity 状态(因此它们不再处于 Activity 状态并且您无法使用它们,直到您再次创建它们)

Drops a snippet, making it inactive. Provide either the name or the ID of an import, class, method, or variable. For more than one snippet, separate the names and IDs with a space. Use the /list command to see the IDs of code snippets.

DROPPED

The snippet is inactive because of an explicit call to the JShell.drop(Snippet).

在下面的代码中你有两个snippets 并且通过使用drop 你让它们不活跃

JShell shell = JShell.create();
shell.eval("int x = 5;");
shell.eval("x = 7;");

shell.snippets().forEach(s1->shell.drop(s1));

shell.snippets().forEach(s2->{
System.out.println(shell.status(s2)); //DROPPED
});

shell.snippets().forEach(System.out::println); //we print all active and inactive snippets

以同样的方式,您还可以在 jshell 控制台中查看所有 Activity 和删除的代码段

/list [选项]

Displays a list of snippets and their ID. If no option is entered, then all active snippets are displayed, but startup snippets aren’t.

/list -all

Displays all snippets, including startup snippets and snippets that failed, were overwritten, or were dropped. IDs that begin with s are startup snippets. IDs that begin with e are snippets that failed.

JShell 中是否有后退一步的功能?

在 jshell 中,每个输入都是一个片段,因此无法回滚片段。

The input should be exactly one complete snippet of source code, that is, one expression, statement, variable declaration, method declaration, class declaration, or import. To break arbitrary input into individual complete snippets, use SourceCodeAnalysis.analyzeCompletion(String).

关于java - 有没有办法删除 JShell 中的历史记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55996599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com