0

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()
    }
}
ice1000
  • 6,406
  • 4
  • 39
  • 85
Manish V
  • 325
  • 3
  • 18

2 Answers2

0

Your code looks like:

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

class KotlinTest {
    val testHolder: TestHolder

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

Which doesn't show any package declaration.

Adding package test at the beginning of this file will probably solve this.

In Kotlin, you're not forced to make your file location match your package name, so no package name will not cause an "error", but lead to an IllegalAccessException.

ice1000
  • 6,406
  • 4
  • 39
  • 85
0

I ran into the same issue and found that it's a bug in Kotlin compiler. The issue is tracked here:

https://youtrack.jetbrains.com/issue/KT-1170

Other resources:

https://discuss.kotlinlang.org/t/kotlin-java-abstract-class-illegalaccesserror/2544/4

Srki Rakic
  • 499
  • 5
  • 15