diff --git a/cloud-annotations/src/main/java/org/incendo/cloud/annotations/AnnotationParser.java b/cloud-annotations/src/main/java/org/incendo/cloud/annotations/AnnotationParser.java index 0734426da..017052736 100644 --- a/cloud-annotations/src/main/java/org/incendo/cloud/annotations/AnnotationParser.java +++ b/cloud-annotations/src/main/java/org/incendo/cloud/annotations/AnnotationParser.java @@ -989,6 +989,16 @@ private void parseParsers(final @NonNull T instance) { } if (commandDescriptor.requiredSender() != Object.class) { + if (!GenericTypeReflector.isSuperType(this.commandSenderType.getType(), commandDescriptor.requiredSender())) { + throw new IllegalArgumentException(String.format( + "Command method %s#%s(%s) has invalid sender type requirement: %s does not inherit from %s", + method.getDeclaringClass().getName(), + method.getName(), + Arrays.stream(method.getParameterTypes()).map(Class::getName).collect(Collectors.joining(", ")), + commandDescriptor.requiredSender().getName(), + this.commandSenderType.getType().getTypeName() + )); + } builder = builder.senderType((Class) commandDescriptor.requiredSender()); } else if (senderType != null) { builder = builder.senderType(senderType); diff --git a/cloud-annotations/src/test/java/org/incendo/cloud/annotations/feature/RequiredSenderDeductionTest.java b/cloud-annotations/src/test/java/org/incendo/cloud/annotations/feature/RequiredSenderTest.java similarity index 84% rename from cloud-annotations/src/test/java/org/incendo/cloud/annotations/feature/RequiredSenderDeductionTest.java rename to cloud-annotations/src/test/java/org/incendo/cloud/annotations/feature/RequiredSenderTest.java index c4203038c..aba829b1c 100644 --- a/cloud-annotations/src/test/java/org/incendo/cloud/annotations/feature/RequiredSenderDeductionTest.java +++ b/cloud-annotations/src/test/java/org/incendo/cloud/annotations/feature/RequiredSenderTest.java @@ -36,9 +36,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows; -class RequiredSenderDeductionTest { +class RequiredSenderTest { private CommandManager> commandManager; + private AnnotationParser> annotationParser; @BeforeEach void setup() { @@ -54,12 +55,12 @@ public boolean hasPermission( return true; } }; - AnnotationParser> annotationParser = new AnnotationParser<>( + this.annotationParser = new AnnotationParser<>( this.commandManager, new TypeToken>() { } ); - annotationParser.parse(new TestClassA()); + this.annotationParser.parse(new TestClassA()); } @Test @@ -83,6 +84,14 @@ void testIncorrectSender() { ); } + @Test + void testInvalidRequirement() { + assertThrows( + IllegalArgumentException.class, + () -> this.annotationParser.parse(new InvalidSenderRequirementTest()) + ); + } + static class TestClassA { @@ -102,4 +111,10 @@ public static class StringSender implements SuperSender { public static class IntegerSender implements SuperSender { } + + static class InvalidSenderRequirementTest { + @Command(value = "invalid_command", requiredSender = String.class) + public void badSenderTypeNoParams() { + } + } }