java – 为什么这个Scala代码在运行时抛出IllegalAccessError?

前端之家收集整理的这篇文章主要介绍了java – 为什么这个Scala代码在运行时抛出IllegalAccessError?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个简单的应用程序:
object Test extends App {
  implicit def t2mapper[X,X0 <: X,X1 <: X](t: (X0,X1)) = new {
    def map[R](f: X => R) = (f(t._1),f(t._2))
  }
  println("Hello!")
  val (foo,bar) = (1,2) map (_ * 2)
  println((foo,bar))
}

(t2mapper是从this answer开始的.)

代码编译正常:

$scalac -version
Scala compiler version 2.9.1 -- Copyright 2002-2011,LAMP/EPFL
$scalac -unchecked Test.scala
$

但是在运行时,它会抛出IllegalAccessError(在Hello!被打印之前):

$java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.1) (6b24-1.11.1-4ubuntu3)
OpenJDK Server VM (build 20.0-b12,mixed mode)
$scala Test
java.lang.IllegalAccessError: tried to access field Test$.reflParams$Cache1 from class Test$delayedInit$body
        at Test$delayedInit$body.(Test.scala:6)
        at Test$.(Test.scala:1)
        at Test$.(Test.scala)
        at Test.main(Test.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:78)
        at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:88)
        at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:78)
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
        at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
        at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56)
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

请注意,最后两行替换为

println((1,2) map (_ * 2))

要么

val (foo,bar) = (2,4)
  println((foo,bar))

要么

val intermediate = (1,2) map (_ * 2)
  val (foo,bar) = intermediate
  println((foo,bar))

它按预期打印(2,4).但当包裹在一个街区

{
    val intermediate = (1,2) map (_ * 2)
    val (foo,bar) = intermediate
    println((foo,bar))
  }

要么

private val blah = {
    val intermediate = (1,bar))
  }

它引发了异常.

为什么第一种和最后一种方法会导致JVM在运行时抛出错误

解决方法

看起来有一些相关的开放漏洞.例如,这个可能是相关的:

https://issues.scala-lang.org/browse/SI-5251?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel

请注意,您也可以创建一个主方法而不是扩展App,它将起作用.

编辑:

当你使用这一行时(我扩展了隐含):

val (foo,bar) = t2mapper((1,2)) map (_ * 2)

然后在Eclipse中鼠标悬停foo或bar,它显示私有[this] val foo.

因此,它似乎与SI-5251非常相似.

原文链接:https://www.f2er.com/java/129256.html

猜你在找的Java相关文章