diff --git a/cloud-annotations/src/test/java/cloud/commandframework/annotations/feature/ParameterInjectionTest.java b/cloud-annotations/src/test/java/cloud/commandframework/annotations/feature/ParameterInjectionTest.java index 9849fe902..e0a704b1c 100644 --- a/cloud-annotations/src/test/java/cloud/commandframework/annotations/feature/ParameterInjectionTest.java +++ b/cloud-annotations/src/test/java/cloud/commandframework/annotations/feature/ParameterInjectionTest.java @@ -52,13 +52,16 @@ void setup() { @Test void testInjection() { // Arrange - this.commandManager.parameterInjectorRegistry().registerInjector(Integer.class, ParameterInjector.constantInjector(5)); - this.commandManager.parameterInjectorRegistry().registerInjector(new TypeToken() { - }, ParameterInjector.constantInjector(new StringWrapper("abc"))); - this.commandManager.parameterInjectorRegistry().registerInjector(new TypeToken>() { - }, ParameterInjector.constantInjector(new Wrapper<>(true))); - this.commandManager.parameterInjectorRegistry().registerInjector(SomeImplementation.class, - ParameterInjector.constantInjector(new SomeImplementation())); + this.commandManager.parameterInjectorRegistry() + .registerInjector(Integer.class, ParameterInjector.constantInjector(5)) + .registerInjector(new TypeToken() {}, ParameterInjector.constantInjector(new StringWrapper("abc"))) + .registerInjector( + new TypeToken>() {}, + ParameterInjector.constantInjector(new Wrapper<>(true)) + ).registerInjector( + SomeImplementation.class, + ParameterInjector.constantInjector(new SomeImplementation()) + ); this.annotationParser.parse(new TestClass()); // Act diff --git a/cloud-core/src/main/java/cloud/commandframework/annotations/injection/ParameterInjectorRegistry.java b/cloud-core/src/main/java/cloud/commandframework/annotations/injection/ParameterInjectorRegistry.java index ef68686d4..db8dcb9de 100644 --- a/cloud-core/src/main/java/cloud/commandframework/annotations/injection/ParameterInjectorRegistry.java +++ b/cloud-core/src/main/java/cloud/commandframework/annotations/injection/ParameterInjectorRegistry.java @@ -40,6 +40,7 @@ import org.apiguardian.api.API; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; +import org.checkerframework.common.returnsreceiver.qual.This; /** * Registry containing mappings between {@link Class} {@link Predicate Predicates} @@ -71,12 +72,13 @@ public ParameterInjectorRegistry() { * @param injected type * @param clazz type that the injector should inject for * @param injector the injector that should inject the value into the command method + * @return {@code this} */ - public synchronized void registerInjector( + public synchronized @This @NonNull ParameterInjectorRegistry registerInjector( final @NonNull Class clazz, final @NonNull ParameterInjector injector ) { - this.registerInjector(TypeToken.get(clazz), injector); + return this.registerInjector(TypeToken.get(clazz), injector); } /** @@ -85,14 +87,15 @@ public synchronized void registerInjector( * @param injected type * @param type type that the injector should inject for * @param injector the injector that should inject the value into the command method + * @return {@code this} * @since 2.0.0 */ @API(status = API.Status.STABLE, since = "2.0.0") - public synchronized void registerInjector( + public synchronized @This @NonNull ParameterInjectorRegistry registerInjector( final @NonNull TypeToken type, final @NonNull ParameterInjector injector ) { - this.registerInjector(cl -> GenericTypeReflector.isSuperType(cl.getType(), type.getType()), injector); + return this.registerInjector(cl -> GenericTypeReflector.isSuperType(cl.getType(), type.getType()), injector); } /** @@ -107,14 +110,16 @@ public synchronized void registerInjector( * @param injected type * @param predicate a predicate that matches if the injector should be used for a type * @param injector the injector that should inject the value into the command method + * @return {@code this} * @since 1.8.0 */ @API(status = API.Status.STABLE, since = "1.8.0") - public synchronized void registerInjector( + public synchronized @This @NonNull ParameterInjectorRegistry registerInjector( final @NonNull Predicate> predicate, final @NonNull ParameterInjector injector ) { this.injectors.add(Pair.of(predicate, injector)); + return this; } @Override @@ -205,12 +210,14 @@ public synchronized void registerInjector( * {@link #getInjectable(Class, CommandContext, AnnotationAccessor)}. * * @param service Service implementation + * @return {@code this} * @since 1.4.0 */ @API(status = API.Status.STABLE, since = "1.4.0") - public void registerInjectionService(final InjectionService service) { + public @This @NonNull ParameterInjectorRegistry registerInjectionService(final InjectionService service) { this.servicePipeline.registerServiceImplementation(new TypeToken>() { }, service, Collections.emptyList()); + return this; } private synchronized @NonNull Collection<@NonNull ParameterInjector> injectors(final @NonNull TypeToken type) {