Skip to content

Commit 348d68c

Browse files
committed
Remove JDK21 specific BaseLocale cache code
We still have to use reflection because spotbugs still runs on JDK21
1 parent 9c1f913 commit 348d68c

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/WebImageFeature.java

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@
112112
import jdk.graal.compiler.graph.Node;
113113
import jdk.graal.compiler.options.OptionValues;
114114
import jdk.graal.compiler.phases.util.Providers;
115-
import jdk.graal.compiler.serviceprovider.JavaVersionUtil;
116115
import jdk.vm.ci.meta.ResolvedJavaMethod;
117116

118117
@AutomaticallyRegisteredFeature
@@ -191,45 +190,33 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
191190
* the cost of having to recreate the Locale and BaseLocale objects once when they're
192191
* requested.
193192
*
194-
* On JDK21, ReferencedKeySet and ReferencedKeyMap don't exist, so we can't reference them
195-
* directly and have to go through reflection.
193+
* On JDK21, ReferencedKeySet and ReferencedKeyMap don't exist. We have to go through
194+
* reflection to access them because analysis tools like spotbugs still run on JDK21
196195
*/
197-
if (JavaVersionUtil.JAVA_SPEC > 21) {
198-
Field baseLocaleCacheField = accessImpl.findField("sun.util.locale.BaseLocale$1InterningCache", "CACHE");
199-
Field localeCacheField = accessImpl.findField("java.util.Locale$LocaleCache", "LOCALE_CACHE");
200-
201-
access.registerFieldValueTransformer(baseLocaleCacheField, (receiver, originalValue) -> {
202-
/*
203-
* Executes `ReferencedKeySet.create(true,
204-
* ReferencedKeySet.concurrentHashMapSupplier())` with reflection.
205-
*/
206-
Class<?> referencedKeySetClazz = ReflectionUtil.lookupClass("jdk.internal.util.ReferencedKeySet");
207-
Method createMethod = ReflectionUtil.lookupMethod(referencedKeySetClazz, "create", boolean.class, Supplier.class);
208-
Method concurrentHashMapSupplierMethod = ReflectionUtil.lookupMethod(referencedKeySetClazz, "concurrentHashMapSupplier");
209-
return ReflectionUtil.invokeMethod(createMethod, null, true, ReflectionUtil.invokeMethod(concurrentHashMapSupplierMethod, null));
210-
});
211-
212-
access.registerFieldValueTransformer(localeCacheField, (receiver, originalValue) -> {
213-
/*
214-
* Executes `ReferencedKeyMap.create(true,
215-
* ReferencedKeyMap.concurrentHashMapSupplier())` with reflection.
216-
*/
217-
Class<?> referencedKeyMapClazz = ReflectionUtil.lookupClass("jdk.internal.util.ReferencedKeyMap");
218-
Method createMethod = ReflectionUtil.lookupMethod(referencedKeyMapClazz, "create", boolean.class, Supplier.class);
219-
Method concurrentHashMapSupplierMethod = ReflectionUtil.lookupMethod(referencedKeyMapClazz, "concurrentHashMapSupplier");
220-
return ReflectionUtil.invokeMethod(createMethod, null, true, ReflectionUtil.invokeMethod(concurrentHashMapSupplierMethod, null));
221-
});
222-
} else {
196+
Field baseLocaleCacheField = accessImpl.findField("sun.util.locale.BaseLocale$1InterningCache", "CACHE");
197+
Field localeCacheField = accessImpl.findField("java.util.Locale$LocaleCache", "LOCALE_CACHE");
198+
199+
access.registerFieldValueTransformer(baseLocaleCacheField, (receiver, originalValue) -> {
223200
/*
224-
* These fields contain an instance of the declaring class (Cache). It has a default
225-
* constructor that we can invoke to create an empty cache.
201+
* Executes `ReferencedKeySet.create(true,
202+
* ReferencedKeySet.concurrentHashMapSupplier())` with reflection.
226203
*/
227-
Field baseLocaleCacheField = accessImpl.findField("sun.util.locale.BaseLocale$Cache", "CACHE");
228-
Field localeCacheField = accessImpl.findField("java.util.Locale$Cache", "LOCALECACHE");
204+
Class<?> referencedKeySetClazz = ReflectionUtil.lookupClass("jdk.internal.util.ReferencedKeySet");
205+
Method createMethod = ReflectionUtil.lookupMethod(referencedKeySetClazz, "create", boolean.class, Supplier.class);
206+
Method concurrentHashMapSupplierMethod = ReflectionUtil.lookupMethod(referencedKeySetClazz, "concurrentHashMapSupplier");
207+
return ReflectionUtil.invokeMethod(createMethod, null, true, ReflectionUtil.invokeMethod(concurrentHashMapSupplierMethod, null));
208+
});
229209

230-
access.registerFieldValueTransformer(baseLocaleCacheField, (receiver, originalValue) -> ReflectionUtil.newInstance(originalValue.getClass()));
231-
access.registerFieldValueTransformer(localeCacheField, (receiver, originalValue) -> ReflectionUtil.newInstance(originalValue.getClass()));
232-
}
210+
access.registerFieldValueTransformer(localeCacheField, (receiver, originalValue) -> {
211+
/*
212+
* Executes `ReferencedKeyMap.create(true,
213+
* ReferencedKeyMap.concurrentHashMapSupplier())` with reflection.
214+
*/
215+
Class<?> referencedKeyMapClazz = ReflectionUtil.lookupClass("jdk.internal.util.ReferencedKeyMap");
216+
Method createMethod = ReflectionUtil.lookupMethod(referencedKeyMapClazz, "create", boolean.class, Supplier.class);
217+
Method concurrentHashMapSupplierMethod = ReflectionUtil.lookupMethod(referencedKeyMapClazz, "concurrentHashMapSupplier");
218+
return ReflectionUtil.invokeMethod(createMethod, null, true, ReflectionUtil.invokeMethod(concurrentHashMapSupplierMethod, null));
219+
});
233220
}
234221

235222
@Override

0 commit comments

Comments
 (0)