Skip to content

Commit bf83641

Browse files
authored
Make lazy vals run on non-fallback graal image - remove dynamic reflection (#16346)
Fix #13985 I tested it and it seems to fix the issue
2 parents 7813558 + 57a04cd commit bf83641

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

library/src/scala/runtime/LazyVals.scala

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@ import scala.annotation.*
99
*/
1010
object LazyVals {
1111
@nowarn
12-
private[this] val unsafe: sun.misc.Unsafe =
13-
classOf[sun.misc.Unsafe].getDeclaredFields.nn.find { field =>
14-
field.nn.getType == classOf[sun.misc.Unsafe] && {
15-
field.nn.setAccessible(true)
16-
true
17-
}
18-
}
19-
.map(_.nn.get(null).asInstanceOf[sun.misc.Unsafe])
20-
.getOrElse {
21-
throw new ExceptionInInitializerError {
22-
new IllegalStateException("Can't find instance of sun.misc.Unsafe")
23-
}
24-
}
12+
private[this] val unsafe: 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()
26+
}
2527

2628
private[this] val base: Int = {
2729
val processors = java.lang.Runtime.getRuntime.nn.availableProcessors()

0 commit comments

Comments
 (0)