Skip to content

IllegalAccessError when calling java method defined in base class #8661

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

Closed
ohze opened this issue Apr 4, 2020 · 2 comments
Closed

IllegalAccessError when calling java method defined in base class #8661

ohze opened this issue Apr 4, 2020 · 2 comments

Comments

@ohze
Copy link

ohze commented Apr 4, 2020

Minimized code

  • pk/B.java
package pk;
class B {
    public void hi() { System.out.println("hi"); }
}
  • pk/C.java
package pk;
public class C extends B {
    public C() {}
}
  • Main.scala
import pk.C
object Main {
  def main(args: Array[String]): Unit = {
    identity(new C).hi()
  }
}

Output

[info] running (fork) Main 
[error] Exception in thread "main" java.lang.IllegalAccessError: tried to access class pk.B from class Main$
[error]         at Main$.main(Main.scala:5)
[error]         at Main.main(Main.scala)

Expectation

run successfully as in scala 2

Notes

In sbt: set Compile/run/fork := true to mute another errors:

[error] java.lang.IllegalAccessError: tried to access class pk.B from class Main$
[error] If using a layered classloader, this can occur if jvm package private classes
 are accessed across layers. This can be fixed by changing to the Flat or
 ScalaInstance class loader layering strategies.
@ohze ohze added the itype:bug label Apr 4, 2020
@ohze
Copy link
Author

ohze commented Apr 4, 2020

Inspect

Decompile the Main$.class file:

  • scala 2:
public final class Main$ ...
    public void main(final String[] args) {
        ((C)Predef$.MODULE$.identity((Object)new C())).hi();
    }
  • dotty 0.23.0-RC1 (same for 0.24.0-bin-20200403-2c3212b-NIGHTLY)
public final class Main$ ...
    public void main(final String[] args) {
        ((B)Predef$.MODULE$.identity((Object)new C())).hi();
    }
}

Note the cast (B) in Main$.main!

giabao added a commit to ohze/akka that referenced this issue Apr 5, 2020
without this fix, this spec will fail:
akka-actor-typed-tests/testOnly akka.actor.typed.TransformMessagesJavaBehaviorSpec
@odersky
Copy link
Contributor

odersky commented Apr 5, 2020

Good catch!

@smarter smarter closed this as completed in 221b6c6 Apr 5, 2020
smarter added a commit that referenced this issue Apr 5, 2020
Fix #8661: Avoid inaccessible cast targets in erasure
giabao added a commit to ohze/akka that referenced this issue Apr 6, 2020
without this fix, this spec will fail:
akka-actor-typed-tests/testOnly akka.actor.typed.TransformMessagesJavaBehaviorSpec
giabao added a commit to ohze/akka that referenced this issue Apr 7, 2020
without this fix, this spec will fail:
akka-actor-typed-tests/testOnly akka.actor.typed.TransformMessagesJavaBehaviorSpec
giabao added a commit to ohze/akka that referenced this issue Apr 7, 2020
without this fix, this spec will fail:
akka-actor-typed-tests/testOnly akka.actor.typed.TransformMessagesJavaBehaviorSpec
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