Add parameter injectors (#104)

This commit is contained in:
Alexander Söderberg 2020-10-25 05:45:38 +01:00
parent e4efffe577
commit c2065aabd1
11 changed files with 469 additions and 17 deletions

View file

@ -0,0 +1,60 @@
//
// 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.lang.reflect.AnnotatedElement;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
final class AnnotatedElementAccessor implements AnnotationAccessor {
private final AnnotatedElement element;
AnnotatedElementAccessor(final @NonNull AnnotatedElement element) {
this.element = Objects.requireNonNull(element, "Method may not be null");
}
@Override
public <A extends Annotation> @Nullable A annotation(
@NonNull final Class<A> clazz
) {
try {
return element.getAnnotation(clazz);
} catch (final NullPointerException exception) {
return null;
}
}
@Override
public @NonNull Collection<@NonNull Annotation> annotations() {
return Collections.unmodifiableCollection(Arrays.asList(this.element.getAnnotations()));
}
}

View file

@ -0,0 +1,70 @@
//
// 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.lang.reflect.AnnotatedElement;
import java.util.Collection;
/**
* Managed access for {@link java.lang.annotation.Annotation} instances
*
* @since 1.2.0
*/
public interface AnnotationAccessor {
/**
* Get a {@link AnnotationAccessor} instance for a {@link AnnotatedElement}, such as
* a {@link Class} or a {@link java.lang.reflect.Method}. This instance can then be
* used as a proxy for retrieving the element's annotations
*
* @param element Annotated element that will be proxied by the accessor
* @return Annotation accessor proxying the given annotated element
*/
static @NonNull AnnotationAccessor of(final @NonNull AnnotatedElement element) {
return new AnnotatedElementAccessor(element);
}
/**
* Get an annotation instance, if it's present. If the annotation
* isn't available, this will return {@code null}
*
* @param clazz Annotation class
* @param <A> Annotation type
* @return Annotation instance, or {@code null}
*/
<A extends Annotation> @Nullable A annotation(@NonNull Class<A> clazz);
/**
* Get an immutable collection containing all of the annotations that
* are accessible using the annotation accessor
*
* @return Immutable collection of annotations
*/
@NonNull Collection<@NonNull Annotation> annotations();
}

View file

@ -0,0 +1,28 @@
//
// 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.
//
/**
* Annotation and annotation parsing related classes and utilities
*/
package cloud.commandframework.annotations;