Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IllegalAccessError when Scala class invokes public super method from package private Java class #22628

Open
lukaseder opened this issue Feb 20, 2025 · 2 comments

Comments

@lukaseder
Copy link

Compiler version

3.6.3

Minimized code

p/I.java

package p;

public interface I {
    String string();
}

p/A.java

package p;

abstract class A implements I {
    @Override
    public String string() {
        return "s";
    }
}

p/B.java

package p;

public class B extends A {
}

q/S.scala

package q

object S {
  def main(args: Array[String]): Unit = {
    println(new S().string())
  }
}

class S extends p.B {
  override def string(): String = super.string()
}

Then run:

scala p/I.java p/A.java p/B.java q/S.scala

Output

Exception in thread "main" java.lang.IllegalAccessError: failed to access class p.A from class q.S (p.A and q.S are in unnamed module of loader 'app')
        at q.S.string(S.scala:10)
        at q.S$.main(S.scala:5)
        at q.S.main(S.scala)

Expectation

This should run just like in Scala 2.13 and earlier, or with the following Java program:

package r;

public class S extends p.B {
    @Override
    public String string() {
        return super.string();
    }

    public static void main(String[] args) {
        System.out.println(new S().string());
    }
}

Workarounds

This can be prevented with an auxiliary override in p.B:

package p;

public class B extends A {
    @Override
    public String string() {
        return super.string();
    }
}

Or, of course, by making A public. Both may not be possible or reasonable, though. I don't really see a workaround on the client side (in q.S)

@Gedochao Gedochao added compat:java regression:scala2 and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants