diff --git a/host/src/main/java/com/volmit/host/ModuleJarCheck.java b/host/src/main/java/com/volmit/host/ModuleJarCheck.java
index 03f14a155..651b9ad4e 100644
--- a/host/src/main/java/com/volmit/host/ModuleJarCheck.java
+++ b/host/src/main/java/com/volmit/host/ModuleJarCheck.java
@@ -3,6 +3,9 @@ package com.volmit.host;
import com.volmit.IrisModule;
import java.io.IOException;
import java.io.UncheckedIOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
@@ -24,16 +27,25 @@ import java.util.Optional;
*
The class path the check runs on may hold classes of an exposed library that the real host's jar
* leaves out. {@code --left-out=} (or the {@code leftOutClasses} parameter) lists them, one entry per
* line as described by {@code HostClassesLeftOut}, and a module that references one fails the check naming it.
+ *
+ * The jar's tooling parts ({@link ToolingPart}) are not generation code. Generation code may not use them, which is
+ * always checked. The parts themselves are checked only for the platform {@code --platform=:} names, such as {@code bukkit} with paper-api, against that jar and what a part may see; without the option,
+ * or for other platforms, parts are skipped and the output lists them.
*/
public final class ModuleJarCheck {
private static final int FAILED_EXIT_CODE = 1;
private static final int MISSING_ARGUMENT_EXIT_CODE = 2;
private static final String LEFT_OUT_OPTION = "--left-out=";
+ private static final String PLATFORM_OPTION = "--platform=";
+ private static final char PLATFORM_OPTION_SEPARATOR = ':';
+ private static final String USAGE = "Usage: ModuleJarCheck [--left-out=] [--platform=:]"
+ + " [exposed host package...]";
private ModuleJarCheck() {}
/** Returns the module classes the jar names, in the order it lists them, or throws a {@link ModuleLoadException}
- * naming the jar.
+ * naming the jar. The jar's tooling parts are skipped.
*/
public static List check(Path jar, Collection exposedHostPackages) {
return check(jar, exposedHostPackages, List.of());
@@ -41,13 +53,86 @@ public final class ModuleJarCheck {
/** Like {@link #check(Path, Collection)}, treating the classes {@code leftOutClasses} describes as missing from the host. */
public static List check(Path jar, Collection exposedHostPackages, Collection leftOutClasses) {
+ return run(jar, exposedHostPackages, leftOutClasses, Optional.empty()).modules();
+ }
+
+ /** Like {@link #check(Path, Collection, Collection)}, also checking the jar's tooling parts of {@code platform} against
+ * {@code platformApiJar}, the jar that holds the platform's api classes, such as paper-api, and what a part may see.
+ * Parts of other platforms are skipped.
+ */
+ public static List check(Path jar, Collection exposedHostPackages, Collection leftOutClasses,
+ ToolingPlatform platform, Path platformApiJar) {
+ Objects.requireNonNull(platform, "platform");
+ return run(jar, exposedHostPackages, leftOutClasses, Optional.of(new CheckedPlatform(platform, platformApiJar))).modules();
+ }
+
+ /** The platform whose parts a check reads, with the jar of its api classes. */
+ record CheckedPlatform(ToolingPlatform platform, Path apiJar) {
+ CheckedPlatform {
+ Objects.requireNonNull(platform, "platform");
+ Objects.requireNonNull(apiJar, "apiJar");
+ }
+
+ /** Reads {@code :}. */
+ static CheckedPlatform parse(String option) {
+ int separator = option.indexOf(PLATFORM_OPTION_SEPARATOR);
+ if (separator <= 0 || separator == option.length() - 1) {
+ throw new IllegalArgumentException("'" + PLATFORM_OPTION + option + "' is not " + PLATFORM_OPTION
+ + ":, such as " + PLATFORM_OPTION + "bukkit:libs/paper-api.jar");
+ }
+ return new CheckedPlatform(ToolingPlatform.named(option.substring(0, separator)), Path.of(option.substring(separator + 1)));
+ }
+ }
+
+ /** What a check found: the module classes, and the tooling parts it did not read. */
+ record Outcome(Path jar, List modules, List skippedParts, boolean checkedAPlatform) {
+ String summary() {
+ var passed = "Module jar " + jar + " passes the pack module checks: " + modules;
+ if (skippedParts.isEmpty()) return passed;
+ var reason = checkedAPlatform ? "of other platforms" : "(name a platform with " + PLATFORM_OPTION + ": to check them)";
+ return passed + "\nSkipped tooling parts " + reason + ": " + skippedParts.stream().map(ToolingPart::declaration).toList();
+ }
+ }
+
+ static Outcome run(Path jar, Collection exposedHostPackages, Collection leftOutClasses, Optional platform) {
Objects.requireNonNull(jar, "jar");
var host = new ExposedHostClassLoader(IrisModule.class.getClassLoader(), ExposedPackages.apiAnd(exposedHostPackages),
HostClassesLeftOut.parse(leftOutClasses));
var read = ModuleJar.read(new JarSnapshot.CopiedJar(jar.toString(), jar));
requireEntryClassesInJar(read);
- PackClassCheck.requireLinkable(List.of(read), host, Optional.empty());
- return read.entryClasses();
+ requireLinkable(read, host, platform);
+ return new Outcome(jar, read.entryClasses(), skippedParts(read, platform), platform.isPresent());
+ }
+
+ private static void requireLinkable(ModuleJar read, ExposedHostClassLoader host, Optional platform) {
+ if (platform.isEmpty()) {
+ PackClassCheck.requireLinkable(List.of(read), host, Optional.empty());
+ return;
+ }
+ var apiJar = requireReadable(platform.get().apiJar());
+ try (var platformClasses = new URLClassLoader("platform-api", new URL[] {urlOf(apiJar)}, null)) {
+ PackClassCheck.requireLinkable(List.of(read), host, Optional.of(new PlatformApi(platform.get().platform(), platformClasses)));
+ } catch (IOException exception) {
+ throw new UncheckedIOException("Cannot close the platform api jar " + apiJar, exception);
+ }
+ }
+
+ private static List skippedParts(ModuleJar read, Optional platform) {
+ var checkedName = platform.map(checked -> checked.platform().name());
+ return read.toolingParts().stream().filter(part -> checkedName.filter(part.platform()::equals).isEmpty()).toList();
+ }
+
+ private static Path requireReadable(Path apiJar) {
+ if (Files.isRegularFile(apiJar)) return apiJar;
+ throw new IllegalArgumentException("Cannot read the platform api jar " + apiJar + "; name the jar of the platform's api, such as paper-api");
+ }
+
+ private static URL urlOf(Path jar) {
+ try {
+ return jar.toUri().toURL();
+ } catch (MalformedURLException exception) {
+ throw new IllegalArgumentException("The platform api jar " + jar + " has no URL form", exception);
+ }
}
private static void requireEntryClassesInJar(ModuleJar jar) {
@@ -60,22 +145,29 @@ public final class ModuleJarCheck {
}
public static void main(String[] arguments) {
- var options = Arrays.stream(arguments).filter(argument -> argument.startsWith(LEFT_OUT_OPTION)).toList();
- var positional = Arrays.stream(arguments).filter(argument -> !argument.startsWith(LEFT_OUT_OPTION)).toList();
- if (positional.isEmpty()) {
- System.err.println("Usage: ModuleJarCheck [--left-out=] [exposed host package...]");
+ var leftOutOptions = optionValues(arguments, LEFT_OUT_OPTION);
+ var platformOptions = optionValues(arguments, PLATFORM_OPTION);
+ var positional = Arrays.stream(arguments)
+ .filter(argument -> !argument.startsWith(LEFT_OUT_OPTION) && !argument.startsWith(PLATFORM_OPTION))
+ .toList();
+ if (positional.isEmpty() || platformOptions.size() > 1) {
+ System.err.println(USAGE);
System.exit(MISSING_ARGUMENT_EXIT_CODE);
}
try {
- var leftOut = options.stream().flatMap(option -> readLines(Path.of(option.substring(LEFT_OUT_OPTION.length()))).stream()).toList();
- var modules = check(Path.of(positional.getFirst()), positional.subList(1, positional.size()), leftOut);
- System.out.println("Module jar " + positional.getFirst() + " passes the pack module checks: " + modules);
+ var leftOut = leftOutOptions.stream().flatMap(option -> readLines(Path.of(option)).stream()).toList();
+ var platform = platformOptions.stream().map(CheckedPlatform::parse).findFirst();
+ System.out.println(run(Path.of(positional.getFirst()), positional.subList(1, positional.size()), leftOut, platform).summary());
} catch (RuntimeException failure) {
System.err.println(failure.getMessage());
System.exit(FAILED_EXIT_CODE);
}
}
+ private static List optionValues(String[] arguments, String option) {
+ return Arrays.stream(arguments).filter(argument -> argument.startsWith(option)).map(argument -> argument.substring(option.length())).toList();
+ }
+
private static List readLines(Path file) {
try {
return Files.readAllLines(file);
diff --git a/host/src/test/java/com/volmit/host/ModuleJarCheckTest.java b/host/src/test/java/com/volmit/host/ModuleJarCheckTest.java
index f1f6a43ec..196dc0f30 100644
--- a/host/src/test/java/com/volmit/host/ModuleJarCheckTest.java
+++ b/host/src/test/java/com/volmit/host/ModuleJarCheckTest.java
@@ -1,12 +1,16 @@
package com.volmit.host;
+import com.volmit.support.FakePlatformApi;
import com.volmit.support.TestModuleJar;
import example.builtin.CarveModule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import java.nio.file.Path;
import java.util.List;
+import java.util.Optional;
import static com.volmit.support.TestModuleJar.module;
import static org.junit.jupiter.api.Assertions.*;
@@ -100,6 +104,149 @@ class ModuleJarCheckTest {
assertTrue(message.contains("pack.User -> example.builtin.CarveModule"), message);
}
+ private static final String BUKKIT_PART = "pack.tools.bukkit.StudioPart";
+ private static final String MODDED_PART = "pack.tools.modded.ModdedPart";
+
+ @TempDir
+ Path work;
+
+ private Path platformJar(String... classNames) {
+ try (var platform = FakePlatformApi.of(work, classNames)) {
+ return platform.jar();
+ }
+ }
+
+ private static String partWith(String className, String members) {
+ int dot = className.lastIndexOf('.');
+ return "package " + className.substring(0, dot) + "; public final class " + className.substring(dot + 1) + " { " + members + " }";
+ }
+
+ private static String usingClass(String className) {
+ return "static final Class> USED = " + className + ".class;";
+ }
+
+ private static TestModuleJar moduleWithPart(String part, String partMembers, String platform) {
+ return module("pack.User", "user:core", NO_MEMBERS, NO_STAGES)
+ .source(part, partWith(part, partMembers))
+ .toolingParts(platform + "=" + part);
+ }
+
+ private static ModuleJarCheck.CheckedPlatform bukkitWith(Path apiJar) {
+ return new ModuleJarCheck.CheckedPlatform(ToolingPlatform.BUKKIT, apiJar);
+ }
+
+ @Test
+ void partsAreSkippedWithoutAPlatformAndTheOutputSaysSo() {
+ var jar = moduleWithPart(BUKKIT_PART, usingClass("org.junit.jupiter.api.Test"), "bukkit")
+ .compileAgainstHostOf(Test.class)
+ .writeInto(build, "user.jar");
+
+ var outcome = ModuleJarCheck.run(jar, NO_EXPOSED_PACKAGES, List.of(), Optional.empty());
+
+ assertEquals(List.of("pack.User"), outcome.modules());
+ assertTrue(outcome.summary().contains("passes the pack module checks: [pack.User]"), outcome.summary());
+ assertTrue(outcome.summary().contains("Skipped tooling parts (name a platform with --platform=: to check them): "
+ + "[bukkit=" + BUKKIT_PART + "]"), outcome.summary());
+ }
+
+ @Test
+ void jarWithoutPartsHasNoSkipNote() {
+ var jar = module("pack.User", "user:core", NO_MEMBERS, NO_STAGES).writeInto(build, "user.jar");
+
+ assertFalse(ModuleJarCheck.run(jar, NO_EXPOSED_PACKAGES, List.of(), Optional.empty()).summary().contains("Skipped"));
+ }
+
+ @Test
+ void generationClassUsingAPartFailsWithoutAPlatformToo() {
+ var jar = module("pack.User", "user:core", "static Object leak() { return new " + BUKKIT_PART + "(); }", NO_STAGES)
+ .source(BUKKIT_PART, partWith(BUKKIT_PART, "public StudioPart() {}"))
+ .toolingParts("bukkit=" + BUKKIT_PART)
+ .writeInto(build, "user.jar");
+
+ var message = failure(() -> ModuleJarCheck.check(jar, NO_EXPOSED_PACKAGES));
+
+ assertTrue(message.contains("pack.User -> " + BUKKIT_PART), message);
+ assertTrue(message.contains("Generation code cannot use the bukkit part; move the call into the part."), message);
+ }
+
+ @Test
+ void partOfTheNamedPlatformPassesAgainstThePlatformJar() {
+ var api = platformJar("org.bukkit.Bukkit");
+ var jar = moduleWithPart(BUKKIT_PART, usingClass("org.bukkit.Bukkit"), "bukkit")
+ .compileAgainst(api)
+ .writeInto(build, "user.jar");
+
+ var outcome = ModuleJarCheck.run(jar, NO_EXPOSED_PACKAGES, List.of(), Optional.of(bukkitWith(api)));
+
+ assertEquals(List.of("pack.User"), outcome.modules());
+ assertEquals(List.of(), outcome.skippedParts());
+ assertFalse(outcome.summary().contains("Skipped"), outcome.summary());
+ assertEquals(List.of("pack.User"), ModuleJarCheck.check(jar, NO_EXPOSED_PACKAGES, List.of(), ToolingPlatform.BUKKIT, api));
+ }
+
+ @Test
+ void partUsingAClassThePlatformJarLacksFailsNamingBothClasses() {
+ var buildApi = platformJar("org.bukkit.Bukkit", "org.bukkit.entity.Player");
+ var checkedApi = platformJar("org.bukkit.Bukkit");
+ var jar = moduleWithPart(BUKKIT_PART, usingClass("org.bukkit.entity.Player"), "bukkit")
+ .compileAgainst(buildApi)
+ .writeInto(build, "user.jar");
+
+ var message = failure(() -> ModuleJarCheck.check(jar, NO_EXPOSED_PACKAGES, List.of(), ToolingPlatform.BUKKIT, checkedApi));
+
+ assertTrue(message.contains(BUKKIT_PART + " -> org.bukkit.entity.Player"), message);
+ }
+
+ @Test
+ void partUsingADeniedClassFailsNamingTheJarAndBothClasses() {
+ var api = platformJar("org.bukkit.craftbukkit.CraftServer");
+ var jar = moduleWithPart(BUKKIT_PART, usingClass("org.bukkit.craftbukkit.CraftServer"), "bukkit")
+ .compileAgainst(api)
+ .writeInto(build, "user.jar");
+
+ var message = failure(() -> ModuleJarCheck.check(jar, NO_EXPOSED_PACKAGES, List.of(), ToolingPlatform.BUKKIT, api));
+
+ assertTrue(message.contains(jar.toString()), message);
+ assertTrue(message.contains(BUKKIT_PART + " -> org.bukkit.craftbukkit.CraftServer"), message);
+ }
+
+ @Test
+ void partOfAnotherPlatformIsSkippedAndListed() {
+ var api = platformJar("org.bukkit.Bukkit");
+ var jar = moduleWithPart(MODDED_PART, usingClass("org.junit.jupiter.api.Test"), "modded")
+ .compileAgainstHostOf(Test.class)
+ .writeInto(build, "user.jar");
+
+ var outcome = ModuleJarCheck.run(jar, NO_EXPOSED_PACKAGES, List.of(), Optional.of(bukkitWith(api)));
+
+ assertTrue(outcome.summary().contains("Skipped tooling parts of other platforms: [modded=" + MODDED_PART + "]"), outcome.summary());
+ }
+
+ @Test
+ void missingPlatformJarFailsNamingIt() {
+ var jar = module("pack.User", "user:core", NO_MEMBERS, NO_STAGES).writeInto(build, "user.jar");
+ var missing = work.resolve("missing-api.jar");
+
+ var failure = assertThrows(IllegalArgumentException.class,
+ () -> ModuleJarCheck.check(jar, NO_EXPOSED_PACKAGES, List.of(), ToolingPlatform.BUKKIT, missing));
+
+ assertTrue(failure.getMessage().contains(missing.toString()), failure.getMessage());
+ }
+
+ @Test
+ void platformOptionNamesAKnownPlatformAndAJar() {
+ var parsed = ModuleJarCheck.CheckedPlatform.parse("bukkit:libs/paper-api.jar");
+
+ assertSame(ToolingPlatform.BUKKIT, parsed.platform());
+ assertEquals(Path.of("libs/paper-api.jar"), parsed.apiJar());
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"bukkit", "bukkit:", ":paper-api.jar", "modded:paper-api.jar"})
+ void malformedPlatformOptionFails(String option) {
+ assertThrows(IllegalArgumentException.class, () -> ModuleJarCheck.CheckedPlatform.parse(option));
+ }
+
@Test
void jarThatUsesAnExposedClassNotLeftOutStillPasses() {
var jar = module("pack.User", "user:core", NO_MEMBERS,