-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a JRT filesystem and add the current JDK to the classpath using it.
- Loading branch information
Showing
6 changed files
with
457 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import com.intellij.core.JavaCoreProjectEnvironment; | ||
import com.intellij.openapi.util.text.StringUtil; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.util.io.URLUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
public final class ClasspathSetup { | ||
private ClasspathSetup() { | ||
} | ||
|
||
public static void addJdkModules(Path jdkHome, JavaCoreProjectEnvironment javaEnv) { | ||
var jrtFileSystem = javaEnv.getEnvironment().getJrtFileSystem(); | ||
|
||
VirtualFile jdkVfsRoot = jrtFileSystem.findFileByPath(jdkHome.toAbsolutePath() + URLUtil.JAR_SEPARATOR); | ||
if (jdkVfsRoot == null) { | ||
System.err.println("Failed to load VFS-entry for JDK home " + jdkHome + ". Is it missing?"); | ||
return; | ||
} | ||
|
||
var modulesFolder = jdkVfsRoot.findChild("modules"); | ||
if (modulesFolder == null) { | ||
System.err.println("VFS for JDK " + jdkHome + " doesn't have a modules subfolder"); | ||
return; | ||
} | ||
|
||
int moduleCount = 0; | ||
List<String> modules = readModulesFromReleaseFile(jdkHome); | ||
if (modules != null) { | ||
for (String module : modules) { | ||
var moduleRoot = modulesFolder.findChild(module); | ||
if (moduleRoot == null || !moduleRoot.isDirectory()) { | ||
System.err.println("Couldn't find module " + module + " even though it was listed in the release file of JDK " + jdkHome); | ||
} else { | ||
javaEnv.addSourcesToClasspath(moduleRoot); | ||
moduleCount++; | ||
} | ||
} | ||
} else { | ||
|
||
for (VirtualFile jrtChild : modulesFolder.getChildren()) { | ||
if (jrtChild.isDirectory()) { | ||
javaEnv.addSourcesToClasspath(jrtChild); | ||
moduleCount++; | ||
} | ||
} | ||
} | ||
|
||
System.out.println("Added " + moduleCount + " modules from " + jdkHome); | ||
} | ||
|
||
public static void addLibraries(Path librariesPath, JavaCoreProjectEnvironment javaEnv) throws IOException { | ||
var libraryFiles = Files.readAllLines(librariesPath) | ||
.stream() | ||
.filter(l -> l.startsWith("-e=")) | ||
.map(l -> l.substring(3)) | ||
.map(File::new) | ||
.toList(); | ||
|
||
for (var libraryFile : libraryFiles) { | ||
if (!libraryFile.exists()) { | ||
throw new UncheckedIOException(new FileNotFoundException(libraryFile.getAbsolutePath())); | ||
} | ||
javaEnv.addJarToClassPath(libraryFile); | ||
System.out.println("Added " + libraryFile); | ||
} | ||
} | ||
|
||
/** | ||
* Reads the "release" file found at the root of normal JDKs | ||
*/ | ||
private static @Nullable List<String> readModulesFromReleaseFile(@NotNull Path jrtBaseDir) { | ||
try (InputStream stream = Files.newInputStream(jrtBaseDir.resolve("release"))) { | ||
Properties p = new Properties(); | ||
p.load(stream); | ||
String modules = p.getProperty("MODULES"); | ||
if (modules != null) { | ||
return StringUtil.split(StringUtil.unquoteString(modules), " "); | ||
} | ||
} catch (IOException | IllegalArgumentException e) { | ||
return null; | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.