Automatically converted Kotlin file doesn't compile

Hi everyone,

This is my first attempt at switching to Kotlin but I’m stuck.

I have an existing java project so I figured the easiest way to switch to Kotlin was to automatically convert one of my unit test classes (by using Code → Convert Java File to Kotlin File).

My test creates some data with a specific pattern and ensures the pattern is detected by checking that the detected representation object is of the correct class.

assertEquals(MySpecificPattern.class, discoveredRepresentation.getClass());

This line gets converted to:

assertEquals(MySpecificPattern::class.java, discoveredRepresentation.javaClass)

If I try to rebuild the project, I get the following errors:

Error:(21, 6) Kotlin: Cannot access class ‘kotlin.reflect.KClass’. Check your module classpath for missing or conflicting dependencies
Error:(28, 51) Kotlin: Unresolved reference: java
Error:(28, 63) Kotlin: Unresolved reference: javaClass

This unit test class is the first class being converted to Kotlin so I want this to work before I convert anything else. I’m using IntelliJ Ultimate 2017.1.4 and I’m not using Maven so it’s just a simple java project.

Do you have kotlin-reflect on your classpath while running this test?

In Maven’s pom.xml it would look like

<dependency>
   <groupId>org.jetbrains.kotlin</groupId>
   <artifactId>kotlin-reflect</artifactId>
   <version>${kotlin.version}</version>
</dependency>

where ${kotlin.version} is the version of kotlin you’re using.

Thanks for your reply. I finally got another chance to work on this so I converted my project to gradle (it used to be a regular IntelliJ project) and now converting files to Kotlin works out of the box without any additional manual steps.

Snippet from build.gradle:

buildscript {
ext.kotlin_version = ‘1.1.2’

repositories {
    mavenCentral()
}
dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

}

apply plugin: ‘java’
apply plugin: ‘kotlin’

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
compile “org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version”
testCompile group: ‘junit’, name: ‘junit’, version: ‘4.12’
}

I used Code → Convert Java File to Kotlin File and it converted the class to Kotlin like before but now it no longer complained about anything.

Looking forward to a deep dive in Kotlin.

Thanks