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
KotlinConf 2023 現地参加レポート
n_takehata
1
230
サーバーサイドKotlinクイズ
n_takehata
0
110
サーバーサイドでのKotlin Coroutines
n_takehata
0
860
KotlessとDynamoDBで自分のツイートを収集するサーバーレスアプリケーションを作る
n_takehata
0
280
書籍『Kotlin サーバーサイドプログラミング実践開発』のこだわりとおすすめポイント
n_takehata
0
420
Server-Side Kotlinで必要なJavaの知識
n_takehata
1
400
MyBatis Dynamic SQLの Kotlinサポートを使う
n_takehata
0
760
grpc-kotlinはどこまでKotlin化できるのか?
n_takehata
0
710
サーバサイドKotlinでgRPCをやってみよう
n_takehata
1
7.4k
Other Decks in Programming
See All in Programming
Micro Frontends for Java Microservices - Devnexus 2024
mraible
PRO
0
470
エンターテイメント業界で利用されるAWS
demuyan
0
210
FigmaとPHPで作る1ミリたりとも表示崩れしない最強の帳票印刷ソリューション
ttskch
43
18k
入門 AWS Amplify Gen2 / Introduction to AWS Amplify Gen2
genkiogasawara
1
320
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
360
R言語の環境構築と基礎 Tokyo.R 112
bob3bob3
0
250
コードレビューで学ぶ!Kotlinオブジェクト指向デザインパターン
akkie76
2
190
Hanami and htmx
bkuhlmann
0
200
効率化に挑戦してみたらモバイル開発が少し快適になった話
ryunakayama
0
120
#phpcon_odawara オープン・クローズドなテストフィクスチャを求めて / open closed test fixtures
77web
3
220
PHPの次期バージョンはこの時期どうなっているのか - Internalsの開発体制について - PHPカンファレンス小田原
youkidearitai
PRO
1
190
使ってみよう Azure AI Document Intelligence
kosmosebi
2
270
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
237
11k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
5
1.5k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
13
1.5k
Design by the Numbers
sachag
274
18k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
20
1.6k
Fantastic passwords and where to find them - at NoRuKo
philnash
36
2.5k
It's Worth the Effort
3n
180
27k
The Cult of Friendly URLs
andyhume
74
5.7k
Build The Right Thing And Hit Your Dates
maggiecrowley
23
2k
Writing Fast Ruby
sferik
620
60k
GraphQLの誤解/rethinking-graphql
sonatard
50
9.2k
Why Our Code Smells
bkeepers
PRO
331
56k
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かわいい!
以上