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
670
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
組織貢献をするフリーランスエンジニアという生き方
n_takehata
2
1.4k
「2024年版 Kotlin サーバーサイドプログラミング実践開発」の補講 〜O/Rマッパー編〜
n_takehata
2
590
2024年版 Kotlin サーバーサイドプログラミング実践開発
n_takehata
7
5k
Server-Side目線で見る、Kotlin Festの楽しみ方
n_takehata
0
400
KotlinとCloud Vision APIで領収書の電子帳簿保存法対応をする
n_takehata
1
900
KotlinConf 2023 現地参加レポート
n_takehata
1
320
サーバーサイドKotlinクイズ
n_takehata
0
190
サーバーサイドでのKotlin Coroutines
n_takehata
0
1.2k
KotlessとDynamoDBで自分のツイートを収集するサーバーレスアプリケーションを作る
n_takehata
0
400
Other Decks in Programming
See All in Programming
.NET Frameworkでも汎用ホストが使いたい!
tomokusaba
0
170
昭和の職場からアジャイルの世界へ
kumagoro95
1
400
新宿駅構内を三人称視点で探索してみる
satoshi7190
2
110
dbt Pythonモデルで実現するSnowflake活用術
trsnium
0
210
PRレビューのお供にDanger
stoticdev
1
200
Rubyで始める関数型ドメインモデリング
shogo_tksk
0
120
Ruby on cygwin 2025-02
fd0
0
150
技術を根付かせる / How to make technology take root
kubode
1
250
パスキーのすべて ── 導入・UX設計・実装の紹介 / 20250213 パスキー開発者の集い
kuralab
3
830
AWS Organizations で実現する、 マルチ AWS アカウントのルートユーザー管理からの脱却
atpons
0
150
GitHub Actions × RAGでコードレビューの検証の結果
sho_000
0
280
Grafana Cloudとソラカメ
devoc
0
180
Featured
See All Featured
Faster Mobile Websites
deanohume
306
31k
Testing 201, or: Great Expectations
jmmastey
42
7.2k
Building an army of robots
kneath
303
45k
Large-scale JavaScript Application Architecture
addyosmani
511
110k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Scaling GitHub
holman
459
140k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
4 Signs Your Business is Dying
shpigford
182
22k
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かわいい!
以上