Skip to content

Commit 206e0a6

Browse files
author
Christian Wimmer
committed
Use JDK 11 classes directly instead via a Target class
1 parent 2a4ccad commit 206e0a6

File tree

9 files changed

+76
-49
lines changed

9 files changed

+76
-49
lines changed

substratevm/src/com.oracle.svm.core.jdk17/src/com/oracle/svm/core/jdk17/Target_java_lang_Module_JDK17OrLater.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import com.oracle.svm.core.jdk.ModuleUtil;
3636

3737
@SuppressWarnings("unused")
38-
@TargetClass(value = Module.class, onlyWith = JDK17OrLater.class)
38+
@TargetClass(value = java.lang.Module.class, onlyWith = JDK17OrLater.class)
3939
public final class Target_java_lang_Module_JDK17OrLater {
4040

4141
// Checkstyle: allow synchronization

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@
7070
import com.oracle.svm.core.classinitialization.EnsureClassInitializedNode;
7171
import com.oracle.svm.core.jdk.JDK17OrLater;
7272
import com.oracle.svm.core.jdk.Resources;
73-
import com.oracle.svm.core.jdk.Target_java_lang_Module;
74-
import com.oracle.svm.core.jdk.Target_jdk_internal_reflect_Reflection;
7573
import com.oracle.svm.core.meta.SharedType;
7674
import com.oracle.svm.core.util.LazyFinalReference;
7775
import com.oracle.svm.core.util.VMError;
7876
import com.oracle.svm.util.ReflectionUtil;
7977
import com.oracle.svm.util.ReflectionUtil.ReflectionUtilError;
8078

79+
import jdk.internal.reflect.ConstantPool;
80+
import jdk.internal.reflect.Reflection;
8181
import jdk.vm.ci.meta.JavaKind;
8282

8383
@Hybrid(canHybridFieldsBeDuplicated = false)
@@ -290,11 +290,9 @@ public final class DynamicHub implements JavaKind.FormatWithToString, AnnotatedE
290290
private AnnotatedSuperInfo annotatedSuperInfo;
291291

292292
/**
293-
* Field used for module information access at run-time The run time type of this field is
294-
* java.lang.Module but can be casted to {@link Target_java_lang_Module} The module is of type
295-
* Object to avoid ClassCastExceptions at image build time due to base module miss-match.
293+
* Field used for module information access at run-time.
296294
*/
297-
private Object module;
295+
private Module module;
298296

299297
/**
300298
* JDK 11 and later: the class that serves as the host for the nest. All nestmates have the same
@@ -303,7 +301,7 @@ public final class DynamicHub implements JavaKind.FormatWithToString, AnnotatedE
303301
private final Class<?> nestHost;
304302

305303
@Platforms(Platform.HOSTED_ONLY.class)
306-
public void setModule(Object module) {
304+
public void setModule(Module module) {
307305
this.module = module;
308306
}
309307

@@ -682,7 +680,7 @@ public Enum<?>[] getEnumConstantsShared() {
682680

683681
@Substitute
684682
public InputStream getResourceAsStream(String resourceName) {
685-
String moduleName = module == null ? null : SubstrateUtil.cast(module, Target_java_lang_Module.class).name;
683+
String moduleName = module == null ? null : module.getName();
686684
String resolvedName = resolveName(resourceName);
687685
return Resources.createInputStream(moduleName, resolvedName);
688686
}
@@ -1253,17 +1251,17 @@ private Constructor<?> getEnclosingConstructor() {
12531251

12541252
@Substitute
12551253
public static Class<?> forName(String className) throws ClassNotFoundException {
1256-
Class<?> caller = Target_jdk_internal_reflect_Reflection.getCallerClass();
1254+
Class<?> caller = Reflection.getCallerClass();
12571255
return forName(className, true, caller.getClassLoader());
12581256
}
12591257

12601258
@Substitute //
1261-
public static Class<?> forName(@SuppressWarnings("unused") Target_java_lang_Module module, String className) {
1259+
public static Class<?> forName(@SuppressWarnings("unused") Module module, String className) {
12621260
/*
12631261
* The module system is not supported for now, therefore the module parameter is ignored and
12641262
* we use the class loader of the caller class instead of the module's loader.
12651263
*/
1266-
Class<?> caller = Target_jdk_internal_reflect_Reflection.getCallerClass();
1264+
Class<?> caller = Reflection.getCallerClass();
12671265
try {
12681266
return forName(className, false, caller.getClassLoader());
12691267
} catch (ClassNotFoundException e) {
@@ -1351,8 +1349,8 @@ public boolean desiredAssertionStatus() {
13511349
}
13521350

13531351
@Substitute //
1354-
public Target_java_lang_Module getModule() {
1355-
return (Target_java_lang_Module) module;
1352+
public Module getModule() {
1353+
return module;
13561354
}
13571355

13581356
@Substitute //
@@ -1504,7 +1502,7 @@ private void setSigners(@SuppressWarnings("unused") Object[] signers) {
15041502
native byte[] getRawTypeAnnotations();
15051503

15061504
@Delete
1507-
native Target_jdk_internal_reflect_ConstantPool getConstantPool();
1505+
native ConstantPool getConstantPool();
15081506

15091507
@Delete
15101508
private native Field[] getDeclaredFields0(boolean publicOnly);
@@ -1582,22 +1580,22 @@ final class Target_java_lang_Class_ReflectionData<T> {
15821580
// Checkstyle: resume
15831581
}
15841582

1585-
@TargetClass(className = "jdk.internal.reflect.ReflectionFactory")
1583+
@TargetClass(value = jdk.internal.reflect.ReflectionFactory.class)
15861584
final class Target_jdk_internal_reflect_ReflectionFactory {
15871585

15881586
@Alias //
15891587
private static Target_jdk_internal_reflect_ReflectionFactory soleInstance;
15901588

1589+
/**
1590+
* This substitution eliminates the SecurityManager check in the original method, which would
1591+
* make some build-time verifications fail.
1592+
*/
15911593
@Substitute
15921594
public static Target_jdk_internal_reflect_ReflectionFactory getReflectionFactory() {
15931595
return soleInstance;
15941596
}
15951597
}
15961598

1597-
@TargetClass(className = "jdk.internal.reflect.ConstantPool")
1598-
final class Target_jdk_internal_reflect_ConstantPool {
1599-
}
1600-
16011599
@TargetClass(className = "java.lang.reflect.RecordComponent", onlyWith = JDK17OrLater.class)
16021600
final class Target_java_lang_reflect_RecordComponent {
16031601
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaLangSubstitutions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ private static Class<?> loadClassOrNull(String name) {
682682

683683
@SuppressWarnings("unused")
684684
@Substitute
685-
private static Class<?> loadClass(Target_java_lang_Module module, String name) {
685+
private static Class<?> loadClass(Module module, String name) {
686686
/* The module system is not supported for now, therefore the module parameter is ignored. */
687687
return ClassForNameSupport.forNameOrNull(name, null);
688688
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_java_lang_ClassLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ static void checkClassLoaderPermission(ClassLoader cl, Class<?> caller) {
178178

179179
@Substitute //
180180
@SuppressWarnings("unused")
181-
Class<?> loadClass(Target_java_lang_Module module, String name) {
181+
Class<?> loadClass(Module module, String name) {
182182
/* The module system is not supported for now, therefore the module parameter is ignored. */
183183
try {
184184
return loadClass(name, false);

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_java_lang_Module.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,24 @@
2727
import java.io.ByteArrayInputStream;
2828
import java.io.InputStream;
2929
import java.util.Arrays;
30-
import java.util.Map;
3130
import java.util.Objects;
3231

3332
import com.oracle.svm.core.annotate.Alias;
34-
import com.oracle.svm.core.annotate.RecomputeFieldValue;
3533
import com.oracle.svm.core.annotate.Substitute;
3634
import com.oracle.svm.core.annotate.TargetClass;
3735
import com.oracle.svm.core.annotate.TargetElement;
3836
import com.oracle.svm.core.jdk.resources.ResourceStorageEntry;
3937

4038
@SuppressWarnings("unused")
4139
@TargetClass(value = java.lang.Module.class)
42-
public final class Target_java_lang_Module {
40+
final class Target_java_lang_Module {
4341

4442
@Alias //
45-
public String name;
43+
private String name;
4644

4745
@SuppressWarnings("static-method")
4846
@Substitute
49-
public InputStream getResourceAsStream(String resourceName) {
47+
private InputStream getResourceAsStream(String resourceName) {
5048
ResourceStorageEntry res = Resources.get(name, resourceName);
5149
return res == null ? null : new ByteArrayInputStream(res.getData().get(0));
5250
}
@@ -87,17 +85,4 @@ private static void addExportsToAllUnnamed0(Module from, String pn) {
8785
ModuleUtil.checkIsPackageContainedInModule(pn, from);
8886
}
8987
}
90-
91-
@TargetClass(className = "java.lang.Module", innerClass = "ReflectionData") //
92-
private static final class Target_java_lang_Module_ReflectionData {
93-
94-
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.NewInstance, declClassName = "java.lang.WeakPairMap") //
95-
static Target_java_lang_WeakPairMap<Module, Module, Boolean> reads;
96-
97-
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.NewInstance, declClassName = "java.lang.WeakPairMap") //
98-
static Target_java_lang_WeakPairMap<Module, Module, Map<String, Boolean>> exports;
99-
100-
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.NewInstance, declClassName = "java.lang.WeakPairMap") //
101-
static Target_java_lang_WeakPairMap<Module, Class<?>, Boolean> uses;
102-
}
10388
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.jdk;
26+
27+
import java.util.Map;
28+
29+
import com.oracle.svm.core.annotate.Alias;
30+
import com.oracle.svm.core.annotate.RecomputeFieldValue;
31+
import com.oracle.svm.core.annotate.TargetClass;
32+
33+
@TargetClass(value = Module.class, innerClass = "ReflectionData") //
34+
final class Target_java_lang_Module_ReflectionData {
35+
36+
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.NewInstance, declClassName = "java.lang.WeakPairMap") //
37+
private static Target_java_lang_WeakPairMap<Module, Module, Boolean> reads;
38+
39+
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.NewInstance, declClassName = "java.lang.WeakPairMap") //
40+
private static Target_java_lang_WeakPairMap<Module, Module, Map<String, Boolean>> exports;
41+
42+
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.NewInstance, declClassName = "java.lang.WeakPairMap") //
43+
private static Target_java_lang_WeakPairMap<Module, Class<?>, Boolean> uses;
44+
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_java_lang_Package.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131

3232
@SuppressWarnings({"unused"})
3333
@TargetClass(Package.class)
34-
public final class Target_java_lang_Package {
34+
final class Target_java_lang_Package {
3535

3636
@Alias
37-
public Target_java_lang_Package(String name,
37+
Target_java_lang_Package(String name,
3838
String spectitle, String specversion, String specvendor,
3939
String impltitle, String implversion, String implvendor,
4040
URL sealbase, ClassLoader loader) {

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_reflect_Reflection.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
import com.oracle.svm.core.hub.DynamicHub;
3131
import com.oracle.svm.core.snippets.KnownIntrinsics;
3232

33-
@TargetClass(className = "jdk.internal.reflect.Reflection")
34-
public final class Target_jdk_internal_reflect_Reflection {
33+
@TargetClass(value = jdk.internal.reflect.Reflection.class)
34+
final class Target_jdk_internal_reflect_Reflection {
3535

3636
@Substitute
3737
@NeverInline("Starting a stack walk in the caller frame")
38-
public static Class<?> getCallerClass() {
38+
private static Class<?> getCallerClass() {
3939
return StackTraceUtils.getCallerClass(KnownIntrinsics.readCallerStackPointer(), true);
4040
}
4141

@@ -44,7 +44,7 @@ private static int getClassAccessFlags(Class<?> cls) {
4444
return cls.getModifiers();
4545
}
4646

47-
@Substitute //
47+
@Substitute
4848
private static boolean areNestMates(Class<?> currentClass, Class<?> memberClass) {
4949
return DynamicHub.fromClass(currentClass).isNestmateOf(memberClass);
5050
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_sun_misc_Unsafe.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
import com.oracle.svm.core.annotate.TargetClass;
3131

3232
/**
33-
* These substitutions are necessary because in JDK 11 the static initializations of these fields
34-
* are copies of the corresponding (recomputed) fields of {@link jdk.internal.misc.Unsafe}. But
35-
* copying a recomputed value during image building does not recompute the value. See GR-12640.
33+
* These substitutions are necessary because the static initializations of these fields are copies
34+
* of the corresponding (recomputed) fields of {@link jdk.internal.misc.Unsafe}. But copying a
35+
* recomputed value during image building does not recompute the value. See GR-12640.
3636
*/
3737
@TargetClass(value = sun.misc.Unsafe.class)
3838
final class Target_sun_misc_Unsafe {

0 commit comments

Comments
 (0)