mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Read tooling parts from the module jar's manifest
Iris-Tooling-Parts lists <platform>=<entry class> entries. A part is the entry class's package and subpackages. A malformed entry, an entry class the jar does not hold, or parts that would share a class fail the load naming the jar and the entry.
This commit is contained in:
1 parent
806792ebae
commit
b943a43442
5 files changed
+233
-4
No files matched your search
@@ -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;
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>The tooling parts are those the manifest lists under {@value ToolingPart#MANIFEST_ATTRIBUTE}, see {@link ToolingPart}.
|
||||
*/
|
||||
record ModuleJar(Path path, String origin, List<String> entryClasses, List<JarClass> classes,
|
||||
List<PackageName> optionalPackages) {
|
||||
List<PackageName> optionalPackages, List<ToolingPart> 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<String> entryClasses, List<JarCl
|
||||
entryClasses = List.copyOf(entryClasses);
|
||||
classes = List.copyOf(classes);
|
||||
optionalPackages = List.copyOf(optionalPackages);
|
||||
toolingParts = List.copyOf(toolingParts);
|
||||
}
|
||||
|
||||
/** A class in the jar, by binary name, and the binary names of the classes it links against. */
|
||||
@@ -72,8 +76,10 @@ record ModuleJar(Path path, String origin, List<String> entryClasses, List<JarCl
|
||||
try (var file = openAsClassLoaderSeesIt(jar)) {
|
||||
var entryClasses = requireEntryClasses(origin, readEntryClasses(file));
|
||||
var classEntries = findClassEntries(file);
|
||||
return new ModuleJar(jar, origin, entryClasses, readClasses(origin, file, classEntries),
|
||||
readOptionalPackages(origin, file.getManifest()));
|
||||
var read = new ModuleJar(jar, origin, entryClasses, readClasses(origin, file, classEntries),
|
||||
readOptionalPackages(origin, file.getManifest()), ToolingPart.readAll(origin, jar, file.getManifest()));
|
||||
ToolingPart.requireEntryClassesIn(origin, read.toolingParts, Set.copyOf(read.classNames()));
|
||||
return read;
|
||||
} catch (IOException exception) {
|
||||
throw new ModuleLoadException(null, origin, "Module jar " + origin + " is not a readable jar", exception);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.util.Optional;
|
||||
* names match: {@code com.volmit} contains {@code com.volmit.stage.Stage} but not
|
||||
* {@code com.volmitx.Fake}.
|
||||
*/
|
||||
record PackageName(String name) implements Comparable<PackageName> {
|
||||
public record PackageName(String name) implements Comparable<PackageName> {
|
||||
private static final char SEPARATOR = '.';
|
||||
private static final String SEPARATOR_PATTERN = "\\.";
|
||||
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
* <p>The attribute lists {@code <platform>=<entry class>} 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<ToolingPart> 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<ToolingPart> parts, Set<String> 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<ToolingPart> 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");
|
||||
}
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,9 @@ public final class TestModuleJar {
|
||||
private final Map<String, String> sources = new LinkedHashMap<>();
|
||||
private final Map<String, byte[]> 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<String> optionalPackages = new ArrayList<>();
|
||||
private final List<String> entryPoints = new ArrayList<>();
|
||||
private final List<Path> 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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user