Successful Ant->IDEA move but a problem with a simple Java->Kotlin module conversion


#1

People,

OK, after some days of IDEA acclimatisation, I have now got my 14 module Java app running successfully in IDEA and can create a Jar file that runs from my server script. However, the point of doing this exercise is to see if Kotlin is nice to use for continuing development / enhancement of the app. I converted this “info.java” module:

package com.thisiscool.conversation;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Info implements Serializable
{
public static final String PARSE = "PARSE";
public static final String UPDATE = "UPDATE";
public static final String HAPPY = "HAPPY";
public static final String SWEAR = "SWEAR";
public static final String UNHAPPY = "UNHAPPY";
public static final String INPUT_TRUNCATED = "InputTruncated";
public static final String INPUT_START_TIME = "InputStartTime";
public static final String RAW_CURRENT_SENTENCE = "RawCurrentSentence";
public static final String DISABLE_MATCH_CLAUSES = "DisableMatchClauses";

public Info()
{}

public final boolean isEmpty()
{
	return info.isEmpty();
}

public final int size()
{
	return info.size();
}

public final void clear()
{
	info.clear();
}

public final boolean containsKey(String key)
{
	return info.containsKey(key);
}

public final Object remove(String key)
{
	return info.remove(key);
}

public final Object get(String key)
{
	return info.get(key);
}

public final Object put(String key, Serializable value)
{
	return info.put(key, value);
}

// private //
private Map<String,Serializable> info = new HashMap<String, Serializable>();
}

I was prompted that some other things would need changing and clicked OK, got a message saying: “Kotlin not configured” -> clicked “Configure”. This resulted in this file:

package com.thisiscool.conversation

import java.io.Serializable
import java.util.HashMap

class Info : Serializable {

    val isEmpty: Boolean
        get() = info.isEmpty()

    fun size(): Int {
        return info.size
    }

    fun clear() {
        info.clear()
    }

    fun containsKey(key: String): Boolean {
        return info.containsKey(key)
    }

    fun remove(key: String): Any {
        return info.remove(key)
    }

    operator fun get(key: String): Any {
        return info[key]
    }

    fun put(key: String, value: Serializable): Any {
        return info.put(key, value)
    }

    // private //
    private val info = HashMap<String, Serializable>()

    companion object {
        val PARSE = "PARSE"
        val UPDATE = "UPDATE"
        val HAPPY = "HAPPY"
        val SWEAR = "SWEAR"
        val UNHAPPY = "UNHAPPY"
        val INPUT_TRUNCATED = "InputTruncated"
        val INPUT_START_TIME = "InputStartTime"
        val RAW_CURRENT_SENTENCE = "RawCurrentSentence"
        val DISABLE_MATCH_CLAUSES = "DisableMatchClauses"
    }
}

but when I tried to build I got these warnings and errors:

Information:Kotlin: Kotlin JPS plugin version 1.1.2
Information:Kotlin: Plugin loaded: KotlinAndroidJpsPlugin
Information:Kotlin: Using kotlin-home = /home/phil/idea-IU-172.2827.15/plugins/Kotlin/kotlinc
Information:Kotlin: Kotlin Compiler version 1.1.2
Information:Module “Conversation” was fully rebuilt due to project configuration/dependencies changes
Information:6/13/17 1:45 PM - Compilation completed with 3 errors and 0 warnings in 3s 913ms
/home/phirho/Honesty/Honesty/Conversation/src/com/thisiscool/conversation/Info.kt
Error:(24, 16) Kotlin: Type mismatch: inferred type is Serializable? but Any was expected
Error:(28, 16) Kotlin: Type mismatch: inferred type is Serializable? but Any was expected
Error:(32, 16) Kotlin: Type mismatch: inferred type is Serializable? but Any was expected

Suggestions about what to do now?

Thanks,

Phil.


#2

Java-to-Kotlin conversion is not always capable to guess the valid nullability of type. For example in your case info[key] can return null if the key is missing, however the return type Any doesn’t allow nulls.

You can either relax return type to nullable Any? or handle the situation when null is returned:

info[key] ?: error("Key $key is missing")

Here error is just a function throwing IllegalStateException with the given message.


#3

Ilya,

ilya.gorbunov [1] JetBrains Team
June 13

Java-to-Kotlin conversion is not always capable to guess the valid
nullability of type. For example in your case info[key] can return
null if the key is missing, however the return type Any doesn’t allow
nulls.

You can either relax return type to nullable Any? or handle the
situation when null is returned:

info[key] ?: error(“Key $key is missing”)

Here error is just a function throwing IllegalStateException with the
given message.

Many thanks for that! I appreciate it! I have posted 3 messages with
questions so far and the only other response was someone saying I posted
on the wrong forum! (I didn’t agree) - I was on the verge of giving up
and going back to JRuby to try and resolve my way forward . .

Regards,

Phil.