From 73d807a08d0b09eda63c6d150ea628079d06e14e Mon Sep 17 00:00:00 2001 From: Szymon Rodziewicz Date: Tue, 15 Nov 2022 13:39:53 +0100 Subject: [PATCH 1/2] Make lazy vals run on non-fallback graal image - remove dynamic reflection --- library/src/scala/runtime/LazyVals.scala | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/library/src/scala/runtime/LazyVals.scala b/library/src/scala/runtime/LazyVals.scala index 0bb78aee94ad..36a9a8e55076 100644 --- a/library/src/scala/runtime/LazyVals.scala +++ b/library/src/scala/runtime/LazyVals.scala @@ -9,19 +9,15 @@ import scala.annotation.* */ object LazyVals { @nowarn - private[this] val unsafe: sun.misc.Unsafe = - classOf[sun.misc.Unsafe].getDeclaredFields.nn.find { field => - field.nn.getType == classOf[sun.misc.Unsafe] && { - field.nn.setAccessible(true) - true - } - } - .map(_.nn.get(null).asInstanceOf[sun.misc.Unsafe]) - .getOrElse { - throw new ExceptionInInitializerError { - new IllegalStateException("Can't find instance of sun.misc.Unsafe") - } - } + private[this] val unsafe: sun.misc.Unsafe = { + val unsafeField = classOf[sun.misc.Unsafe].getDeclaredField("theUnsafe").nn + if unsafeField.getType == classOf[sun.misc.Unsafe] then + unsafeField.setAccessible(true) + else throw new ExceptionInInitializerError { + new IllegalStateException("Can't find instance of sun.misc.Unsafe") + } + unsafeField.get(null).asInstanceOf[sun.misc.Unsafe] + } private[this] val base: Int = { val processors = java.lang.Runtime.getRuntime.nn.availableProcessors() From 57a04cd0f80a2c1334d622e76374338a4d864572 Mon Sep 17 00:00:00 2001 From: Szymon Rodziewicz Date: Thu, 17 Nov 2022 15:03:43 +0100 Subject: [PATCH 2/2] Keep the ExceptionInitializerError when field not found --- library/src/scala/runtime/LazyVals.scala | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/library/src/scala/runtime/LazyVals.scala b/library/src/scala/runtime/LazyVals.scala index 36a9a8e55076..98f67e804e3c 100644 --- a/library/src/scala/runtime/LazyVals.scala +++ b/library/src/scala/runtime/LazyVals.scala @@ -10,13 +10,19 @@ import scala.annotation.* object LazyVals { @nowarn private[this] val unsafe: sun.misc.Unsafe = { - val unsafeField = classOf[sun.misc.Unsafe].getDeclaredField("theUnsafe").nn - if unsafeField.getType == classOf[sun.misc.Unsafe] then - unsafeField.setAccessible(true) - else throw new ExceptionInInitializerError { - new IllegalStateException("Can't find instance of sun.misc.Unsafe") - } - unsafeField.get(null).asInstanceOf[sun.misc.Unsafe] + def throwInitializationException() = + throw new ExceptionInInitializerError( + new IllegalStateException("Can't find instance of sun.misc.Unsafe") + ) + try + val unsafeField = classOf[sun.misc.Unsafe].getDeclaredField("theUnsafe").nn + if unsafeField.getType == classOf[sun.misc.Unsafe] then + unsafeField.setAccessible(true) + unsafeField.get(null).asInstanceOf[sun.misc.Unsafe] + else + throwInitializationException() + catch case _: NoSuchFieldException => + throwInitializationException() } private[this] val base: Int = {