bug icon indicating copy to clipboard operation
bug copied to clipboard

REPL IllegalAccessError to Java package-protected static member

Open som-snytt opened this issue 8 years ago • 1 comments

$ scala
Welcome to Scala 2.12.4 (OpenJDK 64-Bit Server VM, Java 1.8.0_151).
Type in expressions for evaluation. Or try :help.

scala> new p.S().s
res0: String = ""

scala> :pa -raw
// Entering paste mode (ctrl-D to finish)

package p {
  class X { def x = J.j }
}

// Exiting paste mode, now interpreting.


scala> val x = new p.X
x: p.X = p.X@25a7fedf

scala> x.x
java.lang.IllegalAccessError: tried to access method p.J.j()Ljava/lang/String; from class p.X
  at p.X.x(<pastie>:2)
  ... 28 elided

scala> :quit
$ cat p/J.java

package p;

public class J {
  static String j() { return ""; }
}
$ cat p/S.scala 

package p

class S {
  def s = J.j
}

som-snytt avatar Dec 15 '17 01:12 som-snytt

IIUC still an issue

scala> new p.X().x
java.lang.IllegalAccessError: class p.X tried to access method 'java.lang.String p.J.j()' (p.X is in unnamed module of loader scala.tools.nsc.interpreter.IMain$TranslatingClassLoader @29c2c826; p.J is in unnamed module of loader scala.reflect.internal.util.ScalaClassLoader$URLClassLoader @253b380a)
  at p.X.x(<pastie>:2)
  ... 32 elided

som-snytt avatar Jan 05 '21 21:01 som-snytt

JVMS 5.4.4 says it must be

declared by a class in the same run-time package

but the pasted class has a different class loader. Splitting the package will never work.

som-snytt avatar Jun 08 '25 18:06 som-snytt