What I did when I converted java to Kotlin

Overview

I'm scribbling down how to solve the error I got when converting from Java to Kotlin with the Inttellij feature. I may organize it later. I'm a Kotlin beginner and I just erased the error for the time being, so there seems to be a place where it is badly written programmatically. Please let me know if there is a better solution.

Case study

If you have generics, you absolutely have to put it on

For the time being, I attached all <*>

before


var controller: Controller

after


var controller: Controller<*>

If you set the generics to <*>, an error will occur in the method that takes the generics as an argument.

<\ > Seems to be an abbreviation for \ , and out seems to be a restriction that uses generics only as a return value. There was a big difference in generics from Java around here, and I couldn't solve it by stopping thinking. For the time being, I had no choice but to look at the program and put in something other than <>. I'm not sure about kotlin's generics.

before


var controller: Controller

after


var controller: Controller<Data>

I can't have circular generics

This time it was just wasteful, so I erased one side and solved it. There was a method to make a new type and solve it by force, but linguistically, maybe it's about making circular generics.

Getter setter

It disappeared by conversion. In Kotlin, write to access the field as it is. Visibility seems to be read-only by declaring a private set.

before


game.getFinishStatus()

after


game.finishStatus

before


game.setFinishStatus(FinishStatus(true))

after


game.finishStatus=FinishStatus(true)

override

Overridden methods must have the override modifier. It's usually attached automatically, but there were some who didn't. Can you automatically add method overrides for Objects such as toString? Some of them weren't attached even in their own class. I'm not sure.

before


    fun toString(): String {
        return text
    }

after


    override fun toString(): String {
        return text
    }

list add

Kotlin's default list does not allow you to add or remove elements. If you declare it as List in Java, it will be converted to the default list, so add cannot be used. Rewrite the declaration to MutableList.

before


private var commandList: List<IN>? = null
commandList!!.add(command)//error

after


private var commandList: MutableList<IN>? = null
commandList!!.add(command)//No error

list remove

The one whose argument is the index is removeAt

before


shapeList.remove(0)

after


shapeList.removeAt(0)

cast

It disappeared for some reason. If you are doing a safe cast, it seems that there is no problem even if you delete the cast, but there was an error. Maybe it was a cast using Generics.

before


cd//originally(IN)cd //IN is generics

after


cd as IN

Variadic argument assignment

Since the type is different between the variable length argument and the normally declared array, it cannot be assigned. Create a new array from variable length arguments and assign.

before


class ControllerGroup(vararg controllers: Controller<*>) {
    private val controllers: Array<Controller<*>>
    init {
        this.controllers = controllers
    }
}

after


class ControllerGroup(vararg controllers: Controller<*>) {
    private val controllers: Array<Controller<*>>
    init {
        this.controllers = arrayOf(*controllers)
    }
}

Null check for method arguments

Null check related is usually done automatically, but it seems that method arguments are not done?

before


        game.addShape(shape)//Shape?That's why it's said no

after


        game.addShape(shape!!)

Array to initialize later

When you want to remove from the Null check an array that is first filled with Null and then filled with values (resulting in no Null). If you are using Arrays.fill or something. Insert when initializing Array

before


val results = arrayOfNulls<RPSResult>(3)
Arrays.fill(results,RPSResult.DRAW)

after


val results = Array<RPSResult>(3,{RPSResult.DRAW})

static import It seems that you can't call a static method without giving a class name with static import.

before


import game.rps.shape.Shape.*
///////////////////////////////
val results = judgeAll(SCISSOR, SCISSOR, SCISSOR, SCISSOR)

after


val results = Shape.judgeAll(SCISSOR, SCISSOR, SCISSOR, SCISSOR)

The one that doesn't need null but will be initialized later

Add lateinit modifier

before


private var currentShape: Array<Shape?>

after


private lateinit var currentShape: Array<Shape?>

Put an array directly in a variadic argument

before


 Shape.judgeAll(currentShapes)

after


 Shape.judgeAll(*currentShapes)

toArray toTypedArray

before


currentList.toArray(arrayOfNulls<Shape>(currentList.size))

after


currentList.toTypedArray()

reference

I've given all the ones I've seen for the time being, so some of them have nothing to do with the information I wrote here, and I've definitely forgotten some. https://qiita.com/k5n/items/c8bf7a507b64f20eebd0 https://qiita.com/tasogarei/items/266ecf02576d48fc69f6 https://stackoverflow.com/questions/46682455/how-to-solve-violation-of-finite-bound-restriction-in-kotlin https://qiita.com/ssuzaki/items/8a550fca6775c1e6e147 https://qiita.com/k5n/items/18adb5c3503a54e96c22 https://qiita.com/ke__kyukyun1828/items/3832d0bf42e6f7ef150a https://qiita.com/koher/items/d9411a00986f14683a3f https://dogwood008.github.io/kotlin-web-site-ja/docs/reference/packages.html https://qiita.com/k5n/items/cc0377b75d8537ef8a85

Recommended Posts

What I did when I converted java to Kotlin
What I thought about when I started migrating from Java to Kotlin
What Java inexperienced people did to study Kotlin
[java] What I did when comparing Lists in my own class
I did Java to make (a == 1 && a == 2 && a == 3) always true
Summary of points I was worried about when migrating from java to kotlin
paiza Ruby What I did to become B rank
What I learned when building a server in Java
I want to transition screens with kotlin and java!
I tried to find out what changed in Java 9
What I researched about Java 8
What I researched about Java 6
What I researched about Java 9
What I learned about Kotlin
What I researched about Java 5
What I was addicted to when introducing the JNI library
What I fixed when updating to Spring Boot 1.5.12 ・ What I was addicted to
I tried to summarize the basics of kotlin and java
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
What I did when I stumbled on IntelliJ gradle 2.2 → 2.6 environment migration
I want to implement various functions with kotlin and java!
Kotlin Class to send to Java developers
I tried to interact with Java
What I learned with Java Gold
I tried to summarize Java learning (1)
What I learned with Java Silver
What I researched about Java learning
[Android] Convert Android Java code to Kotlin
I tried to summarize Java 8 now
What to do when javax.batch.operations.JobStartException occurs
I want to return to the previous screen with kotlin and java!
About what I did when creating a .clj file in Clojure
What I did when the DB did not start with docker-compose up
I tried to translate the error message when executing Eclipse (Java)
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I was addicted to when implementing google authentication with rails
What I did in the migration from Spring Boot 1.5 series to 2.0 series
What I often do when I have trouble naming with Java (etc.)
Kotlin Class part.2 to send to Java developers
Memorandum: What I was addicted to when I hit the accounting freee API
What I thought when passing the user input value to the Service class
What I don't like when using interface of a function with default arguments in Kotlin from Java
I tried to summarize Java lambda expressions
Convert all Android apps (Java) to Kotlin
What you did to get the Oracle Certified Java Programmer, Silver SE 8
What to do when a javax.el.PropertyNotWritableException occurs
What I did when JSF couldn't display database information in the view
Kotlin scope functions to send to Java developers
I did OpenCV camera calibration in Java
I want to stop Java updates altogether
I want to get the IP address when connecting to Wi-Fi in Java
What is Docker? I tried to summarize
Precautions when migrating from VB6.0 to JAVA
What Java programmers find useful in Kotlin
I went to the Java Women's Club # 1
[* Java *] I went to JJUG CCC 2017 Fall
What to do when undefined method ʻuser_signed_in?'
What I learned from Java monetary calculation
Memo for migration from java to kotlin