Skip to content

Commit

Permalink
GH-401 Support for inherited methods. (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi authored Jun 7, 2024
1 parent 4fbdd29 commit 9f0c706
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -31,7 +32,7 @@ public CommandBuilder<SENDER> processBuilder(InstanceSource source) {
AnnotationInvoker<SENDER> classInvoker = new ClassInvoker<>(type, context);
context = annotationProcessorService.process(classInvoker);

for (Method method : type.getDeclaredMethods()) {
for (Method method : ReflectUtil.getMethods(type)) {
MethodInvoker<SENDER> methodInvoker = new MethodInvoker<>(annotationProcessorService, validatorService, wrapperRegistry, instance, method, context);

context = annotationProcessorService.process(methodInvoker);
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,20 @@ private static Class<?>[] getClasses(Object[] params) {
}


public static List<Method> getMethods(Class<?> type) {
List<Method> 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;
}

}

0 comments on commit 9f0c706

Please sign in to comment.