Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8085937

add autoimports sample script to easily explore Java classes in interactive mode

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b69
    • CPU:
      generic
    • OS:
      generic

      Backports

        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.

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  sundar Sundararajan Athijegannathan
                  Reporter:
                  sundar Sundararajan Athijegannathan
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  2 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved: