|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 2025, 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 | + |
| 26 | +package com.oracle.svm.core.jdk.runtimeinit; |
| 27 | + |
| 28 | +import java.nio.file.FileSystem; |
| 29 | +import java.nio.file.spi.FileSystemProvider; |
| 30 | + |
| 31 | +import org.graalvm.nativeimage.Platform; |
| 32 | +import org.graalvm.nativeimage.Platforms; |
| 33 | + |
| 34 | +import com.oracle.svm.core.SubstrateUtil; |
| 35 | +import com.oracle.svm.core.annotate.Alias; |
| 36 | +import com.oracle.svm.core.annotate.InjectAccessors; |
| 37 | +import com.oracle.svm.core.annotate.TargetClass; |
| 38 | +import com.oracle.svm.core.jdk.JDKInitializedAtRunTime; |
| 39 | +import com.oracle.svm.core.util.BasedOnJDKFile; |
| 40 | +import com.oracle.svm.core.util.VMError; |
| 41 | +import com.oracle.svm.util.ReflectionUtil; |
| 42 | + |
| 43 | +/** |
| 44 | + * This file contains substitutions that are required for initializing {@link FileSystemProvider} at |
| 45 | + * image run time. Other related functionality (general and build time initialization) can be found |
| 46 | + * in {@link com.oracle.svm.core.jdk.FileSystemProviderSupport}. |
| 47 | + * |
| 48 | + * @see JDKInitializedAtRunTime |
| 49 | + * @see com.oracle.svm.core.jdk.FileSystemProviderSupport |
| 50 | + */ |
| 51 | +final class FileSystemProviderRuntimeInitSupport { |
| 52 | +} |
| 53 | + |
| 54 | +// java.io |
| 55 | + |
| 56 | +@TargetClass(className = "java.io.FileSystem", onlyWith = JDKInitializedAtRunTime.class) |
| 57 | +final class Target_java_io_FileSystem_RunTime { |
| 58 | +} |
| 59 | + |
| 60 | +@TargetClass(className = "java.io.File", onlyWith = JDKInitializedAtRunTime.class) |
| 61 | +@SuppressWarnings("unused") |
| 62 | +final class Target_java_io_File_RunTime { |
| 63 | + @Alias // |
| 64 | + @InjectAccessors(DefaultFileSystemAccessor.class) // |
| 65 | + private static Target_java_io_FileSystem_RunTime FS; |
| 66 | +} |
| 67 | + |
| 68 | +@TargetClass(className = "java.io.DefaultFileSystem", onlyWith = JDKInitializedAtRunTime.class) |
| 69 | +final class Target_java_io_DefaultFileSystem_RunTime { |
| 70 | + @Alias |
| 71 | + static native Target_java_io_FileSystem_RunTime getFileSystem(); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Holds the default java.io file system. Initialized at run time via |
| 76 | + * {@code JDKInitializationFeature}. This cache is needed because |
| 77 | + * {@link Target_java_io_DefaultFileSystem_RunTime#getFileSystem()} creates a new instance for every |
| 78 | + * time. In the JDK, this method is called only once. |
| 79 | + */ |
| 80 | +@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-25+25/src/java.base/unix/classes/java/io/DefaultFileSystem.java#L39-L41") |
| 81 | +@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-25+25/src/java.base/windows/classes/java/io/DefaultFileSystem.java#L39-L41") |
| 82 | +class DefaultFileSystemHolder { |
| 83 | + static final Object FS; |
| 84 | + static { |
| 85 | + if (SubstrateUtil.HOSTED) { |
| 86 | + /* |
| 87 | + * Should be unused, but layered images might want to initialize it during image build. |
| 88 | + * We set it to the real default file system instead of null to guard against |
| 89 | + * unintentional usages. In run-time init mode, we don't allow FileSystems in the image |
| 90 | + * heap, so we would fail the image build with an exception if it happens, which helps |
| 91 | + * to detect problems. |
| 92 | + */ |
| 93 | + var defaultFileSystem = ReflectionUtil.lookupClass("java.io.DefaultFileSystem"); |
| 94 | + var getFileSystem = ReflectionUtil.lookupMethod(defaultFileSystem, "getFileSystem"); |
| 95 | + FS = ReflectionUtil.invokeMethod(getFileSystem, null); |
| 96 | + } else { |
| 97 | + FS = Target_java_io_DefaultFileSystem_RunTime.getFileSystem(); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +class DefaultFileSystemAccessor { |
| 103 | + @SuppressWarnings("unused") |
| 104 | + static Target_java_io_FileSystem_RunTime get() { |
| 105 | + VMError.guarantee(DefaultFileSystemHolder.FS != null, "DefaultFileSystemHolder.FS is null"); |
| 106 | + return SubstrateUtil.cast(DefaultFileSystemHolder.FS, Target_java_io_FileSystem_RunTime.class); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// sun.nio.fs |
| 111 | + |
| 112 | +@TargetClass(className = "sun.nio.fs.DefaultFileSystemProvider", onlyWith = JDKInitializedAtRunTime.class) |
| 113 | +final class Target_sun_nio_fs_DefaultFileSystemProvider_RunTime { |
| 114 | + @Alias |
| 115 | + static native FileSystem theFileSystem(); |
| 116 | +} |
| 117 | + |
| 118 | +@TargetClass(className = "sun.nio.fs.UnixFileSystem", onlyWith = JDKInitializedAtRunTime.class) |
| 119 | +@Platforms({Platform.LINUX.class, Platform.DARWIN.class}) |
| 120 | +final class Target_sun_nio_fs_UnixFileSystem_RunTime { |
| 121 | +} |
| 122 | + |
| 123 | +@TargetClass(className = "sun.nio.fs.UnixPath", onlyWith = JDKInitializedAtRunTime.class) |
| 124 | +@Platforms({Platform.LINUX.class, Platform.DARWIN.class}) |
| 125 | +final class Target_sun_nio_fs_UnixPath_RunTime { |
| 126 | + @Alias // |
| 127 | + @InjectAccessors(UnixFileSystemAccessor.class) // |
| 128 | + private Target_sun_nio_fs_UnixFileSystem_RunTime fs; |
| 129 | +} |
| 130 | + |
| 131 | +@SuppressWarnings("unused") |
| 132 | +class UnixFileSystemAccessor { |
| 133 | + static Target_sun_nio_fs_UnixFileSystem_RunTime get(Target_sun_nio_fs_UnixPath_RunTime that) { |
| 134 | + FileSystem theFileSystem = Target_sun_nio_fs_DefaultFileSystemProvider_RunTime.theFileSystem(); |
| 135 | + VMError.guarantee(theFileSystem != null, "DefaultFileSystemProvider.theFileSystem() is null"); |
| 136 | + return SubstrateUtil.cast(theFileSystem, Target_sun_nio_fs_UnixFileSystem_RunTime.class); |
| 137 | + } |
| 138 | + |
| 139 | + static void set(Target_sun_nio_fs_UnixPath_RunTime that, Target_sun_nio_fs_UnixFileSystem_RunTime value) { |
| 140 | + /* |
| 141 | + * `value` should always be DefaultFileSystemProvider.INSTANCE.theFileSystem() but we cannot |
| 142 | + * check that here because it would introduce a class initialization cycle. |
| 143 | + */ |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +@TargetClass(className = "sun.nio.fs.WindowsFileSystem", onlyWith = JDKInitializedAtRunTime.class) |
| 148 | +@Platforms(Platform.WINDOWS.class) |
| 149 | +final class Target_sun_nio_fs_WindowsFileSystem_RunTime { |
| 150 | +} |
| 151 | + |
| 152 | +@TargetClass(className = "sun.nio.fs.WindowsPath", onlyWith = JDKInitializedAtRunTime.class) |
| 153 | +@Platforms(Platform.WINDOWS.class) |
| 154 | +final class Target_sun_nio_fs_WindowsPath_RunTime { |
| 155 | + @Alias // |
| 156 | + @InjectAccessors(WindowsFileSystemAccessor.class) // |
| 157 | + private Target_sun_nio_fs_WindowsFileSystem_RunTime fs; |
| 158 | +} |
| 159 | + |
| 160 | +@SuppressWarnings("unused") |
| 161 | +class WindowsFileSystemAccessor { |
| 162 | + static Target_sun_nio_fs_WindowsFileSystem_RunTime get(Target_sun_nio_fs_WindowsPath_RunTime that) { |
| 163 | + FileSystem theFileSystem = Target_sun_nio_fs_DefaultFileSystemProvider_RunTime.theFileSystem(); |
| 164 | + VMError.guarantee(theFileSystem != null, "DefaultFileSystemProvider.theFileSystem() is null"); |
| 165 | + return SubstrateUtil.cast(theFileSystem, Target_sun_nio_fs_WindowsFileSystem_RunTime.class); |
| 166 | + } |
| 167 | + |
| 168 | + static void set(Target_sun_nio_fs_WindowsPath_RunTime that, Target_sun_nio_fs_WindowsFileSystem_RunTime value) { |
| 169 | + /* |
| 170 | + * `value` should always be DefaultFileSystemProvider.INSTANCE.theFileSystem() but we cannot |
| 171 | + * check that here because it would introduce a class initialization cycle. |
| 172 | + */ |
| 173 | + } |
| 174 | +} |
0 commit comments