JDK-8085937 : add autoimports sample script to easily explore Java classes in interactive mode
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2015-06-08
  • Updated: 2015-09-29
  • Resolved: 2015-06-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u60Fixed 9 b69Fixed
Description
When we use nashorn 'jjs' shell interactively, typically we'd like to explore Java classes of the underlying JDK. It is tedious to specify fully qualified package names for imports or use fully qualified class names with Java.type calls. It'd be nice if we have a sample script that provides "auto" importing of Java classes. A sample script may use __noSuchProperty__ hook and look for full names of Java classes given only a simple name. In most cases unique match is found in JDK classes. If so, auto import can provide matching class. If there is more than one match (say java.awt.List, java.util.List for eg.), the script can interactively provide option to choose the class.

Example session:

$ jjs -scripting

jjs> load("autoimports.js")

jjs> load('autoimports.js')
jjs> LocalDateTime.now()
2015-06-08T08:25:44.304262
jjs> System.out.println("hello")
hello
jjs> m = new HashMap()
{}


There could be an "autoimports" function that prints out script to be used to avoid autoimports. i.e, script that lists all autoimports explicitly.

Example for the above session:

jjs> autoimports()
var LocalDateTime = Java.type('java.time.LocalDateTime');
var System = Java.type('java.lang.System');
var HashMap = Java.type('java.util.HashMap');


That function is useful to avoid autoimports in a script that will be used often (as autoimports script could involve startup time impact to read & initialize all java class->package mapping). The model is to experiment with Java classes in an interactive session and then use autoimports() function to get script needed to use those classes in your script to avoid autoimports.