IllegalAccessError while using a public method in abstract package-private class

I have two java classes:

package me.goto1134;

abstract class ATest<M extends ATest<M>> {
    public M test(final M t) {
        return t;
    }

    @Override
    public String toString() {
        return "Test";
    }
}

and

package me.goto1134;

public class Test extends ATest<Test> {
}

if I run a kotlin test

import me.goto1134.Test

fun main() {
    val test1 = Test()
    val test2 = Test()
    print(test1.test(test2))
}

I get

Exception in thread “main” java.lang.IllegalAccessError: tried to access class me.goto1134.ATest from class TestKt
at TestKt.main(test.kt:6)
at TestKt.main(test.kt)

but running Java test

import me.goto1134.Test;

public class AbstractClassGenericMethodTest {
    public static void main(String[] args) {
        Test test1 = new Test();
        Test test2 = new Test();
        System.out.println(test1.test(test2));
    }
}

will lead to a valid output.

How can I work aroung the problem?

P.S. I found the problem using ojalgo library that is used for Linear Algebra in my case.

There is also a relevant(?) issue https://youtrack.jetbrains.com/issue/KT-11700, but there is no progress on solving it.

1 Like

Yes, that’s the issue. It was recently discussed at a design meeting, but we don’t have a definite way to fix this for now.

I see only one way to avoid the error with Kotlin code, if you have access to class Test: explicitly specify an override like this:

package me.goto1134;

public class Test extends ATest<Test> {
    @Override
    public Test test(final Test t) {
        return t;
    }
}