From 61631fb20f0df0a3ab5eb4098d8a98b7c73790bf Mon Sep 17 00:00:00 2001 From: Rollczi Date: Fri, 7 Jun 2024 23:47:21 +0200 Subject: [PATCH] Support inherited methods. --- .../annotations/InstanceSourceProcessor.java | 3 +- .../annotations/reflect/SuperMethodTest.java | 34 +++++++++++++++++++ .../litecommands/reflect/ReflectUtil.java | 16 +++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 litecommands-annotations/test/dev/rollczi/litecommands/annotations/reflect/SuperMethodTest.java diff --git a/litecommands-annotations/src/dev/rollczi/litecommands/annotations/InstanceSourceProcessor.java b/litecommands-annotations/src/dev/rollczi/litecommands/annotations/InstanceSourceProcessor.java index 4e68e737..b61acc58 100644 --- a/litecommands-annotations/src/dev/rollczi/litecommands/annotations/InstanceSourceProcessor.java +++ b/litecommands-annotations/src/dev/rollczi/litecommands/annotations/InstanceSourceProcessor.java @@ -3,6 +3,7 @@ import dev.rollczi.litecommands.annotations.validator.method.MethodValidatorService; import dev.rollczi.litecommands.command.builder.CommandBuilder; import dev.rollczi.litecommands.meta.Meta; +import dev.rollczi.litecommands.reflect.ReflectUtil; import dev.rollczi.litecommands.wrapper.WrapperRegistry; import java.lang.reflect.Method; @@ -31,7 +32,7 @@ public CommandBuilder processBuilder(InstanceSource source) { AnnotationInvoker classInvoker = new ClassInvoker<>(type, context); context = annotationProcessorService.process(classInvoker); - for (Method method : type.getDeclaredMethods()) { + for (Method method : ReflectUtil.getMethods(type)) { MethodInvoker methodInvoker = new MethodInvoker<>(annotationProcessorService, validatorService, wrapperRegistry, instance, method, context); context = annotationProcessorService.process(methodInvoker); diff --git a/litecommands-annotations/test/dev/rollczi/litecommands/annotations/reflect/SuperMethodTest.java b/litecommands-annotations/test/dev/rollczi/litecommands/annotations/reflect/SuperMethodTest.java new file mode 100644 index 00000000..bb330a34 --- /dev/null +++ b/litecommands-annotations/test/dev/rollczi/litecommands/annotations/reflect/SuperMethodTest.java @@ -0,0 +1,34 @@ +package dev.rollczi.litecommands.annotations.reflect; + +import dev.rollczi.litecommands.annotations.LiteTestSpec; +import dev.rollczi.litecommands.annotations.command.Command; +import dev.rollczi.litecommands.annotations.execute.Execute; +import org.junit.jupiter.api.Test; + +class SuperMethodTest extends LiteTestSpec { + + @Command(name = "test") + static class TestCommand extends BaseCommand { + @Execute(name = "new") + String executeNew() { + return "new"; + } + } + + static class BaseCommand { + @Execute(name = "legacy") + String executeLegacy() { + return "legacy"; + } + } + + @Test + void testSuperMethod() { + platform.execute("test legacy") + .assertSuccess("legacy"); + + platform.execute("test new") + .assertSuccess("new"); + } + +} diff --git a/litecommands-core/src/dev/rollczi/litecommands/reflect/ReflectUtil.java b/litecommands-core/src/dev/rollczi/litecommands/reflect/ReflectUtil.java index e502efa4..50392b1c 100644 --- a/litecommands-core/src/dev/rollczi/litecommands/reflect/ReflectUtil.java +++ b/litecommands-core/src/dev/rollczi/litecommands/reflect/ReflectUtil.java @@ -194,4 +194,20 @@ private static Class[] getClasses(Object[] params) { } + public static List getMethods(Class type) { + List methods = new ArrayList<>(); + + for (Method declaredMethod : type.getDeclaredMethods()) { + methods.add(declaredMethod); + } + + Class superclass = type.getSuperclass(); + + if (superclass != Object.class) { + methods.addAll(getMethods(superclass)); + } + + return methods; + } + }