< prev index next >
src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/Main.java
Print this page
@@ -23,27 +23,35 @@
* questions.
*/
package jdk.nashorn.tools.jjs;
+import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
+
+import java.awt.Desktop;
import java.awt.GraphicsEnvironment;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
+import java.net.URI;
+import java.util.concurrent.Callable;
import java.util.function.Consumer;
+import java.util.function.Function;
import jdk.internal.jline.console.completer.Completer;
import jdk.internal.jline.console.UserInterruptException;
import jdk.nashorn.api.scripting.NashornException;
import jdk.nashorn.internal.objects.Global;
+import jdk.nashorn.internal.objects.NativeJava;
import jdk.nashorn.internal.runtime.Context;
import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.Property;
import jdk.nashorn.internal.runtime.ScriptEnvironment;
+import jdk.nashorn.internal.runtime.ScriptFunction;
import jdk.nashorn.internal.runtime.ScriptRuntime;
import jdk.nashorn.tools.Shell;
/**
* Interactive command line Shell for Nashorn.
@@ -107,11 +115,32 @@
final Global oldGlobal = Context.getGlobal();
final boolean globalChanged = (oldGlobal != global);
final PropertiesHelper propsHelper = new PropertiesHelper(env._classpath);
final NashornCompleter completer = new NashornCompleter(context, global, this, propsHelper);
- try (final Console in = new Console(System.in, System.out, HIST_FILE, completer)) {
+ try (final Console in = new Console(System.in, System.out, HIST_FILE, completer,
+ str -> {
+ try {
+ final Object res = context.eval(global, str, global, "<shell>");
+ if (res != null && res != UNDEFINED) {
+ // Special case Java types: show the javadoc for the class.
+ if (NativeJava.isType(UNDEFINED, res)) {
+ openBrowserForJavadoc(NativeJava.typeName(UNDEFINED, res).toString());
+ }
+
+ if (res instanceof ScriptFunction) {
+ return ((ScriptFunction)res).getDocumentation();
+ }
+
+ // FIXME: better than toString for other cases?
+ return JSType.toString(res);
+ }
+ } catch (Exception ignored) {
+ }
+ return null;
+ })) {
+
if (globalChanged) {
Context.setGlobal(global);
}
global.addShellBuiltins();
@@ -162,11 +191,11 @@
continue;
}
try {
final Object res = context.eval(global, source, global, "<shell>");
- if (res != ScriptRuntime.UNDEFINED) {
+ if (res != UNDEFINED) {
err.println(toString(res, global));
}
} catch (final Exception exp) {
// Is this a ECMAScript SyntaxError at last column (of the single line)?
// If so, it is because parser expected more input but got EOF. Try to
@@ -216,16 +245,27 @@
private void evalImpl(final Context context, final Global global, final String source,
final PrintWriter err, final boolean doe) {
try {
final Object res = context.eval(global, source, global, "<shell>");
- if (res != ScriptRuntime.UNDEFINED) {
+ if (res != UNDEFINED) {
err.println(JSType.toString(res));
}
} catch (final Exception e) {
err.println(e);
if (doe) {
e.printStackTrace(err);
}
}
}
+
+ // FIXME: needs to be changed to use javase 9 docs later
+ private static String JAVADOC_BASE = "http://download.java.net/jdk9/docs/api/";
+
+ private static void openBrowserForJavadoc(String clsName) {
+ try {
+ final URI uri = new URI(JAVADOC_BASE + clsName.replace('.', '/') + ".html");
+ Desktop.getDesktop().browse(uri);
+ } catch (Exception ignored) {
+ }
+ }
}
< prev index next >