✨ Add access to parameter annotations in the parameter injector
This commit is contained in:
parent
92f8661b59
commit
9550dce5e6
7 changed files with 147 additions and 6 deletions
|
|
@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
- Allow command argument names to include `_` and `-` ([#186](https://github.com/Incendo/cloud/pull/186))
|
||||
- Make it easier to use translatable components with MinecraftHelp ([#197](https://github.com/Incendo/cloud/pull/197))
|
||||
- Show "No result for query" when a multi-help topic is empty
|
||||
- Use the method+field annotation accessor rather than the method accessor when injecting method parameters
|
||||
|
||||
### Deprecated
|
||||
- Description, and everything using Description directly ([#207](https://github.com/Incendo/cloud/pull/207))
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ class MethodCommandExecutionHandler<C> implements CommandExecutionHandler<C> {
|
|||
final Optional<?> value = this.injectorRegistry.getInjectable(
|
||||
parameter.getType(),
|
||||
commandContext,
|
||||
this.annotationAccessor
|
||||
AnnotationAccessor.of(AnnotationAccessor.of(parameter), this.annotationAccessor)
|
||||
);
|
||||
if (value.isPresent()) {
|
||||
arguments.add(value.get());
|
||||
|
|
|
|||
|
|
@ -60,6 +60,18 @@ public interface AnnotationAccessor {
|
|||
return new AnnotatedElementAccessor(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a {@link AnnotationAccessor} instance that delegates to multiple {@link AnnotatedElement} instances.
|
||||
* The first accessor that provides a requested annotation will always be used
|
||||
*
|
||||
* @param accessors The accessor to delegate to
|
||||
* @return Annotation accessor that delegates to the given accessors (using their natural ordering)
|
||||
* @since 1.4.0
|
||||
*/
|
||||
static @NonNull AnnotationAccessor of(final @NonNull AnnotationAccessor@NonNull... accessors) {
|
||||
return new MultiDelegateAnnotationAccessor(accessors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an annotation instance, if it's present. If the annotation
|
||||
* isn't available, this will return {@code null}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,64 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg & Contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework.annotations;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
final class MultiDelegateAnnotationAccessor implements AnnotationAccessor {
|
||||
|
||||
private final AnnotationAccessor[] accessors;
|
||||
|
||||
MultiDelegateAnnotationAccessor(final @NonNull AnnotationAccessor@NonNull... accessors) {
|
||||
this.accessors = accessors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <A extends Annotation> @Nullable A annotation(@NonNull final Class<A> clazz) {
|
||||
A instance = null;
|
||||
for (final AnnotationAccessor annotationAccessor : accessors) {
|
||||
instance = annotationAccessor.annotation(clazz);
|
||||
if (instance != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Collection<@NonNull Annotation> annotations() {
|
||||
final List<Annotation> annotationList = new LinkedList<>();
|
||||
for (final AnnotationAccessor annotationAccessor : accessors) {
|
||||
annotationList.addAll(annotationAccessor.annotations());
|
||||
}
|
||||
return Collections.unmodifiableCollection(annotationList);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -44,7 +44,7 @@ public interface ParameterInjector<C, T> {
|
|||
* annotated method. If the injector cannot (or shouldn't) create a value, it is free to return {@code null}.
|
||||
*
|
||||
* @param context Command context that is requesting the injection
|
||||
* @param annotationAccessor Annotation accessor proxying the method which the value is being injected into
|
||||
* @param annotationAccessor Annotation accessor proxying the method and parameter which the value is being injected into
|
||||
* @return The value, if it could be created. Else {@code null}, in which case no value will be injected
|
||||
* by this particular injector
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -0,0 +1,64 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg & Contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package cloud.commandframework;
|
||||
|
||||
import cloud.commandframework.annotations.AnnotationAccessor;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
|
||||
public class AnnotationAccessorTest {
|
||||
|
||||
@Qualifier("method")
|
||||
public static void annotatedMethod(@Qualifier("parameter") final String parameter) {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testQualifierResolutionOrder() throws Exception {
|
||||
final Method method = AnnotationAccessorTest.class.getMethod("annotatedMethod", String.class);
|
||||
final Parameter parameter = method.getParameters()[0];
|
||||
final AnnotationAccessor accessor = AnnotationAccessor.of(
|
||||
AnnotationAccessor.of(parameter),
|
||||
AnnotationAccessor.of(method)
|
||||
);
|
||||
final Qualifier qualifier = accessor.annotation(Qualifier.class);
|
||||
Assertions.assertNotNull(qualifier);
|
||||
Assertions.assertEquals("parameter", qualifier.value());
|
||||
}
|
||||
|
||||
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
private @interface Qualifier {
|
||||
|
||||
String value();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -48,7 +48,7 @@ public class ParameterInjectorRegistryTest {
|
|||
private Injector injector;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
void setup() {
|
||||
this.commandSender = new TestCommandSender();
|
||||
this.commandManager = new TestCommandManager();
|
||||
this.commandContextFactory = new StandardCommandContextFactory<>();
|
||||
|
|
@ -63,7 +63,7 @@ public class ParameterInjectorRegistryTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleInjection() {
|
||||
void testSimpleInjection() {
|
||||
Assertions.assertEquals(INJECTED_INTEGER, parameterInjectorRegistry.getInjectable(
|
||||
Integer.class,
|
||||
this.createContext(),
|
||||
|
|
@ -72,7 +72,7 @@ public class ParameterInjectorRegistryTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testGuiceInjection() {
|
||||
void testGuiceInjection() {
|
||||
this.parameterInjectorRegistry.registerInjectionService(GuiceInjectionService.create(this.injector));
|
||||
Assertions.assertEquals(TestModule.INJECTED_INTEGER, parameterInjectorRegistry.getInjectable(
|
||||
Integer.class,
|
||||
|
|
@ -82,7 +82,7 @@ public class ParameterInjectorRegistryTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testNonExistentInjection() {
|
||||
void testNonExistentInjection() {
|
||||
Assertions.assertNull(parameterInjectorRegistry.getInjectable(
|
||||
String.class,
|
||||
this.createContext(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue