Kotlin java abstract class IllegalAccessError


#1

I am having a issue with java interop and Kotlin I have a package protected java abstract class ,AbstractTest, which is used as a member in another class,TestHolder. When I create a derived instance of the abstract class ,Test, and try to use it in kotlin I get:

java.lang.IllegalAccessError: tried to access class test.AbtractTest from class KotlinTest

The Java version of the same code seems to work fine though …

package test;

abstract class AbtractTest {
}

package test;

public class Test extends AbtractTest {
}

package test;

public class TestHolder {
    private AbtractTest test;

    public AbtractTest getTest() {
        return test;
    }

    public void setTest(AbtractTest test) {
        this.test = test;
    }
}

public class JavaTest {
    private TestHolder testHolder;

    public JavaTest() {
        testHolder = new TestHolder();
        testHolder.setTest(new Test());
    }
}

fun main(args: Array<String>) {
    JavaTest()
    KotlinTest()
}

class KotlinTest {
    val testHolder: TestHolder

    init {
        testHolder = TestHolder()
        testHolder.test= Test()
    }
}

#2

If KotlinTest class is not in the same test package, it cannot access package protected class AbstractTest.


#3

I am curious why it works in the Java Class … any ideas?


#4

Kotlin generates redundant CHECKCAST test/AbtractTest instruction before passing Test instance to setTest.
That CHECKCAST fails with IllegalAccessError if its type argument is inaccessible in that package.

This problem is tracked in the issue https://youtrack.jetbrains.com/issue/KT-15315