diff --git a/src/main/java/com/volmit/host/FeatureDescriptor.java b/src/main/java/com/volmit/host/FeatureDescriptor.java index 93b74e4e5..3ae32b0cd 100644 --- a/src/main/java/com/volmit/host/FeatureDescriptor.java +++ b/src/main/java/com/volmit/host/FeatureDescriptor.java @@ -5,6 +5,7 @@ import com.volmit.IrisFeature; import com.volmit.config.Feature; import com.volmit.config.OptionsMetadata; import com.volmit.resource.ResourceId; +import java.lang.annotation.IncompleteAnnotationException; import java.lang.reflect.Modifier; import java.util.List; import java.util.Objects; @@ -49,14 +50,18 @@ public record FeatureDescriptor( throw new IllegalArgumentException("Missing @Feature: " + type.getName()); } - return new FeatureDescriptor<>( - ResourceId.parse(annotation.id()), - annotation.name(), - annotation.key(), - annotation.root().isEmpty() ? Optional.empty() : Optional.of(annotation.root()), - type, - new FeatureAttachment(Set.of(annotation.targets())), - OptionsMetadata.inspect(type)); + try { + return new FeatureDescriptor<>( + ResourceId.parse(annotation.id()), + annotation.name(), + annotation.key(), + annotation.root().isEmpty() ? Optional.empty() : Optional.of(annotation.root()), + type, + new FeatureAttachment(Set.of(annotation.targets())), + OptionsMetadata.inspect(type)); + } catch (IncompleteAnnotationException missing) { + throw IncompleteFeatureException.of(type, annotation, missing); + } } private static void requireKey(ResourceId id, String key) { diff --git a/src/main/java/com/volmit/host/IncompleteFeatureException.java b/src/main/java/com/volmit/host/IncompleteFeatureException.java new file mode 100644 index 000000000..946f37ce7 --- /dev/null +++ b/src/main/java/com/volmit/host/IncompleteFeatureException.java @@ -0,0 +1,38 @@ +package com.volmit.host; + +import com.volmit.config.Feature; +import java.lang.annotation.IncompleteAnnotationException; +import java.util.Arrays; +import java.util.stream.Collectors; + +/** A settings class whose {@code @Feature} lacks an element the API requires, such as a class compiled before + * {@code key} existed. The load names the module around it ({@link LoadedModules}). + */ +final class IncompleteFeatureException extends IllegalArgumentException { + private static final String KEY = "key"; + + private IncompleteFeatureException(String message, Throwable cause) { + super(message, cause); + } + + static IncompleteFeatureException of(Class type, Feature feature, IncompleteAnnotationException missing) { + if (!KEY.equals(missing.elementName())) { + return new IncompleteFeatureException("The @Feature of settings class " + type.getName() + " has no " + + missing.elementName() + "; add " + missing.elementName() + " = ... to it and rebuild the jar", missing); + } + return new IncompleteFeatureException("The @Feature of settings class " + type.getName() + " has no key, the JSON" + + " field packs write the settings under; add key = \"" + suggestedKey(feature) + "\" (or another plain field" + + " name) to it and rebuild the jar", missing); + } + + /** The feature id's path in camel case, such as {@code mossCover} for {@code example:moss/cover}. */ + private static String suggestedKey(Feature feature) { + String id = feature.id(); + String path = id.substring(id.indexOf(':') + 1); + String camel = Arrays.stream(path.split("[/_.-]+")) + .filter(word -> !word.isEmpty()) + .map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1)) + .collect(Collectors.joining()); + return camel.isEmpty() ? "settings" : Character.toLowerCase(camel.charAt(0)) + camel.substring(1); + } +} diff --git a/src/main/java/com/volmit/host/LoadedModules.java b/src/main/java/com/volmit/host/LoadedModules.java index dcb6f9978..2805ec262 100644 --- a/src/main/java/com/volmit/host/LoadedModules.java +++ b/src/main/java/com/volmit/host/LoadedModules.java @@ -83,6 +83,9 @@ public record LoadedModules( throw new IllegalArgumentException("Module " + discovered.info().id() + " from " + discovered.origin() + " failed to link or initialize a class while installing: " + error + "; a missing method or field means the jar was built against a different Iris API", error); + } catch (IncompleteFeatureException incomplete) { + throw new IllegalArgumentException("Module " + discovered.info().id() + " from " + discovered.origin() + ": " + + incomplete.getMessage(), incomplete); } } } diff --git a/src/test/java/com/volmit/host/PackModulesTest.java b/src/test/java/com/volmit/host/PackModulesTest.java index 800dfd886..68cfc74a7 100644 --- a/src/test/java/com/volmit/host/PackModulesTest.java +++ b/src/test/java/com/volmit/host/PackModulesTest.java @@ -632,6 +632,35 @@ class PackModulesTest { } } + @Test + void settingsWithoutAKeyFailNamingTheModuleTheClassAndTheKeyToAdd(@TempDir Path build) { + var apiWithoutKeys = new TestModuleJar().source("com.volmit.config.Feature", """ + package com.volmit.config; + import java.lang.annotation.*; + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + public @interface Feature { + String id(); + String name(); + com.volmit.FeatureAttachment.Target[] targets(); + } + """).writeInto(build, "api-without-keys.jar"); + module("pack.old.Old", "old:flora", """ + public void register(com.volmit.FeatureRegistry registry) { registry.registerSettings(Patch.class); } + @com.volmit.config.Feature(id = "old:flora/wild-patch", name = "Patch", targets = com.volmit.FeatureAttachment.Target.BIOME) + public static final class Patch implements com.volmit.IrisFeature {} + """, NO_STAGES) + .compileAgainstOlderApi(apiWithoutKeys) + .writeInto(pack, "old.jar"); + + try (var packModules = PackModules.load(pack)) { + assertEquals("Module old:flora from " + origin("old.jar") + ": The @Feature of settings class pack.old.Old$Patch" + + " has no key, the JSON field packs write the settings under; add key = \"floraWildPatch\" (or" + + " another plain field name) to it and rebuild the jar", + failure(() -> LoadedModules.load(packModules.modules(), Map.of(), Set.of(), Set.of()))); + } + } + @Test void duplicateModuleIdsAcrossJarsFailNamingBothJars() { module("pack.one.Duplicate", "dup:core", NO_MEMBERS, NO_STAGES).writeInto(pack, "one.jar"); diff --git a/src/test/java/com/volmit/support/TestModuleJar.java b/src/test/java/com/volmit/support/TestModuleJar.java index 4234089e6..d0f5e977c 100644 --- a/src/test/java/com/volmit/support/TestModuleJar.java +++ b/src/test/java/com/volmit/support/TestModuleJar.java @@ -94,6 +94,14 @@ public final class TestModuleJar { return this; } + /** Puts a jar or class directory before the API on the compile classpath, so the sources compile against an + * older copy of an API class it holds; it is not packaged. + */ + public TestModuleJar compileAgainstOlderApi(Path classes) { + classpath.addFirst(classes); + return this; + } + /** Compiles against wherever {@code type} was loaded from, such as a host library jar. */ public TestModuleJar compileAgainstHostOf(Class type) { return compileAgainst(classesOf(type));