从 Scala 调用 Java:受保护的构造函数
在 Scala 2.8.0 Final 上编译不会出现错误:
import javax.swing.tree.TreePath
object A extends Application {
val path1 = new TreePath()
val path2 = new TreePath(path1, "foo")
}
但是,在执行时我得到:
java.lang.IllegalAccessError: tried to access method javax.swing.tree.TreePath.<init>()V from class A$
at A$.<init>(A.scala:5)
at A$.<clinit>(A.scala)
at A.main(A.scala)
这是一个错误、功能还是已知的限制?
This compiles without error on Scala 2.8.0 final:
import javax.swing.tree.TreePath
object A extends Application {
val path1 = new TreePath()
val path2 = new TreePath(path1, "foo")
}
However, on execution I get:
java.lang.IllegalAccessError: tried to access method javax.swing.tree.TreePath.<init>()V from class A$
at A$.<init>(A.scala:5)
at A$.<clinit>(A.scala)
at A.main(A.scala)
Is this a bug, feature, or a known limitation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个 bug,一个功能。您当然应该得到访问错误(功能),因为这就是 protected 应该做的。然而,编译器应该对上下文有足够的了解,以便能够告诉您并警告您,而不是将其留给运行时(错误,或至少是增强的候选者)。
This is a sort-of-bug, sort-of-feature. You certainly should get the access error (feature) because that's what protected is supposed to do. The compiler arguably ought to know enough about the context to be able to tell, however, and warn you instead of leaving it to runtime (bug, or at least candidate-for-enhancement).