Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Convert Java File to Kotlin File おかしな変換集
Search
Takehata Naoto
August 25, 2018
Programming
1
580
Convert Java File to Kotlin File おかしな変換集
Kotlin Fest 2018のLT大会発表のスライドです。
Takehata Naoto
August 25, 2018
Tweet
Share
More Decks by Takehata Naoto
See All by Takehata Naoto
KotlinとCloud Vision APIで領収書の電子帳簿保存法対応をする / Cloud Vision API with Kotlin
n_takehata
1
77
KotlinConf 2023 現地参加レポート
n_takehata
1
240
サーバーサイドKotlinクイズ
n_takehata
0
110
サーバーサイドでのKotlin Coroutines
n_takehata
0
890
KotlessとDynamoDBで自分のツイートを収集するサーバーレスアプリケーションを作る
n_takehata
0
290
書籍『Kotlin サーバーサイドプログラミング実践開発』のこだわりとおすすめポイント
n_takehata
0
420
Server-Side Kotlinで必要なJavaの知識
n_takehata
1
410
MyBatis Dynamic SQLの Kotlinサポートを使う
n_takehata
0
770
grpc-kotlinはどこまでKotlin化できるのか?
n_takehata
0
730
Other Decks in Programming
See All in Programming
Long journey of Ruby standard library RubyKaigi 2024
andpad
2
230
Exploring the Implementation of “t.Run”, “t.Parallel”, and “t.Cleanup”
akarin
1
160
Balkan Ruby 2024 — How and why to run SQLite on Rails in production
fractaledmind
0
110
教えて!スクラムコーチ品質とスピードのバランスはどうすりゃいいの?
pinboro
0
120
AppRouter Panel Talk
yosuke_furukawa
PRO
1
530
The test code generator using static analysis and LLM
mikik0
1
170
GNU Makeの使い方 / How to use GNU Make
kaityo256
PRO
13
4.4k
スタックトレース始めてみた
kuro_kurorrr
5
1.1k
CQRS meets modern Java
simas
PRO
2
480
TypeScriptで使いやすいOpenAPIの書き方
yukimochi_dwango
1
910
仕様と実装で学ぶOpenTelemetry
drumato
2
1.1k
freeeのエンジニアが 就活で出そうな コーディングテストを 解説してみる
freee
1
170
Featured
See All Featured
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
104
6.7k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
14
8.4k
Side Projects
sachag
451
41k
Happy Clients
brianwarren
92
6.4k
Debugging Ruby Performance
tmm1
70
11k
Creatively Recalculating Your Daily Design Routine
revolveconf
211
11k
Building Your Own Lightsaber
phodgson
100
5.7k
Put a Button on it: Removing Barriers to Going Fast.
kastner
58
3.1k
Mobile First: as difficult as doing things right
swwweet
217
8.6k
Practical Orchestrator
shlominoach
183
9.8k
Become a Pro
speakerdeck
PRO
13
4.6k
A designer walks into a library…
pauljervisheath
201
23k
Transcript
Convert Java File to Kotlin File おかしな変換集 株式会社 アプリボット ⽵端
尚⼈
⾃⼰紹介 ⽵端 尚⼈ @n_takehata 分野:サーバーサイドエンジニア(Kotlin) 2006.04 2007.12 2011.01 2014.04 公務員
SES サイバーエージェント アプリボット 業種:ゲーム開発
省略します
• IntelliJにはJavaのコードをKotlinに変換して くれる素晴らしい機能があります • 対象ファイルを選択 -> Code -> Convert Java
File to Kotlin File
2万⾏を⼀気に変換した時に出た おかしな変換例を紹介します!
1個⽬
public String getMessage() { UserService userService = new UserService(); return
userService.createMessage(); } val message: String get() { val userService = UserService() return userService.createMessage() } なんかプロパティになってる Java Kotlin
拡張プロパティ val score1 = 0 val score2 = 0 val
totalScore get() = score1 + score2 ・get〜という名前の引数なしメソッドが 全てプロパティとして認識されてしまう ・割とゴリゴリ処理を書いた拡張プロパティが できあがった
2個⽬
Java Kotlin public sta8c Integer calc(Integer num1, Integer num2) {
return num1 + num2; } companion object { fun calc(num1: Int?, num2: Int?): Int { return num1!! + num2!! } } 謎のオブジェクトcompanionが出現する
コンパニオンオブジェクト Javaから呼び出そうとすると、Companionというオブジェクトを 経由しなくてはならなくなる companion object { fun execute() { //
・・・ } } Sample.Companion.execute(); Java Kotlin staticな関数を定義したい時、コンパニオンオブジェクトを使う
companion object { @JvmStaMc fun execute() { // ・・・ }
} @JvmSatic付けてあげてね
3個⽬(最後)
Java public String execute(Integer userId, Integer targetId) { Map<Integer, String>
userMap = createUserMap(); return userMap.entrySet() .stream() .filter(entry -> { if (userMap.containsKey(userId)) { return true; } if (userMap.containsKey(targetId)) { return true; } return false; }).map(entry -> { return entry.getValue(); }).toString(); }
Kotlin fun execute(userId: Int?, targetId: Int?): String { val userMap
= createUserMap() return userMap.entries .stream() .filter { entry -> if (userMap.containsKey(userId)) {
[email protected]
() .stream() .filter true } if (userMap.containsKey(targetId)) {
[email protected]
() .stream() .filter true } false }.map { entry -> entry.value }.toString() }
難しいのであとで聞いてください
• Convert Java File to Kotlin Fileは素晴らしい 機能 • でも頼りすぎるとエラーまみれになる
ツールの⽤法・容量を守って Kotlinを愛でましょう
Kotlinかわいい!
以上