Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlinらしいコードを書こう - Convert Java File to Kotlin ...

Kotlinらしいコードを書こう - Convert Java File to Kotlin File のあとにやること / What to do after Convert Java File to Kotlin File

Yuki Anzai

June 23, 2024
Tweet

More Decks by Yuki Anzai

Other Decks in Technology

Transcript

  1. LPUMJOԽલʹSFOBNFͷDPNNJUΛೖΕΔ % mv DetailHeaderView.java DetailHeaderView.kt % git add -A %

    git commit -m 'rename DetailHeaderView.java to DetailHeaderView.kt' … % mv DetailHeaderView.kt DetailHeaderView.java ͜ͷޙʹ Convert Java File to Kotlin File ͢Δ
  2. Λͳ͘͢ public void addAll(List<String> newItems) { items.addAll(newItems); } Java fun

    addAll(newItems: List<String>?) { items.addAll(newItems!!) } Kotlin (Auto Convert) fun addAll(newItems: List<String>) { items.addAll(newItems) } Kotlin
  3. ෆཁͳ Λͳ͘͢ public static DetailFragment createInstance(String title) { DetailFragment fragment

    = new DetailFragment(); Bundle args = new Bundle(); args.putString(DetailFragment.ARGS_TITLE, title); fragment.setArguments(args); return fragment; } Java public void putString(@Nullable String key, @Nullable String value) { unparcel(); mMap.put(key, value); }
  4. Kotlin (Auto Convert) ෆཁͳ Λͳ͘͢ fun createInstance(title: String?): DetailFragment {

    val fragment = DetailFragment() val args = Bundle() args.putString(ARGS_TITLE, title) fragment.arguments = args return fragment }
  5. ෆཁͳ Λͳ͘͢ fun createInstance(title: String): DetailFragment { val fragment =

    DetailFragment() val args = Bundle() args.putString(ARGS_TITLE, title) fragment.arguments = args return fragment } Kotlin
  6. MBUFJOJUWBS Kotlin (Auto Convert) class ViewHolder { private var titleView:

    TextView? = null fun init(view: View) { titleView = view.findViewById(R.id.title_view) } fun setTitle(title: String?) { titleView!!.text = title } }
  7. MBUFJOJUWBS class ViewHolder { private lateinit var titleView: TextView fun

    init(view: View) { titleView = view.findViewById(R.id.title_view) } fun setTitle(title: String) { titleView.text = title } } Kotlin
  8. WBSΛͰ͖Δ͔͗Γ΍ΊΔ int sum = 0; for (Item item : items)

    { sum += item.getPrice(); } Java var sum = 0 for (item in items) { sum += item.price } Kotlin (Auto Convert) val sum = items.sumOf { it.price } Kotlin
  9. LPUMJOTUEMJCΛ࢖͏ w ͍ΖΜͳศརؔ਺͕͋ΔͷͰࣗ෼Ͱ࣮૷͢Δલʹௐ΂Α͏ w fi MUFS  fi MUFS/PU/VMM NBQ

    NBQ/PU/VMM w fl BU.BQ JG&NQUZ w HFU0S/VMM w EJTUJODU EJTUJODU#Z w DPFSDF"U.PTU DPFSDF"U-FBTU DPFSDF*O w BT4FRVFODF
  10. MFU  Λ࢖͏ LogParams log; if (title == null) {

    log = null; } else { log = new LogParams(title); } Java val log = if (title == null) { null } else { LogParams(title) } Kotlin (Auto Convert) val log = title?.let { LogParams(it) } Kotlin
  11. BQQMZ BMTP TextView titleView = view.findViewById(R.id.title_view); titleView.setTextSize(16); titleView.setTextColor(getResources().getColor(R.color.title)); titleView.setText(title); Java

    val titleView = view.findViewById<TextView>(R.id.title_view) titleView.textSize = 16f titleView.setTextColor(resources.getColor(R.color.title)) titleView.text = title Kotlin (Auto Convert)
  12. BQQMZ BMTP val titleView = view.findViewById<TextView>(R.id.title_view) titleView.textSize = 16f titleView.setTextColor(resources.getColor(R.color.title))

    titleView.text = title Kotlin (Auto Convert) view.findViewById<TextView>(R.id.title_view).apply { textSize = 16f setTextColor(resources.getColor(R.color.title)) text = title } Kotlin
  13. @Λ࢖͏ okButton.setListener((v, log) -> onClickOk(log)); Java Kotlin (Auto Convert) okButton.setListener

    { v: View?, log: String -> onClickOk(log) } okButton.setListener { _, log -> onClickOk(log) } Kotlin
  14. ֦ுؔ਺Λ࢖͏ fun convert(value: A): B { … } Kotlin ͷ৔߹

    val b = a?.let { convert(it) } fun A.toB(): B { … } ͷ৔߹ val b = a?.toB()
  15. 4NBSUDBTU if (parentFragment instanceof Listener) { ((Listener) parentFragment).onUpdate(title); } Java

    if (parentFragment is Listener) { (parentFragment as Listener).onUpdate(title) } Kotlin (Auto Convert) if (parentFragment is Listener) { parentFragment.onUpdate(title) } Kotlin
  16. BT if (parentFragment instanceof Listener) { ((Listener) parentFragment).onUpdate(title); } Java

    if (parentFragment is Listener) { (parentFragment as Listener).onUpdate(title) } Kotlin (Auto Convert) (parentFragment as? Listener)?.onUpdate(title) Kotlin
  17. XIFO public static ImageUrl create( String light, String dark )

    { if (light == null) { return null; } else if (dark == null) { return null; } else { return new ImageUrl(light, dark); } } Java Kotlin (Auto Convert) companion object { fun create( light: String?, dark: String? ): ImageUrl? { return if (light == null) { null } else if (dark == null) { null } else { ImageUrl(light, dark) } } }
  18. Kotlin (Auto Convert) XIFO companion object { fun create( light:

    String?, dark: String? ): ImageUrl? { return if (light == null) { null } else if (dark == null) { null } else { ImageUrl(light, dark) } } } companion object { fun create( light: String?, dark: String? ): ImageUrl? { return when { light == null -> null dark == null -> null else -> ImageUrl(light, dark) } } } Kotlin
  19. ؔ਺ࢀর cancelButton.setListener(() -> { onClickCancel(); return Unit.INSTANCE; }); Java cancelButton.setListener

    { onClickCancel() Unit } Kotlin (Auto Convert) cancelButton.setListener(::onClickCancel) Kotlin fun setListener(listener: () -> Unit) { this.listener = listener }
  20. Ͱ͖Δ͚ͩ*NNVUBCMF$PMMFDUJPOʹ͢Δ List<String> log = new ArrayList<>(); for(int i = 0;

    i < items.size(); i++){ Item item = items.get(i); log.add(i + ": " +item.getTitle()); } Java val log: MutableList<String> = ArrayList() for (i in items.indices) { val item = items[i] log.add(i.toString() + ": " + item.title) } Kotlin (Auto Convert)
  21. Ͱ͖Δ͚ͩ*NNVUBCMF$PMMFDUJPOʹ͢Δ val log: MutableList<String> = ArrayList() for (i in items.indices)

    { val item = items[i] log.add(i.toString() + ": " + item.title) } Kotlin (Auto Convert) val log = items.mapIndexed { i, item -> "$i: ${item.title}" } Kotlin
  22. .VUBCMF$PMMFDUJPOΛެ։͠ͳ͍ public class DataHolder { public final List<String> items =

    new ArrayList<>(); public void addAll(List<String> newItems) { items.addAll(newItems); } Java class DataHolder { val items: MutableList<String> = ArrayList() fun addAll(newItems: List<String>?) { items.addAll(newItems!!) } Kotlin (Auto Convert)
  23. .VUBCMF$PMMFDUJPOΛެ։͠ͳ͍ class DataHolder { private val _items: MutableList<String> = ArrayList()

    val items: List<String> get() = _items fun addAll(newItems: List<String>) { _items.addAll(newItems) } } Kotlin
  24. "SSBZ-JTU ˠNVUBCMF-JTU0G public class DetailFragment extends Fragment { private List<String>

    items = new ArrayList<>(); void addItem(String item) { items.add(item); } Java class DetailFragment : Fragment() { private val items: MutableList<String> = ArrayList() fun addItem(item: String) { items.add(item) } Kotlin (Auto Convert)
  25. "SSBZ-JTU ˠNVUBCMF-JTU0G class DetailFragment : Fragment() { private val items:

    MutableList<String> = ArrayList() fun addItem(item: String) { items.add(item) } Kotlin (Auto Convert) class DetailFragment : Fragment() { private val items = mutableListOf<String>() fun addItem(item: String) { items.add(item) } Kotlin HashMap, LinkedHashMap → mutableMapOf() HashSet, LinkedHashSet → mutableSetOf()
  26. CVJME-JTU\^ CVJME.BQ\^ ArrayList<String> params = new ArrayList<>(); if (!hasParamA) {

    params.add(PARAM_A); } else { if (hasParamB) { params.add(PARAM_B); } else { params.add(PARAM_C); } } Java val params = ArrayList<String>() if (!hasParamA) { params.add(PARAM_A) } else { if (hasParamB) { params.add(PARAM_B) } else { params.add(PARAM_C) } } Kotlin (Auto Convert)
  27. CVJME-JTU\^ CVJME.BQ\^ val params = ArrayList<String>() if (!hasParamA) { params.add(PARAM_A)

    } else { if (hasParamB) { params.add(PARAM_B) } else { params.add(PARAM_C) } } Kotlin (Auto Convert) val params = buildList { if (!hasParamA) { add(PARAM_A) } else { if (hasParamB) { add(PARAM_B) } else { add(PARAM_C) } } } Kotlin Immutable
  28. CVOEMF0G Bundle args = new Bundle(); args.putString(DetailFragment.ARGS_TITLE, title); fragment.setArguments(args); Java

    val args = Bundle() args.putString(ARGS_TITLE, title) fragment.arguments = args Kotlin (Auto Convert) fragment.arguments = bundleOf( ARGS_TITLE to title ) Kotlin
  29. 5FYU6UJMTJT&NQUZ ˠJT/VMM0S&NQUZ val title = args.getString(ARGS_TITLE) if (!TextUtils.isEmpty(title)) { …

    } Kotlin (Auto Convert) val title = args.getString(ARGS_TITLE) if (!title.isNullOrEmpty()) { … } Kotlin String title = args.getString(ARGS_TITLE); if (!TextUtils.isEmpty(title)) { … } Java
  30. 5FYU6UJMTFRVBMT ˠ val title = args.getString(ARGS_TITLE) if (TextUtils.equals(title, current)) {

    … } Kotlin (Auto Convert) val title = args.getString(ARGS_TITLE) if (title == current) { … } Kotlin String title = args.getString(ARGS_TITLE); if (TextUtils.equals(title, current)) { … } Java
  31. CZWJFX.PEFMT private DetailViewModel viewModel; @Override public void onCreate(@Nullable Bundle savedInstanceState)

    { super.onCreate(savedInstanceState); viewModel = new ViewModelProvider(this).get(DetailViewModel.class); Java private var viewModel: DetailViewModel? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) viewModel = ViewModelProvider(this).get(DetailViewModel::class.java) Kotlin (Auto Convert)
  32. CZWJFX.PEFMT private var viewModel: DetailViewModel? = null override fun onCreate(savedInstanceState:

    Bundle?) { super.onCreate(savedInstanceState) viewModel = ViewModelProvider(this).get(DetailViewModel::class.java) Kotlin (Auto Convert) private val viewModel: DetailViewModel by viewModels() Kotlin
  33. MBUFJOJUWBSGPSˏ*OKFDU @AndroidEntryPoint public class DetailFragment extends Fragment { @Inject DetailLogger

    logger; Java @AndroidEntryPoint class DetailFragment : Fragment() { @Inject var logger: DetailLogger? = null Kotlin (Auto Convert) @AndroidEntryPoint class DetailFragment : Fragment() { @Inject lateinit var logger: DetailLogger Kotlin
  34. νΣοΫϦετ w ͸࢒͍ͬͯͳ͍͔ w ෆཁͳ ͸࢒͍ͬͯͳ͍͔ w MBUFJOJUWBSʹͰ͖Δͱ͜Ζ͸ͳ͍͔ w WBMʹͰ͖ΔWBS͸࢒͍ͬͯͳ͍͔

    w ࢖͑ΔTUEMJC͸ͳ͍͔ w MFU  Ͱ؆ܿʹͰ͖Δͱ͜Ζ͸ͳ͍͔ w BQQMZ BMTPͰ·ͱΊΒΕΔॳظԽॲཧ͸ͳ͍͔
  35. νΣοΫϦετGPS"OESPJE w CVOEMF0G͕࢖͑Δͱ͜Ζ͸ͳ͍͔ w 5FYU6UJMTJT&NQUZ ͕࢒ͬͯͳ͍͔ w 5FYU6UJMTFRVBMT ͕࢒ͬͯͳ͍͔ w

    CZWJFX.PEFMT Λ࢖͍ͬͯΔ͔ w !*OKFDU͕͍ͭͨ fi FME͸MBUFJOJUWBSʹͳ͍ͬͯΔ͔ɹ