From c5c1fbf9a12def0b3b5e6c6e84c36b185dfb4628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Fri, 11 Aug 2023 10:57:53 -0700 Subject: [PATCH] Access `RecordComponent` via `Class.forName`. (#2465) This should mean that GraalVM will understand the reflective lookup of its methods. See [documentation](https://www.graalvm.org/latest/reference-manual/native-image/dynamic-features/Reflection/#automatic-detection). --- .../com/google/gson/internal/reflect/ReflectionHelper.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/reflect/ReflectionHelper.java b/gson/src/main/java/com/google/gson/internal/reflect/ReflectionHelper.java index de1df3552d..0345292523 100644 --- a/gson/src/main/java/com/google/gson/internal/reflect/ReflectionHelper.java +++ b/gson/src/main/java/com/google/gson/internal/reflect/ReflectionHelper.java @@ -33,7 +33,7 @@ public class ReflectionHelper { try { // Try to construct the RecordSupportedHelper, if this fails, records are not supported on this JVM. instance = new RecordSupportedHelper(); - } catch (NoSuchMethodException e) { + } catch (ReflectiveOperationException e) { instance = new RecordNotSupportedHelper(); } RECORD_HELPER = instance; @@ -215,11 +215,10 @@ private static class RecordSupportedHelper extends RecordHelper { private final Method getName; private final Method getType; - private RecordSupportedHelper() throws NoSuchMethodException { + private RecordSupportedHelper() throws NoSuchMethodException, ClassNotFoundException { isRecord = Class.class.getMethod("isRecord"); getRecordComponents = Class.class.getMethod("getRecordComponents"); - // Class java.lang.reflect.RecordComponent - Class classRecordComponent = getRecordComponents.getReturnType().getComponentType(); + Class classRecordComponent = Class.forName("java.lang.reflect.RecordComponent"); getName = classRecordComponent.getMethod("getName"); getType = classRecordComponent.getMethod("getType"); }