diff --git a/host/src/main/java/com/volmit/host/ModuleJar.java b/host/src/main/java/com/volmit/host/ModuleJar.java
index 5210558f1..706b8c40b 100644
--- a/host/src/main/java/com/volmit/host/ModuleJar.java
+++ b/host/src/main/java/com/volmit/host/ModuleJar.java
@@ -16,6 +16,7 @@ import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
+import java.util.Set;
import java.util.SortedSet;
import java.util.function.Predicate;
import java.util.jar.JarEntry;
@@ -36,9 +37,11 @@ import java.util.zip.ZipFile;
*
*
The optional packages are those the jar's manifest lists under {@value #OPTIONAL_PACKAGES_ATTRIBUTE}, separated
* by spaces or commas: packages, with their subpackages, whose classes the jar uses but expects may be absent.
+ *
+ *
The tooling parts are those the manifest lists under {@value ToolingPart#MANIFEST_ATTRIBUTE}, see {@link ToolingPart}.
*/
record ModuleJar(Path path, String origin, List entryClasses, List classes,
- List optionalPackages) {
+ List optionalPackages, List toolingParts) {
static final String ENTRY_POINT_FILE = "META-INF/services/" + IrisModule.class.getName();
static final String OPTIONAL_PACKAGES_ATTRIBUTE = "Iris-Optional-Packages";
private static final String OPTIONAL_PACKAGE_SEPARATORS = "[\\s,]+";
@@ -53,6 +56,7 @@ record ModuleJar(Path path, String origin, List entryClasses, List entryClasses, List {
+public record PackageName(String name) implements Comparable {
private static final char SEPARATOR = '.';
private static final String SEPARATOR_PATTERN = "\\.";
diff --git a/host/src/main/java/com/volmit/host/ToolingPart.java b/host/src/main/java/com/volmit/host/ToolingPart.java
new file mode 100644
index 000000000..557def90f
--- /dev/null
+++ b/host/src/main/java/com/volmit/host/ToolingPart.java
@@ -0,0 +1,107 @@
+package com.volmit.host;
+
+import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+import java.util.jar.Manifest;
+import java.util.regex.Pattern;
+
+/** One entry of a jar's {@value #MANIFEST_ATTRIBUTE} attribute: the tooling part of one platform that starts at
+ * {@code entryClass}. The part is that class's package with its subpackages. Its classes are loaded by the platform's
+ * own loader ({@link ToolingPartLoader}), never by the pack's, so they may use the platform; generation code may not
+ * use them.
+ *
+ * The attribute lists {@code =} entries separated by spaces; a platform may appear more than
+ * once. Entries of one platform may share a package. Otherwise parts must not overlap: two entries in one package, or
+ * in nested packages, of different platforms or of nested packages would put a class in two parts.
+ *
+ * @param jar the load's copy of the jar that holds the part, see {@link JarSnapshot}
+ * @param jarOrigin the original jar's name as messages show it
+ */
+public record ToolingPart(String platform, String entryClass, PackageName packageName, String jarOrigin, Path jar) {
+ static final String MANIFEST_ATTRIBUTE = "Iris-Tooling-Parts";
+ private static final Pattern PLATFORM_NAME = Pattern.compile("[a-z][a-z0-9]*");
+ private static final String ENTRY_SEPARATORS = "\\s+";
+ private static final char PLATFORM_SEPARATOR = '=';
+ private static final char PACKAGE_SEPARATOR = '.';
+ private static final String ENTRY_FORMAT_HINT = "write it like bukkit=com.example.tools.StudioPart, a platform name and"
+ + " an entry class that is in a package";
+
+ static boolean isPlatformName(String name) {
+ return PLATFORM_NAME.matcher(name).matches();
+ }
+
+ /** The parts a jar's manifest declares, in the order it lists them. Fails on an entry that is not a platform and a
+ * class in a package, and on parts that share classes.
+ */
+ static List readAll(String origin, Path jar, Manifest manifest) {
+ var declared = manifest == null ? null : manifest.getMainAttributes().getValue(MANIFEST_ATTRIBUTE);
+ if (declared == null || declared.isBlank()) return List.of();
+ var parts = Arrays.stream(declared.trim().split(ENTRY_SEPARATORS))
+ .distinct()
+ .map(entry -> parse(origin, jar, entry))
+ .toList();
+ requireNoSharedClasses(origin, parts);
+ return parts;
+ }
+
+ /** Fails when a part's entry class is not among {@code classNames}, the classes of its jar. */
+ static void requireEntryClassesIn(String origin, List parts, Set classNames) {
+ for (var part : parts) {
+ if (classNames.contains(part.entryClass)) continue;
+ throw ModuleLoadException.ofJar(origin, "Module jar " + origin + " declares tooling part " + part.declaration()
+ + " in " + MANIFEST_ATTRIBUTE + ", but the jar holds no class " + part.entryClass);
+ }
+ }
+
+ /** Whether the class is in this part's package or one of its subpackages. */
+ boolean contains(String binaryName) {
+ return packageName.contains(binaryName);
+ }
+
+ String declaration() {
+ return platform + PLATFORM_SEPARATOR + entryClass;
+ }
+
+ private static ToolingPart parse(String origin, Path jar, String entry) {
+ int separator = entry.indexOf(PLATFORM_SEPARATOR);
+ var platform = separator < 0 ? "" : entry.substring(0, separator);
+ var entryClass = separator < 0 ? "" : entry.substring(separator + 1);
+ int classSeparator = entryClass.lastIndexOf(PACKAGE_SEPARATOR);
+ boolean wellFormed = isPlatformName(platform) && classSeparator > 0 && !entryClass.endsWith(String.valueOf(PACKAGE_SEPARATOR))
+ && PackageName.tryParse(entryClass).isPresent();
+ if (!wellFormed) throw malformedEntry(origin, entry);
+ return new ToolingPart(platform, entryClass, new PackageName(entryClass.substring(0, classSeparator)), origin, jar);
+ }
+
+ private static ModuleLoadException malformedEntry(String origin, String entry) {
+ return ModuleLoadException.ofJar(origin, "Module jar " + origin + " declares tooling part '" + entry + "' in "
+ + MANIFEST_ATTRIBUTE + ", which is malformed; " + ENTRY_FORMAT_HINT);
+ }
+
+ private static void requireNoSharedClasses(String origin, List parts) {
+ for (int first = 0; first < parts.size(); first++) {
+ for (int second = first + 1; second < parts.size(); second++) {
+ if (parts.get(first).sharesClassesWithoutBeingOnePart(parts.get(second))) {
+ throw overlappingParts(origin, parts.get(first), parts.get(second));
+ }
+ }
+ }
+ }
+
+ private boolean sharesClassesWithoutBeingOnePart(ToolingPart other) {
+ boolean packagesOverlap = contains(other.entryClass) || other.contains(entryClass);
+ return packagesOverlap && !samePackageOfSamePlatform(other);
+ }
+
+ private boolean samePackageOfSamePlatform(ToolingPart other) {
+ return platform.equals(other.platform) && packageName.equals(other.packageName);
+ }
+
+ private static ModuleLoadException overlappingParts(String origin, ToolingPart first, ToolingPart second) {
+ return ModuleLoadException.ofJar(origin, "Module jar " + origin + " declares overlapping tooling parts "
+ + first.declaration() + " and " + second.declaration() + " in " + MANIFEST_ATTRIBUTE + "; a part is its entry"
+ + " class's package with its subpackages, and no class may belong to two parts");
+ }
+}
diff --git a/host/src/test/java/com/volmit/host/ToolingPartTest.java b/host/src/test/java/com/volmit/host/ToolingPartTest.java
new file mode 100644
index 000000000..cd3eedded
--- /dev/null
+++ b/host/src/test/java/com/volmit/host/ToolingPartTest.java
@@ -0,0 +1,107 @@
+package com.volmit.host;
+
+import com.volmit.support.TestModuleJar;
+import org.junit.jupiter.api.Test;
+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 static com.volmit.support.TestModuleJar.module;
+import static org.junit.jupiter.api.Assertions.*;
+
+class ToolingPartTest {
+ private static final String ORIGIN = "modules/user.jar";
+
+ @TempDir
+ Path pack;
+
+ private static TestModuleJar jarWithPartClasses(String... classNames) {
+ var jar = module("pack.User", "user:core", "", "");
+ for (var className : classNames) {
+ int dot = className.lastIndexOf('.');
+ jar.source(className, "package " + className.substring(0, dot) + "; public final class " + className.substring(dot + 1) + " {}");
+ }
+ return jar;
+ }
+
+ private ModuleJar read(TestModuleJar jar) {
+ return ModuleJar.read(new JarSnapshot.CopiedJar(ORIGIN, jar.writeInto(pack, "user.jar")));
+ }
+
+ private String failureOf(TestModuleJar jar) {
+ return assertThrows(ModuleLoadException.class, () -> read(jar)).getMessage();
+ }
+
+ @Test
+ void jarWithoutTheAttributeHasNoParts() {
+ assertEquals(List.of(), read(jarWithPartClasses()).toolingParts());
+ }
+
+ @Test
+ void blankAttributeHasNoParts() {
+ assertEquals(List.of(), read(jarWithPartClasses().toolingParts(" ")).toolingParts());
+ }
+
+ @Test
+ void severalEntriesAreReadInListedOrderAndAPlatformMayRepeat() {
+ var jar = jarWithPartClasses("pack.tools.bukkit.StudioPart", "pack.other.bukkit.ImportPart", "pack.tools.modded.ModdedPart")
+ .toolingParts("bukkit=pack.tools.bukkit.StudioPart modded=pack.tools.modded.ModdedPart\tbukkit=pack.other.bukkit.ImportPart");
+
+ var parts = read(jar).toolingParts();
+
+ assertEquals(List.of("bukkit=pack.tools.bukkit.StudioPart", "modded=pack.tools.modded.ModdedPart",
+ "bukkit=pack.other.bukkit.ImportPart"), parts.stream().map(ToolingPart::declaration).toList());
+ assertEquals(new PackageName("pack.tools.bukkit"), parts.getFirst().packageName());
+ assertEquals(ORIGIN, parts.getFirst().jarOrigin());
+ assertTrue(parts.getFirst().contains("pack.tools.bukkit.deep.Helper"));
+ assertFalse(parts.getFirst().contains("pack.tools.bukkitx.Helper"));
+ }
+
+ @Test
+ void twoEntriesOfOnePlatformMayShareAPackage() {
+ var jar = jarWithPartClasses("pack.tools.bukkit.StudioPart", "pack.tools.bukkit.ImportPart")
+ .toolingParts("bukkit=pack.tools.bukkit.StudioPart bukkit=pack.tools.bukkit.ImportPart");
+
+ assertEquals(2, read(jar).toolingParts().size());
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"bukkit", "bukkit=", "=pack.tools.StudioPart", "Bukkit=pack.tools.StudioPart", "1bukkit=pack.tools.StudioPart",
+ "bukkit=StudioPart", "bukkit=pack.tools.", "bukkit=pack..StudioPart", "bukkit=pack.tools.StudioPart=again", "bukkit:pack.tools.StudioPart"})
+ void malformedEntryFailsNamingTheJarAndTheEntry(String entry) {
+ var message = failureOf(jarWithPartClasses("pack.tools.StudioPart").toolingParts("modded=pack.tools.StudioPart " + entry));
+
+ assertTrue(message.contains(ORIGIN), message);
+ assertTrue(message.contains("'" + entry + "'"), message);
+ assertTrue(message.contains("Iris-Tooling-Parts"), message);
+ }
+
+ @Test
+ void entryClassThatTheJarDoesNotHoldFailsNamingTheJarAndTheEntry() {
+ var message = failureOf(jarWithPartClasses("pack.tools.StudioPart").toolingParts("bukkit=pack.tools.MissingPart"));
+
+ assertTrue(message.contains(ORIGIN), message);
+ assertTrue(message.contains("bukkit=pack.tools.MissingPart"), message);
+ assertTrue(message.contains("holds no class pack.tools.MissingPart"), message);
+ }
+
+ @Test
+ void partsOfDifferentPlatformsInOnePackageFail() {
+ var jar = jarWithPartClasses("pack.tools.BukkitPart", "pack.tools.ModdedPart")
+ .toolingParts("bukkit=pack.tools.BukkitPart modded=pack.tools.ModdedPart");
+
+ var message = failureOf(jar);
+
+ assertTrue(message.contains(ORIGIN), message);
+ assertTrue(message.contains("overlapping tooling parts bukkit=pack.tools.BukkitPart and modded=pack.tools.ModdedPart"), message);
+ }
+
+ @Test
+ void partInsideAnotherPartsPackageFails() {
+ var jar = jarWithPartClasses("pack.tools.StudioPart", "pack.tools.deep.ImportPart")
+ .toolingParts("bukkit=pack.tools.StudioPart bukkit=pack.tools.deep.ImportPart");
+
+ assertTrue(failureOf(jar).contains("overlapping tooling parts"));
+ }
+}
diff --git a/host/src/test/java/com/volmit/support/TestModuleJar.java b/host/src/test/java/com/volmit/support/TestModuleJar.java
index 26945ebea..7c0a6fe33 100644
--- a/host/src/test/java/com/volmit/support/TestModuleJar.java
+++ b/host/src/test/java/com/volmit/support/TestModuleJar.java
@@ -34,7 +34,9 @@ public final class TestModuleJar {
private final Map sources = new LinkedHashMap<>();
private final Map resources = new LinkedHashMap<>();
private static final String OPTIONAL_PACKAGES_ATTRIBUTE = "Iris-Optional-Packages";
+ private static final String TOOLING_PARTS_ATTRIBUTE = "Iris-Tooling-Parts";
private boolean multiRelease;
+ private String toolingParts;
private final List optionalPackages = new ArrayList<>();
private final List entryPoints = new ArrayList<>();
private final List classpath = new ArrayList<>(List.of(classesOf(IrisModule.class)));
@@ -91,6 +93,12 @@ public final class TestModuleJar {
return this;
}
+ /** Sets the manifest's {@code Iris-Tooling-Parts} attribute to {@code declaration}, verbatim. */
+ public TestModuleJar toolingParts(String declaration) {
+ toolingParts = declaration;
+ return this;
+ }
+
/** Puts a jar or class directory on the compile classpath only; it is not packaged. */
public TestModuleJar compileAgainst(Path classes) {
classpath.add(classes);
@@ -165,6 +173,7 @@ public final class TestModuleJar {
if (!optionalPackages.isEmpty()) {
manifest.getMainAttributes().putValue(OPTIONAL_PACKAGES_ATTRIBUTE, String.join(" ", optionalPackages));
}
+ if (toolingParts != null) manifest.getMainAttributes().putValue(TOOLING_PARTS_ATTRIBUTE, toolingParts);
return manifest;
}