mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-12-01 10:14:36 +00:00
Hide the platform packages of the tooling api from packs
com.volmit.tooling.<platform> is carved out of the exposed packages the way com.volmit.host is; the neutral com.volmit.tooling stays visible.
This commit is contained in:
1 parent
b943a43442
commit
2e4fd306e8
2 files changed
+46
-3
No files matched your search
@@ -11,15 +11,20 @@ import java.util.TreeSet;
|
|||||||
* as the packages of built-in modules whose stages packs may require. A package is exposed with
|
* as the packages of built-in modules whose stages packs may require. A package is exposed with
|
||||||
* all its subpackages, and only whole package names match: {@code com.volmit} does not expose
|
* all its subpackages, and only whole package names match: {@code com.volmit} does not expose
|
||||||
* {@code com.volmitx}. The host side of IrisApi, {@code com.volmit.host}, lies inside the API's
|
* {@code com.volmitx}. The host side of IrisApi, {@code com.volmit.host}, lies inside the API's
|
||||||
* root package but is never exposed: it loads and runs modules, and no module may reach it.
|
* root package but is never exposed: it loads and runs modules, and no module may reach it. The
|
||||||
|
* platform packages of the tooling api, {@code com.volmit.tooling.<platform>}, are never exposed either:
|
||||||
|
* only a tooling part may use them. The neutral {@code com.volmit.tooling} stays exposed.
|
||||||
*/
|
*/
|
||||||
final class ExposedPackages {
|
final class ExposedPackages {
|
||||||
/** The root package of the Iris API. Module classes must not use it. */
|
/** The root package of the Iris API. Module classes must not use it. */
|
||||||
static final String API_PACKAGE = IrisModule.class.getPackageName();
|
static final String API_PACKAGE = IrisModule.class.getPackageName();
|
||||||
/** The host side of IrisApi, which packs never see, though it lies inside {@link #API_PACKAGE}. */
|
/** The host side of IrisApi, which packs never see, though it lies inside {@link #API_PACKAGE}. */
|
||||||
static final String HOST_PACKAGE = ExposedPackages.class.getPackageName();
|
static final String HOST_PACKAGE = ExposedPackages.class.getPackageName();
|
||||||
|
/** The neutral tooling api. Each platform's own api is a subpackage of it, such as {@code com.volmit.tooling.bukkit}. */
|
||||||
|
static final String TOOLING_PACKAGE = API_PACKAGE + ".tooling";
|
||||||
private static final PackageName API = new PackageName(API_PACKAGE);
|
private static final PackageName API = new PackageName(API_PACKAGE);
|
||||||
private static final PackageName HOST = new PackageName(HOST_PACKAGE);
|
private static final PackageName HOST = new PackageName(HOST_PACKAGE);
|
||||||
|
private static final PackageName TOOLING = new PackageName(TOOLING_PACKAGE);
|
||||||
private static final char PACKAGE_SEPARATOR = '.';
|
private static final char PACKAGE_SEPARATOR = '.';
|
||||||
private static final char PATH_SEPARATOR = '/';
|
private static final char PATH_SEPARATOR = '/';
|
||||||
|
|
||||||
@@ -50,12 +55,13 @@ final class ExposedPackages {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean exposes(String binaryName) {
|
boolean exposes(String binaryName) {
|
||||||
return !HOST.contains(binaryName) && packages.stream().anyMatch(name -> name.contains(binaryName));
|
return reachesPacks(binaryName) && !isToolingPlatformPackage(packageNameOf(binaryName, PACKAGE_SEPARATOR));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Whether a resource path, such as {@code com/volmit/IrisModule.class}, lies in an exposed package. */
|
/** Whether a resource path, such as {@code com/volmit/IrisModule.class}, lies in an exposed package. */
|
||||||
boolean exposesResource(String path) {
|
boolean exposesResource(String path) {
|
||||||
return exposes(path.replace(PATH_SEPARATOR, PACKAGE_SEPARATOR));
|
var directory = packageNameOf(path, PATH_SEPARATOR).replace(PATH_SEPARATOR, PACKAGE_SEPARATOR);
|
||||||
|
return reachesPacks(path.replace(PATH_SEPARATOR, PACKAGE_SEPARATOR)) && !isToolingPlatformPackage(directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The exposed package a class is in; the most specific one when exposed packages nest. */
|
/** The exposed package a class is in; the most specific one when exposed packages nest. */
|
||||||
@@ -67,6 +73,21 @@ final class ExposedPackages {
|
|||||||
.orElseThrow(() -> new IllegalArgumentException(binaryName + " is not in an exposed package"));
|
.orElseThrow(() -> new IllegalArgumentException(binaryName + " is not in an exposed package"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean reachesPacks(String dottedName) {
|
||||||
|
return !HOST.contains(dottedName) && packages.stream().anyMatch(name -> name.contains(dottedName));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Whether the package lies below {@code com.volmit.tooling}, where each platform's api has its package. */
|
||||||
|
private static boolean isToolingPlatformPackage(String packageName) {
|
||||||
|
return TOOLING.contains(packageName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** What precedes the last separator: the package of a class name, or the directory of a resource path. */
|
||||||
|
private static String packageNameOf(String name, char separator) {
|
||||||
|
int lastSeparator = name.lastIndexOf(separator);
|
||||||
|
return lastSeparator < 0 ? "" : name.substring(0, lastSeparator);
|
||||||
|
}
|
||||||
|
|
||||||
private static PackageName requireExposable(PackageName name) {
|
private static PackageName requireExposable(PackageName name) {
|
||||||
if (name.equals(HOST) || HOST.contains(name.name())) {
|
if (name.equals(HOST) || HOST.contains(name.name())) {
|
||||||
throw new IllegalArgumentException("Exposed package '" + name + "' is IrisApi's host side, which packs never see");
|
throw new IllegalArgumentException("Exposed package '" + name + "' is IrisApi's host side, which packs never see");
|
||||||
|
|||||||
@@ -33,6 +33,28 @@ class ExposedPackagesTest {
|
|||||||
assertEquals("Exposed package 'com.volmit.host.schema' is IrisApi's host side, which packs never see", failure.getMessage());
|
assertEquals("Exposed package 'com.volmit.host.schema' is IrisApi's host side, which packs never see", failure.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void platformToolingPackagesAreNeverExposedButTheNeutralToolingApiIs() {
|
||||||
|
var exposed = ExposedPackages.apiAnd(List.of("com"));
|
||||||
|
|
||||||
|
assertEquals("com.volmit.tooling", ExposedPackages.TOOLING_PACKAGE);
|
||||||
|
assertTrue(exposed.exposes("com.volmit.tooling.Command"));
|
||||||
|
assertTrue(exposed.exposes("com.volmit.tooling.Command$Builder"));
|
||||||
|
assertFalse(exposed.exposes("com.volmit.tooling.bukkit.BukkitToolPart"));
|
||||||
|
assertFalse(exposed.exposes("com.volmit.tooling.modded.deep.ModdedToolPart"));
|
||||||
|
assertTrue(exposed.exposes("com.volmit.toolingx.Other"));
|
||||||
|
assertTrue(exposed.exposes("com.volmit.tool.bukkit.Other"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void platformToolingResourcesAreHiddenLikeTheirClasses() {
|
||||||
|
var exposed = ExposedPackages.apiOnly();
|
||||||
|
|
||||||
|
assertTrue(exposed.exposesResource("com/volmit/tooling/Command.class"));
|
||||||
|
assertFalse(exposed.exposesResource("com/volmit/tooling/bukkit/BukkitToolPart.class"));
|
||||||
|
assertFalse(exposed.exposesResource("com/volmit/tooling/bukkit/part.yml"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void onlyWholePackageNamesMatch() {
|
void onlyWholePackageNamesMatch() {
|
||||||
var exposed = ExposedPackages.apiAnd(List.of("example.builtin"));
|
var exposed = ExposedPackages.apiAnd(List.of("example.builtin"));
|
||||||
|
|||||||
Reference in new issue
Block a user