Introduction to JShell: the Java REPL Tool #jjug_ccc #ccc_ab4
-
Upload
bitterfox -
Category
Technology
-
view
6.237 -
download
0
Transcript of Introduction to JShell: the Java REPL Tool #jjug_ccc #ccc_ab4
#ccc_ab4 https://sli.do/
Introduction to JShell:the Java REPL ToolJJUG CCC 2016 Spring@bitter_fox#ccc_ab4
#ccc_ab4 https://sli.do/
@bitter_fox(吉田真也)
●OpenJDKコミッタ
–Lambda, Kulla(JShell)
●日本OSS奨励賞受賞
–JavaOne参加で授賞式は参加できず><
●立命館大学 大学院 1回
–18卒
#ccc_ab4 https://sli.do/
@bitter_fox(吉田真也)
●OpenJDKコミッタ
–Lambda, Kulla(JShell)
●日本OSS奨励賞受賞
–JavaOne参加で授賞式は参加できず><
●立命館大学 大学院 1回
–18卒
#ccc_ab4 https://sli.do/
#ccc_ab4
Sli.do(質問)http://sli.do/#ccc_ab4
#ccc_ab4 https://sli.do/
アジェンダ
●JShell●Inside JShell & JShellAPI●Contributing to JShell●Beyond JShell
#ccc_ab4 https://sli.do/
ゴミプロジェクト
#ccc_ab4 https://sli.do/
ゴミプロジェクト
API,構文挙動確認
プロトタイプ開発
プレゼン教育
#ccc_ab4 https://sli.do/
ゴミプロジェクト
ゴチャゴチャ! Class,main邪魔
コンパイル実行面倒
#ccc_ab4 https://sli.do/
#ccc_ab4 https://sli.do/
新バージョンのJava
手元のライブラリ
Maven コード補完
#ccc_ab4 https://sli.do/
JShell
●Read-Eval-Print Loop(REPL)ツール
–Javaコードを実行できる
–SampleHogeHogeからの解放
●JDK9 EA is available–https://jdk9.java.net/download/
–Win, Macでは,環境が壊れる可能性,注意
–2017/03/23 General Availability
#ccc_ab4 https://sli.do/
JShellで実行できるJavaコードImport宣言
Class/Interface/Enum/Annotation宣言
フィールド宣言
メソッド宣言
ブロック
文式
#ccc_ab4 https://sli.do/
Import宣言
jshell> import java.time.*;
jshell> import java.nio.LocalDate
●入力の末尾の;は省略可
#ccc_ab4 https://sli.do/
Class/Enum/Interface/Anno宣言
jshell> class Student {
...> int age;
...> String name;
...> }
| created class Student
#ccc_ab4 https://sli.do/
フィールド宣言(変数宣言)
jshell> int MAX = 300
MAX ==> 300
#ccc_ab4 https://sli.do/
式
jshell> MAX / 2
$1 ==> 150
●式の結果は一時変数$nに
#ccc_ab4 https://sli.do/
メソッド宣言
jshell> int f(int n) {return n*2;}
| created method f(int)
jshell> f(100)
$2 ==> 200
#ccc_ab4 https://sli.do/
宣言の更新
メソッドやクラスの定義は後から変えられる
jshell> int f(int n) {return n*2;}
| created method f(int)
jshell> f(100)
$2 ==> 200
jshell> int f(int n) {return n*n;}
| modified method f(int)
jshell> f(100)
f(100)
$4 ==> 10000
#ccc_ab4 https://sli.do/
前方参照
●定義する前に参照可能
class Student {
String name;
Id id;
}
class ID {String id;}
#ccc_ab4 https://sli.do/
JShellの使いドコロ
●新しい言語機能の確認–ラムダ式,Java9の新言語機能
●新ライブラリ,APIの検証,動作確認
–StreamAPI, Date and Time API, etc
●教育–REPLを使うことで即時に結果が分かる
●高機能電卓
#ccc_ab4 https://sli.do/
Javaのラムダ式(デモ)
●(args) -> {stat}●関数型インターフェースがターゲット
–抽象メソッドが1つしかないインターフェース–defaultメソッドは関係ない
interface Func {
void f();
default Func andThen(Func f) {…}
}
Func f = () -> {}
#ccc_ab4 https://sli.do/
Java8Puzzler(デモ)
String s = Stream.of("Hello", "Fizz", "Buzz", "JavaSE8")
.map(String::length)
.map(Integer::toString)
.collect(Collectors.joining(", "));
System.out.println(s);(1): 5, 4, 4, 7, (2): 5, 4, 4, 7(3): [5, 4, 4, 7](4): コンパイルエラー(5): 実行時エラー
#ccc_ab4 https://sli.do/
Java9LangFeature(デモ)
●識別子_はコンパイルエラー
●外部の変数をtry-w-resの対象に
AutoCloseable ac = …;
try (ac) {…}●匿名クラスでダイアモンド演算子
list = new ArrayList<>() {{add(...);}}●@SafeVarargs on private methods●private interface method
#ccc_ab4 https://sli.do/
JShellのコマンド
●/で始まる
●状態の表示●設定の変更●履歴へのアクセス
●一意に定まれば,先頭数文字でOK–/help → \he
#ccc_ab4 https://sli.do/
JShellのコマンド:/helpjshell> /help
| /list [all|start|<name or id>] -- list the source you have typed
| /edit <name or id> -- edit a source entry referenced by name or id
| /drop <name or id> -- delete a source entry referenced by name or id
| /save [all|history|start] <file> -- Save snippet source to a file.
| /open <file> -- open a file as source input
| /vars -- list the declared variables and their values
| /methods -- list the declared methods and their signatures
| /classes -- list the declared classes
| /imports -- list the imported items
| /exit -- exit jshell
| /reset -- reset jshell
| /reload [restore] [quiet] -- reset and replay relevant history -- current or previous (restore)
| /classpath <path> -- add a path to the classpath
| /history -- history of what you have typed
| /help [<command>|<subject>] -- get information about jshell
| /set editor|start|feedback|newmode|prompt|format ... -- set jshell configuration information
| /? [<command>|<subject>] -- get information about jshell
| /! -- re-run last snippet
| /<id> -- re-run snippet by id
| /-<n> -- re-run n-th previous snippet
|
| For more information type '/help' followed by the name of command or a subject.
| For example '/help /list' or '/help intro'. Subjects:
| intro -- an introduction to the jshell tool
| shortcuts -- a description of shortcuts
#ccc_ab4 https://sli.do/
JShellのコマンド:/vars, /methods, /classes, /imports, /list
●シグネチャや値,入力したソースコードなどを表示
jshell> /list all s1 : import java.util.*;
s2 : import java.io.*;
s3 : import java.math.*;
s4 : import java.net.*;
s5 : import java.util.concurrent.*;
s6 : import java.util.prefs.*;
s7 : import java.util.regex.*;
s8 : void printf(String format, Object... args) {
System.out.printf(format, args); }
1 : int n = 100;
2 : System.out.println(n)
e1 : this is error
#ccc_ab4 https://sli.do/
JShellのコマンド:/vars, /methods, /classes, /imports, /list
●シグネチャや値,入力したソースコードなどを表示
jshell> /list all s1 : import java.util.*;
s2 : import java.io.*;
s3 : import java.math.*;
s4 : import java.net.*;
s5 : import java.util.concurrent.*;
s6 : import java.util.prefs.*;
s7 : import java.util.regex.*;
s8 : void printf(String format, Object... args) {
System.out.printf(format, args); }
1 : int n = 100;
2 : System.out.println(n)
e1 : this is error
Startup
Error
#ccc_ab4 https://sli.do/
Startup
●最初に実行されるプログラム–/save start [file]
●自分なりに改造すると捗る–オススメ
import j.u.stream.*
import j.u.function.*
import j.nio.file.*
import static j.u.Arrays.*
import static j.l.Math.*
#ccc_ab4 https://sli.do/
JShellのコマンド:/set
●設定変更
/seteditor
start
feedback
newmode
prompt
format
/set start ./startup.jsh
#ccc_ab4 https://sli.do/
JShellのコマンド:/set
●設定変更
/seteditor
start
feedback
newmode
prompt
format
/set start ./startup.jsh
Feedback
#ccc_ab4 https://sli.do/
Feedback
●出力のモード–独自に変更可能–プロンプトとフォーマット–かなり難しい,変更すればするほど手に馴染む
/set newmode mymode–新しいモードを作成
/set prompt mymode “$” “...”
/set format mymode …–プロンプトとフォーマットの設定
/set feedback mymode–出力モードの変更
#ccc_ab4 https://sli.do/
JShellのコマンド:再実行
●/!–直前のプログラムを再実行
●/<id>–idに対応するプログラムを再実行
●/-<n>–n個前のプログラムを再実行
#ccc_ab4 https://sli.do/
その他のJShellコマンド
/edit <name or id>–コードの編集
/drop <name or id>–コード,定義の削除
/open <file>–ファイルからコードの実行
/reset–jshellを起動時に戻す
/reload [restore] [quiet]–リセットして再実行
/classpath <path>–クラスパスを追加
/history–入力履歴を表示(コマンド含む)
#ccc_ab4 https://sli.do/
補完機能
●コード補完–TAB
●import補完
–Alt-Enter i●変数定義の補完
–Alt-Enter v
※Alt-EnterはAlt-F1かC-[ C-m
#ccc_ab4 https://sli.do/
ドキュメンテーション機能
●メソッドのシグネチャの表示–Shift-Tab
jshell> IntStream.range([Shift-Tab]
java.util.stream.IntStream.range(int arg0, int arg1)
#ccc_ab4 https://sli.do/
デモ
●IntStream|AEi
–(import IntStream)●IntStream.ra|T
–IntStream.range●IntStream.range(|ST
–range(int, int)●IntStream.range(0, 10).summaryStatistics()|AEv
–IntSummaryStatistics | = IntStream.range(0, 10).summaryStatistics()
|はカーソル,AEはAltEnter,TはTab,SはShift
#ccc_ab4 https://sli.do/
jshell> Inside JShell >> JShell API
#ccc_ab4 https://sli.do/
JShellの仕組み
JShell Tooljline
int a = 0
JShell API
Compiler(javac)
RemoteAgent
JVM
JDI
int a = 0
Is Complete?CompletionDocumentation
Eval & Resulta ==> 0
#ccc_ab4 https://sli.do/
snippet()
JShell API
JShell
SourceCodeAnalysis
SnippetEvent
XxxSnippet
eval(), d rop()
sourceCodeAnalysis()
state
#analyzeCompletion()#analyzeType()#completeSuggesions()#documentation()#listQualifiedNames()
#ccc_ab4 https://sli.do/
デモ
JShellAPIをJShellから叩く
JShell js = JShell.create()
js.eval(“int n = 0;”)
#ccc_ab4 https://sli.do/
Inside eval()int a = 0 Snippet.Kindを決定
Snippetを作成
ラップしたJavaコード
クラスファイル
依存性を検索再コンパイル
RemoteAgentで実行
クラスをロード
実行
SnippetEventを作成
RemoteAgent
a ==> 0
#ccc_ab4 https://sli.do/
ヒミツのコマンド:/debug(デモ)
jshell> /debug
| Debugging on
jshell> int n=0
Compiling: int n = 0;
Kind: VARIABLE -- int n=0
compileAndLoad [Unit(n)]
++setCompilationInfo() Snippet:VariableKey(n)#1-int n=0;
package REPL;
class $JShell$1 {
public static int n;
public static Object do_it$() throws Throwable {
int n_ = 0;
return n = n_;
}
}
-- diags: []
setStatus() Snippet:VariableKey(n)#25-int n = 0; - status: VALID
compileAndLoad ins = [Unit(n)] -- legit = [Unit(n)]
Compiler generating class REPL.$JShell$25
compileAndLoad [Unit(n)] -- deps: [] success: true
recordCompilation: Snippet:VariableKey(n)#25-int n = 0; -- status VALID, unresolved []
n ==> 0
#ccc_ab4 https://sli.do/
ヒミツのコマンド:/debug(デモ)
jshell> /debug
| Debugging on
jshell> int n=0
Compiling: int n = 0;
Kind: VARIABLE -- int n=0
compileAndLoad [Unit(n)]
++setCompilationInfo() Snippet:VariableKey(n)#1-int n=0;
package REPL;
class $JShell$1 {
public static int n;
public static Object do_it$() throws Throwable {
int n_ = 0;
return n = n_;
}
}
-- diags: []
setStatus() Snippet:VariableKey(n)#25-int n = 0; - status: VALID
compileAndLoad ins = [Unit(n)] -- legit = [Unit(n)]
Compiler generating class REPL.$JShell$25
compileAndLoad [Unit(n)] -- deps: [] success: true
recordCompilation: Snippet:VariableKey(n)#25-int n = 0; -- status VALID, unresolved []
n ==> 0
入力を解析し,種類を判別
対応するSnippet作成
Snippetに応じた
Wrap
コンパイル依存性解析
#ccc_ab4 https://sli.do/
jshell> Contributing to JShell
#ccc_ab4 https://sli.do/
JShell in OpenJDK
●つまり,OSS!
–Project Kulla
●世界中のどこから,誰からでも貢献可能–新機能提案–バグ報告–フィードバック–パッチ提案
#ccc_ab4 https://sli.do/
How to Contribute
1.JShellを試す!
–https://jdk9.java.net/download/
2.MLに参加!–バグ報告,新機能要求,フィードバックを送る
3.ソースコードを落として開発!–パッチをMLに
–Bitbucketなどで公開
Try! Join! Feedback! Develop!
#ccc_ab4 https://sli.do/
Project Kulla
http://openjdk.java.net/projects/kulla/●ML
http://mail.openjdk.java.net/mailman/listinfo/kulla-dev●リポジトリ
http://hg.openjdk.java.net/jdk9/dev
kullaのリポジトリはマージされて使われていない
●Jira
https://bugs.openjdk.java.net/
http://bit.ly/1q8gzdU
Author以上の権限が無いと課題作成できない
#ccc_ab4 https://sli.do/
より詳細な情報は・・・
●I-6_2 OpenJDK コミュニティに参加してみよう
–17:30-17:50 Room I
–KUBOTA Yuji
–個人的な経験を交えて OpenJDKへの参加方法を紹介
#ccc_ab4 https://sli.do/
jshell> Beyond JShell
#ccc_ab4 https://sli.do/
JavaFX on JShell
●JavaFXをJShellから使いたいけど・・・
jshell> new javafx.stage.Stage()
| java.lang.ExceptionInInitializerError thrown
| at Window.<init> (Window.java:1380)
| at Stage.<init> (Stage.java:239)
| at Stage.<init> (Stage.java:227)
| at (#1:1)
#ccc_ab4 https://sli.do/
JShell for JavaFX:jfxshell
●JavaFXを動かせるJShell作った
–jfxshell
–https://bitbucket.org/bitter_fox/jfxshell
–https://bitbucket.org/bitter_fox/jfxshell/downloads/jfxshell.jar
●JavaFXの全パッケージimport済
#ccc_ab4 https://sli.do/
ブラウザを作ろう(デモ)
WebView wv
TextField address
Button go
#ccc_ab4 https://sli.do/
JShell with Maven
●https://github.com/kawasima/try-artifactjava -jar try-artifact-0.1.2.jar
jshell> /resolve org.apache.commons:commons-lang3:jar:3.4
| Path /home/HOGEHOGE/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar added to classpath
jshell> org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric(30)
| Expression value is: "8D1ysKPGhHPLGpsducw0ch0SnSfixb"
| assigned to temporary variable $1 of type String
※今後利用方法が大幅に変更になる可能性があります
#ccc_ab4 https://sli.do/
マイクロフレームワーク enkan(とkotowari)ではじめるREPL駆動開発
●CD-7 18:30-19:20 Room C+D–川島 義隆さん(@kawasima)
●Javaのマイクロフレームワークenkan●REPL駆動開発
–REPLで挙動を確認,変更しながら開発
–将来的にはJShellで!?
#ccc_ab4 https://sli.do/
まとめ
●JShellで快適Javaプログラミング
●JShellはOSS&拡張可能
●Please Try JShell & Give Me Feedback!!–https://jdk9.java.net/download/
#ccc_ab4 https://sli.do/
JShell developped by
●Engineering
–Robert Field
–Jan Lahoda●OpenJDK Committers
–Shinya Yoshida
–YOU?●Testing
–AndreiEremeev
●Advisors/Cheerleaders/Reviewers
–Brian Goets
–Maurizio Cimadamore
–Joe Darcy
–Paul Sandoz
–Jonathan Gibbions
–MichelTrudeau
–Sundararajan Athijegannathan
–Remi Forax
–Arun Gupta
–Mani sarkar
–Daniel Daugherty
#ccc_ab4 https://sli.do/
Q&A on sli.do#ccc_ab4
●他のライブラリ(jar/maven)とかどう読みこませるのですか
–手元のライブラリは/classpathコマンド
–mavenセントラルリポジトリからは
kawasimaさんが拡張したtry-artifact●ファイルを読み込ませて実行させることはできますか?
–/openコマンド●どこかで動いているJVMに接続して、内部変数のprintとかできますか?
–そういう機能はない–実装上で解決しないといけない問題が多そう
#ccc_ab4 https://sli.do/
Q&A on sli.do#ccc_ab4
●今ある一時変数の一覧とか出せますか?
–/varsで変数一覧は見れるけど,一時変数だけはできない●アノテーションとかも使用可能ですか?
–もちろん!利用可能です!●src配下の参照が出来るようにするおまじないとかありますか?
–/classpath srcでどうでしょうか●static import とかも記述可能ですか?
–もちろん!–import static hoge.Hoge.*
#ccc_ab4 https://sli.do/
Q&A on sli.do#ccc_ab4
●前方参照と同じように、 import も後回しにできますか?
–できます!–ただ,前方参照は宣言においてのみなので,
入力と同時に実行するスニペットではエラー●script で一気にimport や変数読むとか、できますか
–できます!–起動時の場合はstartupのスライドを参照ください
–途中では,/openコマンド
#ccc_ab4 https://sli.do/
Q&A on sli.do#ccc_ab4
●補完のカスタマイズとかできますか?
–補完のプラグインの様な仕組みはないです–ですが,OSSなので,是非フォークしてください!
●一度作ったクラスを消す方法とかあるのか?
–/dropコマンドで削除できます●裏でコンパイルして別の JVM で実行しているということは,*.java やら *.class やらの一時ファイルがこっそり作られてるのでしょうか?
–作られています!–オンメモリで作っているので,HDDは汚しません!
#ccc_ab4 https://sli.do/
Q&A on sli.do#ccc_ab4
●Shebangとして実行する事はできますか?
–できなくはないです–ただし,#!行も実行されてしまう(エラーになる)ため,微妙・・・
●System.exit(0); はできますか?
–是非JDK9をインストールして,jshellで実際に試してみてください!–「System.exit(0)」は別プロセスのJVM上で動くので,別プロセスは終了します.
JShellToolは別プロセスが終了すると,「/reload restore」を実行します
「/reload restore」はリセットして再実行なのですが,なんか怪しい動作する(バグ?)
他にもOOMEなどの再起不能な状態になり,プロセスが終了した場合も,同様な動作をします
#ccc_ab4 https://sli.do/
Q&A on sli.do#ccc_ab4
●JShellのリリーススケジュールとJDKのリリーススケジュールって同期してるんですか?いまからコントリビュートしたとしてJDK9でのリリースに間に合いますか?
–JDKの一部なので同期しています
–現在は以下のスケジュールです●2016/05/26 Feature Complete
–全機能の実装が完了し,テストと共にmasterにマージ●2016/08/11 All Tests Run
–この日までに全てのテストは一度は実行されていないとダメ●2016/09/01 Rampdown Start
–品質向上のための期間がスタート.Phase1ではP1〜P3のバグが修正可能.●2016/10/20 Zero Bug Bounce
–この日までに,JDK9をターゲットにするバグは修正されるか,リスケされなければならない.以降に作成されるバグは原則的に将来のリリースをターゲットとする.
●2016/12/01 Rampdown Phase 2–Phase2ではshowstopperなバグだけが修正可能
●2017/01/26 Final Release Candidate●2017/03/23 General Availability
–jshellはすでにmasterにマージされているため,「Feature Complete」は関係ない(と思われる)
–P4,P5のバグは9/1まで,P1〜P3のバグは10/20まで修正可能!
–つまり,今からコントリビュートしても十分に間に合います!!!あなたとJShell,今すぐクローン!
#ccc_ab4 https://sli.do/
Thank you for your attention!