Skip to content

Commit 57a04cd

Browse files
committed
Keep the ExceptionInitializerError when field not found
1 parent 73d807a commit 57a04cd

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

library/src/scala/runtime/LazyVals.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ import scala.annotation.*
1010
object LazyVals {
1111
@nowarn
1212
private[this] val unsafe: sun.misc.Unsafe = {
13-
val unsafeField = classOf[sun.misc.Unsafe].getDeclaredField("theUnsafe").nn
14-
if unsafeField.getType == classOf[sun.misc.Unsafe] then
15-
unsafeField.setAccessible(true)
16-
else throw new ExceptionInInitializerError {
17-
new IllegalStateException("Can't find instance of sun.misc.Unsafe")
18-
}
19-
unsafeField.get(null).asInstanceOf[sun.misc.Unsafe]
13+
def throwInitializationException() =
14+
throw new ExceptionInInitializerError(
15+
new IllegalStateException("Can't find instance of sun.misc.Unsafe")
16+
)
17+
try
18+
val unsafeField = classOf[sun.misc.Unsafe].getDeclaredField("theUnsafe").nn
19+
if unsafeField.getType == classOf[sun.misc.Unsafe] then
20+
unsafeField.setAccessible(true)
21+
unsafeField.get(null).asInstanceOf[sun.misc.Unsafe]
22+
else
23+
throwInitializationException()
24+
catch case _: NoSuchFieldException =>
25+
throwInitializationException()
2026
}
2127

2228
private[this] val base: Int = {

0 commit comments

Comments
 (0)