From adcecfa1d65014ff06068d194ad6eac9f837de9a Mon Sep 17 00:00:00 2001 From: Loic Ottet Date: Thu, 7 Dec 2023 16:29:54 +0100 Subject: [PATCH] Fix failure when no resource bundle nullary constructor is present (cherry picked from commit ad3843ebd2d61068fb8542cf94038f41efb8e7d9) --- .../jdk/localization/LocalizationSupport.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/localization/LocalizationSupport.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/localization/LocalizationSupport.java index 6fdc8e5621e7..82b03c200a12 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/localization/LocalizationSupport.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/localization/LocalizationSupport.java @@ -25,6 +25,7 @@ package com.oracle.svm.core.jdk.localization; +import java.lang.reflect.Constructor; import java.nio.charset.Charset; import java.util.Collection; import java.util.HashMap; @@ -209,8 +210,25 @@ public static Locale parseLocaleFromTag(String tag) { } public void prepareClassResourceBundle(@SuppressWarnings("unused") String basename, Class bundleClass) { - RuntimeReflection.register(bundleClass); - RuntimeReflection.registerForReflectiveInstantiation(bundleClass); + registerNullaryConstructor(bundleClass); onClassBundlePrepared(bundleClass); } + + /** + * Bundle lookup code tries to reflectively access the default constructor of candidate bundle + * classes, and then tries to invoke them if they exist. We therefore need to register the + * default constructor as invoked if it exists, and as queried if it doesn't, which we know will + * result in a negative query. + */ + private static void registerNullaryConstructor(Class bundleClass) { + RuntimeReflection.register(bundleClass); + Constructor nullaryConstructor; + try { + nullaryConstructor = bundleClass.getDeclaredConstructor(); + } catch (NoSuchMethodException e) { + RuntimeReflection.registerConstructorLookup(bundleClass); + return; + } + RuntimeReflection.register(nullaryConstructor); + } }