88 import static java.nio.file.StandardOpenOption.CREATE;
89 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
90 import static java.nio.file.StandardOpenOption.WRITE;
91 import java.util.MissingResourceException;
92 import java.util.Optional;
93 import java.util.ResourceBundle;
94 import java.util.Spliterators;
95 import java.util.function.Function;
96 import java.util.function.Supplier;
97 import jdk.internal.jshell.tool.Feedback.FormatAction;
98 import jdk.internal.jshell.tool.Feedback.FormatCase;
99 import jdk.internal.jshell.tool.Feedback.FormatErrors;
100 import jdk.internal.jshell.tool.Feedback.FormatResolve;
101 import jdk.internal.jshell.tool.Feedback.FormatUnresolved;
102 import jdk.internal.jshell.tool.Feedback.FormatWhen;
103 import static java.util.stream.Collectors.toList;
104 import static jdk.jshell.Snippet.SubKind.VAR_VALUE_SUBKIND;
105 import static java.util.stream.Collectors.toMap;
106 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_COMPA;
107 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_DEP;
108 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_EVNT;
109 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_FMGR;
110 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN;
111
112 /**
113 * Command line REPL tool for Java using the JShell API.
114 * @author Robert Field
115 */
116 public class JShellTool implements MessageHandler {
117
118 private static final String LINE_SEP = System.getProperty("line.separator");
119 private static final Pattern LINEBREAK = Pattern.compile("\\R");
120 private static final String RECORD_SEPARATOR = "\u241E";
121 private static final String RB_NAME_PREFIX = "jdk.internal.jshell.tool.resources";
122 private static final String VERSION_RB_NAME = RB_NAME_PREFIX + ".version";
123 private static final String L10N_RB_NAME = RB_NAME_PREFIX + ".l10n";
124
125 final InputStream cmdin;
126 final PrintStream cmdout;
127 final PrintStream cmderr;
1358 } else if (at.hasOption("-none")) {
1359 startup = "";
1360 }
1361 return true;
1362 }
1363
1364 boolean cmdClasspath(String arg) {
1365 if (arg.isEmpty()) {
1366 errormsg("jshell.err.classpath.arg");
1367 return false;
1368 } else {
1369 state.addToClasspath(toPathResolvingUserHome(arg).toString());
1370 fluffmsg("jshell.msg.classpath", arg);
1371 return true;
1372 }
1373 }
1374
1375 boolean cmdDebug(String arg) {
1376 if (arg.isEmpty()) {
1377 debug = !debug;
1378 InternalDebugControl.setDebugFlags(state, debug ? DBG_GEN : 0);
1379 fluff("Debugging %s", debug ? "on" : "off");
1380 } else {
1381 int flags = 0;
1382 for (char ch : arg.toCharArray()) {
1383 switch (ch) {
1384 case '0':
1385 flags = 0;
1386 debug = false;
1387 fluff("Debugging off");
1388 break;
1389 case 'r':
1390 debug = true;
1391 fluff("REPL tool debugging on");
1392 break;
1393 case 'g':
1394 flags |= DBG_GEN;
1395 fluff("General debugging on");
1396 break;
1397 case 'f':
1398 flags |= DBG_FMGR;
1399 fluff("File manager debugging on");
1400 break;
1401 case 'c':
1402 flags |= DBG_COMPA;
1403 fluff("Completion analysis debugging on");
1404 break;
1405 case 'd':
1406 flags |= DBG_DEP;
1407 fluff("Dependency debugging on");
1408 break;
1409 case 'e':
1410 flags |= DBG_EVNT;
1411 fluff("Event debugging on");
1412 break;
1413 default:
1414 hard("Unknown debugging option: %c", ch);
1415 fluff("Use: 0 r g f c d");
1416 return false;
1417 }
1418 }
1419 InternalDebugControl.setDebugFlags(state, flags);
1420 }
1421 return true;
1422 }
1423
1424 private boolean cmdExit() {
1425 regenerateOnDeath = false;
1426 live = false;
1427 if (!replayableHistory.isEmpty()) {
1428 // Prevent history overflow by calculating what will fit, starting
1429 // with most recent
1430 int sepLen = RECORD_SEPARATOR.length();
1431 int length = 0;
1432 int first = replayableHistory.size();
1433 while(length < Preferences.MAX_VALUE_LENGTH && --first >= 0) {
1434 length += replayableHistory.get(first).length() + sepLen;
1435 }
|
88 import static java.nio.file.StandardOpenOption.CREATE;
89 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
90 import static java.nio.file.StandardOpenOption.WRITE;
91 import java.util.MissingResourceException;
92 import java.util.Optional;
93 import java.util.ResourceBundle;
94 import java.util.Spliterators;
95 import java.util.function.Function;
96 import java.util.function.Supplier;
97 import jdk.internal.jshell.tool.Feedback.FormatAction;
98 import jdk.internal.jshell.tool.Feedback.FormatCase;
99 import jdk.internal.jshell.tool.Feedback.FormatErrors;
100 import jdk.internal.jshell.tool.Feedback.FormatResolve;
101 import jdk.internal.jshell.tool.Feedback.FormatUnresolved;
102 import jdk.internal.jshell.tool.Feedback.FormatWhen;
103 import static java.util.stream.Collectors.toList;
104 import static jdk.jshell.Snippet.SubKind.VAR_VALUE_SUBKIND;
105 import static java.util.stream.Collectors.toMap;
106 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_COMPA;
107 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_DEP;
108 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_EC;
109 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_EVNT;
110 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_FMGR;
111 import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN;
112
113 /**
114 * Command line REPL tool for Java using the JShell API.
115 * @author Robert Field
116 */
117 public class JShellTool implements MessageHandler {
118
119 private static final String LINE_SEP = System.getProperty("line.separator");
120 private static final Pattern LINEBREAK = Pattern.compile("\\R");
121 private static final String RECORD_SEPARATOR = "\u241E";
122 private static final String RB_NAME_PREFIX = "jdk.internal.jshell.tool.resources";
123 private static final String VERSION_RB_NAME = RB_NAME_PREFIX + ".version";
124 private static final String L10N_RB_NAME = RB_NAME_PREFIX + ".l10n";
125
126 final InputStream cmdin;
127 final PrintStream cmdout;
128 final PrintStream cmderr;
1359 } else if (at.hasOption("-none")) {
1360 startup = "";
1361 }
1362 return true;
1363 }
1364
1365 boolean cmdClasspath(String arg) {
1366 if (arg.isEmpty()) {
1367 errormsg("jshell.err.classpath.arg");
1368 return false;
1369 } else {
1370 state.addToClasspath(toPathResolvingUserHome(arg).toString());
1371 fluffmsg("jshell.msg.classpath", arg);
1372 return true;
1373 }
1374 }
1375
1376 boolean cmdDebug(String arg) {
1377 if (arg.isEmpty()) {
1378 debug = !debug;
1379 InternalDebugControl.setDebugFlags(state, debug ? DBG_GEN | DBG_EC : 0);
1380 fluff("Debugging %s", debug ? "on" : "off");
1381 } else {
1382 int flags = 0;
1383 for (char ch : arg.toCharArray()) {
1384 switch (ch) {
1385 case '0':
1386 flags = 0;
1387 debug = false;
1388 fluff("Debugging off");
1389 break;
1390 case 'r':
1391 debug = true;
1392 fluff("REPL tool debugging on");
1393 break;
1394 case 'g':
1395 flags |= DBG_GEN;
1396 fluff("General debugging on");
1397 break;
1398 case 'x':
1399 flags |= DBG_EC;
1400 fluff("Execution control debugging on");
1401 break;
1402 case 'f':
1403 flags |= DBG_FMGR;
1404 fluff("File manager debugging on");
1405 break;
1406 case 'c':
1407 flags |= DBG_COMPA;
1408 fluff("Completion analysis debugging on");
1409 break;
1410 case 'd':
1411 flags |= DBG_DEP;
1412 fluff("Dependency debugging on");
1413 break;
1414 case 'e':
1415 flags |= DBG_EVNT;
1416 fluff("Event debugging on");
1417 break;
1418 default:
1419 hard("Unknown debugging option: %c", ch);
1420 fluff("Use: 0 r g x f c d");
1421 return false;
1422 }
1423 }
1424 InternalDebugControl.setDebugFlags(state, flags);
1425 }
1426 return true;
1427 }
1428
1429 private boolean cmdExit() {
1430 regenerateOnDeath = false;
1431 live = false;
1432 if (!replayableHistory.isEmpty()) {
1433 // Prevent history overflow by calculating what will fit, starting
1434 // with most recent
1435 int sepLen = RECORD_SEPARATOR.length();
1436 int length = 0;
1437 int first = replayableHistory.size();
1438 while(length < Preferences.MAX_VALUE_LENGTH && --first >= 0) {
1439 length += replayableHistory.get(first).length() + sepLen;
1440 }
|