A settings class whose @Feature has no key fails the load naming the module, the class and the key to add

This commit is contained in:
piexel committed 2026-10-10 08:35:10 +02:00
1 parent 6757cbdd0f
commit f8d85dcd5e
5 files changed
+91 -8

No files matched your search

@@ -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<F extends IrisFeature>(
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) {
@@ -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);
}
}
@@ -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);
}
}
}
@@ -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");
@@ -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));