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
2024 コーディング研修
ckazu
2
660
ペパボOpenTelemetry革命
pyama86
2
1.1k
The Final Frontier of Web Development: React Server Components vs Jakarta EE
ivargrimstad
0
130
freeeのエンジニアが 就活で出そうな コーディングテストを 解説してみる
freee
1
170
TypeScriptとGraphQLで実現する 型安全なAPI実装 / TSKaigi 2024
hokaccha
5
2.8k
TypeScriptで使いやすいOpenAPIの書き方
yukimochi_dwango
1
910
WinActorの勉強を継続する方法
tamai_63
0
130
Try creating your own orderedmap
kazamori
1
280
CQRS meets modern Java
simas
PRO
2
480
Three ways to use AI on Android: The Good, the Bad and the Ugly
marxallski
0
120
TypeScriptから始める VR生活
tamagokakeg
2
120
Next.js App Router
quramy
14
2.3k
Featured
See All Featured
The Illustrated Children's Guide to Kubernetes
chrisshort
32
47k
What's new in Ruby 2.0
geeforr
338
31k
Facilitating Awesome Meetings
lara
43
5.6k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
21
2k
Robots, Beer and Maslow
schacon
PRO
155
8k
Fashionably flexible responsive web design (full day workshop)
malarkey
398
65k
What the flash - Photography Introduction
edds
64
11k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
Designing with Data
zakiwarfel
96
4.8k
What’s in a name? Adding method to the madness
productmarketing
PRO
17
2.7k
Debugging Ruby Performance
tmm1
70
11k
GraphQLの誤解/rethinking-graphql
sonatard
56
9.3k
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かわいい!
以上