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);
}
}
}