Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | JShell API & Architecture Adding Interactive Java Support to your Tool Robert Field JShell Architect Core Language and Tools Group Oracle Corporation November 7, 2015
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 3
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Agenda Architecture API Meta-demo Completion analysis Questions 1 2 3 4 5 4
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Agenda Architecture API Meta-demo Completion analysis Questions 1 2 3 4 5 5
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | What is JShell? • Tool providing a dynamic interaction with the Java™ programming language • Read-Evaluate-Print Loop (REPL) for the Java™ platform – Type in a snippet of Java code, see the results • Deeply integrated with JDK tool-set – Stays current and compatible • Also, an API for use within other applications 6
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | JShell Guiding Principles • Compatibily: current and future • Avoid chasing corner-cases • Build on the platform (JDK) • Easy and natural to use • One size does not fit all (API) 7
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | JShell Architecture 8
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Implementation: Snippet Processing • Parsing – Determine the Kind of Snippet and its validity – Uses minor subclassing of standard JavacParser which allows entry into grammar at the snippet level • Wrapping – Given knowledge of Kind, wrap as appropriate to create a valid Java program – Generate outer static class, possibly method, var field, temp variable setters, etc • Analyze – Determine type info and resolution errors (standard compiler API) – Possibly rewrapping one or more times 9
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Implementation: Snippet Processing (cont) • Compile to bytecode – Use standard compiler API – Use in-memory FileManager – “Corral” body with rewrap on resolution errors • Remotely Load / Reload – define() on URLClassLoader specialization – Java Debug Interface redefineClasses(…) – Yank into new wrapping class • Execute 10
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Agenda Architecture API Meta-demo Completion analysis Questions 1 2 3 4 5 11
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | JShell API 12
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | JShell class • Creation of JShell instances • Evaluation of snippets • Control of environment • Execution control • Snippet enumeration • Snippet mutable state query • Event monitoring/control 13
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | JShell class API – JShell instance creation / close Return Method JShell create() [static] JShell.Builder builder() [static] void close() 14
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | JShell class API – Evaluation & Discard Return Method Stream<SnippetEvent> eval(String input) Stream<SnippetEvent> drop(PersistentSnippet snippet) 15
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | JShell class API – Snippet Enumerations Return Method Stream<Snippet> snippets() Stream<VarSnippet> variables() Stream <MethodSnippet> methods() Stream <TypeDeclSnippet> types() 16
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | JShell class API – Snippet Queries (mutable state) Return Method Snippet.Status status(Snippet snippet) Stream<Diag> diagnostics(Snippet snippet) Stream<String> unresolvedDependencies( DeclarationSnippet snippet) String varValue(VarSnippet snippet) 17
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Snippet.Status enum • VALID • RECOVERABLE_DEFINED • RECOVERABLE_NOT_DEFINED • REJECTED • DROPPED • OVERWRITTEN • NONEXISTENT 18
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | JShell.Builder • Alternative to JShell.create() • Configure JShell instance – Set evaluation input, output, error – ID generation format – Temp variable generation format • Example: JShell myShell = JShell.builder() .out(myOutStream) .err(myErrStream) .build(); 19
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | JShell.Builder API Return Method JShell.Builder in(java.io.InputStream in) out(java.io.PrintStream out) err(java.io.PrintStream err) JShell.Builder idGenerator( BiFunction<Snippet,Integer,String> generator) JShell.Builder tempVariableNameGenerator( Supplier<String> generator) JShell build() 20
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | SnippetEvent • Stream of them returned by methods that change the state of Snippets – eval(…) – drop(…) • Represent a Snippet state transition (including from NONEXISTENT) • Changes to dependents are reported • Immutable 21
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | SnippetEvent API – Some queries Return Method Snippet snippet() Snippet.Status status() Snippet causeSnippet() String value() Exception exception() 22
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Snippet • Represents one source snippet of code • Specialized for the kind of Snippet • Immutable • State of the Snippet (mutable) via JShell query 23
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Snippet Hierarchy • Snippet • PersistentSnippet • DeclarationSnippet • VarSnippet • MethodSnippet • TypeDeclSnippet • ImportSnippet • StatementSnippet • ExpressionSnippet • ErroneousSnippet 24
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Snippet API Return Method String id() String source() Snippet.Kind kind() Snippet.SubKind subKind() 25
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | MethodSnippet API – Additional Queries Return Method String name() String signature() String parameterTypes() 26
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Agenda Architecture API Meta-demo Completion analysis Questions 1 2 3 4 5 27
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 28 Demo
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Agenda Architecture API Meta-demo Completion analysis Questions 1 2 3 4 5 29
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | SourceCodeAnalysis • Find the bounds of a single source snippet • Determine if the input in complete • Suggest completions • Provide documentation • Accessed via JShell.sourceCodeAnalysis() • Suggestions use existing state and full compiler type analysis 30
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | SourceCodeAnalysis API Return Method SourceCodeAnalysis. CompletionInfo analyzeCompletion(String input) Stream< SourceCodeAnalysis. Suggestion> completionSuggestions( String input, int cursor, int[] anchor) String documentation( String input, int cursor) 31
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | JDK 9 / JShell Information • Current schedule: GA September 22, 2016 • Early access binaries + docs, updated weekly: https://jdk9.java.net/ • Source code: http://hg.openjdk.java.net/jdk9/dev/ • -- Kulla (JShell) Project: http://openjdk.java.net/projects/kulla/ • JEP 222: jshell: The Java Shell: http://openjdk.java.net/jeps/222 32
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | The Team • Engineering – Robert Field – Jan Lahoda • OpenJDK Commiters – Shinya Yoshida – You? • Testing – Andrei Eremeev – Brian Goetz – Maurizio Cimadamore – Joe Darcy – Paul Sandoz – Jonathan Gibbons – Michel Trudeau – Sundararajan Athijegannathan – Rémi Forax – Arun Gupta – Mani Sarkar – Daniel Daugherty 33 • Advisors / Cheerleaders / Reviewers
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Agenda Architecture API Meta-demo Completion analysis Questions 1 2 3 4 5 34
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 35 Q & A
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 36
Interactive Java Support to your tool -- The JShell API and Architecture

Interactive Java Support to your tool -- The JShell API and Architecture

  • 2.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JShell API & Architecture Adding Interactive Java Support to your Tool Robert Field JShell Architect Core Language and Tools Group Oracle Corporation November 7, 2015
  • 3.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 3
  • 4.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Agenda Architecture API Meta-demo Completion analysis Questions 1 2 3 4 5 4
  • 5.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Agenda Architecture API Meta-demo Completion analysis Questions 1 2 3 4 5 5
  • 6.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | What is JShell? • Tool providing a dynamic interaction with the Java™ programming language • Read-Evaluate-Print Loop (REPL) for the Java™ platform – Type in a snippet of Java code, see the results • Deeply integrated with JDK tool-set – Stays current and compatible • Also, an API for use within other applications 6
  • 7.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JShell Guiding Principles • Compatibily: current and future • Avoid chasing corner-cases • Build on the platform (JDK) • Easy and natural to use • One size does not fit all (API) 7
  • 8.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JShell Architecture 8
  • 9.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Implementation: Snippet Processing • Parsing – Determine the Kind of Snippet and its validity – Uses minor subclassing of standard JavacParser which allows entry into grammar at the snippet level • Wrapping – Given knowledge of Kind, wrap as appropriate to create a valid Java program – Generate outer static class, possibly method, var field, temp variable setters, etc • Analyze – Determine type info and resolution errors (standard compiler API) – Possibly rewrapping one or more times 9
  • 10.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Implementation: Snippet Processing (cont) • Compile to bytecode – Use standard compiler API – Use in-memory FileManager – “Corral” body with rewrap on resolution errors • Remotely Load / Reload – define() on URLClassLoader specialization – Java Debug Interface redefineClasses(…) – Yank into new wrapping class • Execute 10
  • 11.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Agenda Architecture API Meta-demo Completion analysis Questions 1 2 3 4 5 11
  • 12.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JShell API 12
  • 13.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JShell class • Creation of JShell instances • Evaluation of snippets • Control of environment • Execution control • Snippet enumeration • Snippet mutable state query • Event monitoring/control 13
  • 14.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JShell class API – JShell instance creation / close Return Method JShell create() [static] JShell.Builder builder() [static] void close() 14
  • 15.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JShell class API – Evaluation & Discard Return Method Stream<SnippetEvent> eval(String input) Stream<SnippetEvent> drop(PersistentSnippet snippet) 15
  • 16.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JShell class API – Snippet Enumerations Return Method Stream<Snippet> snippets() Stream<VarSnippet> variables() Stream <MethodSnippet> methods() Stream <TypeDeclSnippet> types() 16
  • 17.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JShell class API – Snippet Queries (mutable state) Return Method Snippet.Status status(Snippet snippet) Stream<Diag> diagnostics(Snippet snippet) Stream<String> unresolvedDependencies( DeclarationSnippet snippet) String varValue(VarSnippet snippet) 17
  • 18.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Snippet.Status enum • VALID • RECOVERABLE_DEFINED • RECOVERABLE_NOT_DEFINED • REJECTED • DROPPED • OVERWRITTEN • NONEXISTENT 18
  • 19.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JShell.Builder • Alternative to JShell.create() • Configure JShell instance – Set evaluation input, output, error – ID generation format – Temp variable generation format • Example: JShell myShell = JShell.builder() .out(myOutStream) .err(myErrStream) .build(); 19
  • 20.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JShell.Builder API Return Method JShell.Builder in(java.io.InputStream in) out(java.io.PrintStream out) err(java.io.PrintStream err) JShell.Builder idGenerator( BiFunction<Snippet,Integer,String> generator) JShell.Builder tempVariableNameGenerator( Supplier<String> generator) JShell build() 20
  • 21.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | SnippetEvent • Stream of them returned by methods that change the state of Snippets – eval(…) – drop(…) • Represent a Snippet state transition (including from NONEXISTENT) • Changes to dependents are reported • Immutable 21
  • 22.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | SnippetEvent API – Some queries Return Method Snippet snippet() Snippet.Status status() Snippet causeSnippet() String value() Exception exception() 22
  • 23.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Snippet • Represents one source snippet of code • Specialized for the kind of Snippet • Immutable • State of the Snippet (mutable) via JShell query 23
  • 24.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Snippet Hierarchy • Snippet • PersistentSnippet • DeclarationSnippet • VarSnippet • MethodSnippet • TypeDeclSnippet • ImportSnippet • StatementSnippet • ExpressionSnippet • ErroneousSnippet 24
  • 25.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Snippet API Return Method String id() String source() Snippet.Kind kind() Snippet.SubKind subKind() 25
  • 26.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | MethodSnippet API – Additional Queries Return Method String name() String signature() String parameterTypes() 26
  • 27.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Agenda Architecture API Meta-demo Completion analysis Questions 1 2 3 4 5 27
  • 28.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | 28 Demo
  • 29.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Agenda Architecture API Meta-demo Completion analysis Questions 1 2 3 4 5 29
  • 30.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | SourceCodeAnalysis • Find the bounds of a single source snippet • Determine if the input in complete • Suggest completions • Provide documentation • Accessed via JShell.sourceCodeAnalysis() • Suggestions use existing state and full compiler type analysis 30
  • 31.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | SourceCodeAnalysis API Return Method SourceCodeAnalysis. CompletionInfo analyzeCompletion(String input) Stream< SourceCodeAnalysis. Suggestion> completionSuggestions( String input, int cursor, int[] anchor) String documentation( String input, int cursor) 31
  • 32.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | JDK 9 / JShell Information • Current schedule: GA September 22, 2016 • Early access binaries + docs, updated weekly: https://jdk9.java.net/ • Source code: http://hg.openjdk.java.net/jdk9/dev/ • -- Kulla (JShell) Project: http://openjdk.java.net/projects/kulla/ • JEP 222: jshell: The Java Shell: http://openjdk.java.net/jeps/222 32
  • 33.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | The Team • Engineering – Robert Field – Jan Lahoda • OpenJDK Commiters – Shinya Yoshida – You? • Testing – Andrei Eremeev – Brian Goetz – Maurizio Cimadamore – Joe Darcy – Paul Sandoz – Jonathan Gibbons – Michel Trudeau – Sundararajan Athijegannathan – Rémi Forax – Arun Gupta – Mani Sarkar – Daniel Daugherty 33 • Advisors / Cheerleaders / Reviewers
  • 34.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Agenda Architecture API Meta-demo Completion analysis Questions 1 2 3 4 5 34
  • 35.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | 35 Q & A
  • 36.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 36

Editor's Notes

  • #4 .
  • #5 Describe the architecture of JShell Run through the API Demo the JShell API from inside the JShell tool Provide tool examples One more piece of the API, completion analysis Q & A – feel free to ask questions as we go
  • #6 Describe the architecture of JShell Run through the API Demo the JShell API from inside the JShell tool Provide tool examples One more piece of the API, completion analysis Q & A – feel free to ask questions as we go
  • #7 Dynamic interaction REPL – as seem, dynamic languages Snippet: expression, statement, or declaration Platform: in / built-on Showing command tool using technology. Encourage other tools built on API.
  • #8 I've seen language tools that “cleverly” implemented functionality their own way, only to to be continually slightly out of sync, playing catch-up, and eventually falling too far behind. JShell built on and in the platform. API makes functionality available to any tool.
  • #9 Compilation: built on Compiler API Execution: standard JVM controlled by Java Platform Debug Architecture All core functionality provided by JShell API. The jshell tool just one possible client. The jshell tool uses the popular jline2 API for command line interaction.
  • #12 Describe the architecture of JShell Run through the API Demo the JShell API from inside the JShell tool Provide tool examples One more piece of the API, completion analysis Q & A – feel free to ask questions as we go
  • #13 An instance of JShell is the starting point. Doing an eval() of an input string returns SnippetEvents. Which hold several things, like value. But also created Snippets. Queries can be made for information about the Snippet. SourceCodeAnalysis provides information like completion. [API Demo?]
  • #28 Describe the architecture of JShell Run through the API Demo the JShell API from inside the JShell tool Provide tool examples One more piece of the API, completion analysis Q & A – feel free to ask questions as we go
  • #30 Describe the architecture of JShell Run through the API Demo the JShell API from inside the JShell tool Provide tool examples One more piece of the API, completion analysis Q & A – feel free to ask questions as we go
  • #33 JShell now in JDK 9
  • #34 Jan and I are the Oracle engineering team. Shinya Yoshida has contributed significantly as an OpenJDK Commiter, as could you, … The project was Brian's inspiration. Many people have advised, reviewed, and cheered us on.
  • #35 Describe the architecture of JShell Run through the API Demo the JShell API from inside the JShell tool Provide tool examples One more piece of the API, completion analysis Q & A – feel free to ask questions as we go
  • #37 This is a Safe Harbor Front slide, one of two Safe Harbor Statement slides included in this template. One of the Safe Harbor slides must be used if your presentation covers material affected by Oracle’s Revenue Recognition Policy To learn more about this policy, e-mail: Revrec-americasiebc_us@oracle.com For internal communication, Safe Harbor Statements are not required. However, there is an applicable disclaimer (Exhibit E) that should be used, found in the Oracle Revenue Recognition Policy for Future Product Communications. Copy and paste this link into a web browser, to find out more information. http://my.oracle.com/site/fin/gfo/GlobalProcesses/cnt452504.pdf For all external communications such as press release, roadmaps, PowerPoint presentations, Safe Harbor Statements are required. You can refer to the link mentioned above to find out additional information/disclaimers required depending on your audience.