Add mod validation for annotations: schemas, migrations, configuration bounds, listener methods, and commands.

This commit is contained in:
Overlord committed 2026-10-08 12:44:23 +02:00
1 parent 9af951b4be
commit c79a90b88f
10 files changed
+389 -18

No files matched your search

@@ -95,6 +95,26 @@ public final class RecordBinder
return keys;
}
/**
* The problems of the {@link Key}, {@link Min}, and {@link Max} annotations of {@code type}'s
* components, as {@link #keys} and binding report them; empty when there are none. For
* build-time checks.
*/
public static List<String> check(Class<? extends Record> type)
{
List<String> problems = new ArrayList<>();
try { RecordBinder.keys(type); }
catch (IllegalArgumentException e) { problems.add(e.getMessage()); }
for (RecordComponent component : type.getRecordComponents()) {
try { RecordBinder.bounds(component); }
catch (IllegalArgumentException e) { problems.add(e.getMessage()); }
}
return List.copyOf(problems);
}
/** A record of {@code type} from {@code table}, or {@code null} when a problem was found. */
public <T extends Record> @Nullable T record(Map<String, Object> table, Class<T> type, String path)
{
@@ -43,8 +43,10 @@ import java.util.function.Consumer;
* with a filter in front: only the commands of its module (and command) reach it. An
* {@link EveryNTicks} method is a main-thread listener for {@link TickEvent} that only every n-th
* tick reaches. {@link Every} methods are returned as schedules.
*
* <p>{@link #check} runs the same rules on a class without an object, for build-time checks.
*/
final class ListenerScanner
public final class ListenerScanner
{
/** What {@link #scan} found, each list in registration order. */
record Found(List<ListenerMethod> listeners, List<ScheduledMethod> schedules) {}
@@ -83,11 +85,40 @@ final class ListenerScanner
*/
static Found scan(Object target)
{
Map<Class<?>, Method[]> hierarchy = ListenerScanner.declaredMethods(target.getClass());
List<String> problems = new ArrayList<>();
Found found = ListenerScanner.scan(target.getClass(), target, problems);
if (!problems.isEmpty()) throw new IllegalArgumentException(String.join(" ", problems));
return found;
}
/**
* The problems that {@code Events.register(Object)} reports for an object of {@code type}, one
* per method or class; empty when there are none. Nothing is called.
*/
public static List<String> check(Class<?> type)
{
List<String> problems = new ArrayList<>();
try { ListenerScanner.scan(type, null, problems); }
catch (IllegalArgumentException e) { problems.add(e.getMessage()); }
return List.copyOf(problems);
}
/**
* The annotated methods of {@code type}, bound to {@code target}; without a target, their
* actions do nothing. Adds the problems to {@code problems}.
*
* @throws IllegalArgumentException when the methods of a class cannot be read
*/
private static Found scan(Class<?> type, @Nullable Object target, List<String> problems)
{
Map<Class<?>, Method[]> hierarchy = ListenerScanner.declaredMethods(type);
List<Class<?>> types = List.copyOf(hierarchy.keySet());
List<ListenerMethod> listeners = new ArrayList<>();
List<ScheduledMethod> schedules = new ArrayList<>();
List<String> problems = new ArrayList<>();
for (int i = 0; i < types.size(); i++)
{
@@ -128,8 +159,6 @@ final class ListenerScanner
}
}
if (!problems.isEmpty()) throw new IllegalArgumentException(String.join(" ", problems));
return new Found(List.copyOf(listeners), List.copyOf(schedules));
}
@@ -189,7 +218,7 @@ final class ListenerScanner
}
/** The listener for {@code method}, or {@code null} after adding its problems to {@code problems}. */
private static @Nullable ListenerMethod listener(Object target, Method method, boolean modThread, @Nullable OnClientCommand clientCommand, String where, List<String> problems)
private static @Nullable ListenerMethod listener(@Nullable Object target, Method method, boolean modThread, @Nullable OnClientCommand clientCommand, String where, List<String> problems)
{
Class<?>[] parameters = method.getParameterTypes();
int before = problems.size();
@@ -230,7 +259,7 @@ final class ListenerScanner
}
/** The {@link TickEvent} listener for an {@link EveryNTicks} method, or {@code null} after adding its problems to {@code problems}. */
private static @Nullable ListenerMethod everyNTicks(Object target, Method method, EveryNTicks annotation, String where, List<String> problems)
private static @Nullable ListenerMethod everyNTicks(@Nullable Object target, Method method, EveryNTicks annotation, String where, List<String> problems)
{
int before = problems.size();
int n = annotation.value();
@@ -252,7 +281,7 @@ final class ListenerScanner
}
/** The schedule of an {@link Every} method, or {@code null} after adding its problems to {@code problems}. */
private static @Nullable ScheduledMethod every(Object target, Method method, Every annotation, String where, List<String> problems)
private static @Nullable ScheduledMethod every(@Nullable Object target, Method method, Every annotation, String where, List<String> problems)
{
int before = problems.size();
@@ -300,13 +329,16 @@ final class ListenerScanner
/**
* A handle for {@code method} of type {@code type}, bound to {@code target} for an instance
* method, or {@code null} after adding the problem to {@code problems}.
* method, or {@code null} after adding the problem to {@code problems}. Without a target, the
* access is checked and the handle does nothing.
*/
private static @Nullable MethodHandle handle(Object target, Method method, MethodType type, String where, List<String> problems)
private static @Nullable MethodHandle handle(@Nullable Object target, Method method, MethodType type, String where, List<String> problems)
{
try
{
MethodHandle handle = MethodHandles.privateLookupIn(method.getDeclaringClass(), MethodHandles.lookup()).unreflect(method);
if (target == null) return MethodHandles.empty(type);
if (!Modifier.isStatic(method.getModifiers())) handle = handle.bindTo(target);
return handle.asType(type);
@@ -27,13 +27,25 @@ import java.util.function.Function;
* @param version from {@link Schema}, or 1 without it
* @param migrations stored schema version to migration, from the {@link Migrate} methods
*/
record RecordSchema<R extends Record>(int version, Map<Integer, Function<Row, R>> migrations)
public record RecordSchema<R extends Record>(int version, Map<Integer, Function<Row, R>> migrations)
{
RecordSchema
public RecordSchema
{
migrations = Map.copyOf(migrations);
}
/**
* The problems that {@link #of} reports for {@code type}, in one message; empty when there are
* none. For build-time checks.
*/
public static List<String> check(Class<? extends Record> type)
{
try { RecordSchema.of(type); }
catch (IllegalArgumentException e) { return List.of(e.getMessage()); }
return List.of();
}
/** @throws IllegalArgumentException listing every invalid annotation of {@code type} */
static <R extends Record> RecordSchema<R> of(Class<R> type)
{