CMDAPIChangesForTeamCommand #40
@ -26,10 +26,7 @@ import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.function.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class AbstractSWCommand<T> {
|
||||
@ -148,12 +145,12 @@ public abstract class AbstractSWCommand<T> {
|
||||
}
|
||||
|
||||
private boolean validateMethod(Method method) {
|
||||
if (!checkType(method.getAnnotations(), method.getReturnType(), annotation -> {
|
||||
if (!checkType(method.getAnnotations(), method.getReturnType(), false, annotation -> {
|
||||
CommandMetaData.Method methodMetaData = annotation.annotationType().getAnnotation(CommandMetaData.Method.class);
|
||||
if (methodMetaData == null) return aClass -> true;
|
||||
if (methodMetaData == null) return (aClass, varArg) -> true;
|
||||
if (method.getParameterCount() > methodMetaData.maxParameterCount() || method.getParameterCount() < methodMetaData.minParameterCount())
|
||||
return aClass -> false;
|
||||
return aClass -> {
|
||||
return (aClass, varArg) -> false;
|
||||
return (aClass, varArg) -> {
|
||||
Class<?>[] types = methodMetaData.value();
|
||||
if (types == null) return true;
|
||||
for (Class<?> type : types) {
|
||||
@ -164,20 +161,19 @@ public abstract class AbstractSWCommand<T> {
|
||||
}, "The method '" + method + "'")) return false;
|
||||
boolean valid = true;
|
||||
for (Parameter parameter : method.getParameters()) {
|
||||
Class<?> type = parameter.getType();
|
||||
if (parameter.isVarArgs()) type = type.getComponentType();
|
||||
if (!checkType(parameter.getAnnotations(), type, annotation -> {
|
||||
if (!checkType(parameter.getAnnotations(), parameter.getType(), parameter.isVarArgs(), annotation -> {
|
||||
CommandMetaData.Parameter parameterMetaData = annotation.annotationType().getAnnotation(CommandMetaData.Parameter.class);
|
||||
if (parameterMetaData == null) return aClass -> true;
|
||||
if (parameterMetaData == null) return (aClass, varArg) -> true;
|
||||
Class<?> handler = parameterMetaData.handler();
|
||||
if (Predicate.class.isAssignableFrom(handler)) {
|
||||
if (BiPredicate.class.isAssignableFrom(handler)) {
|
||||
try {
|
||||
return (Predicate<Class<?>>) handler.getConstructor().newInstance();
|
||||
return (BiPredicate<Class<?>, Boolean>) handler.getConstructor().newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
|
||||
NoSuchMethodException e) {
|
||||
}
|
||||
}
|
||||
return aClass -> {
|
||||
return (aClass, varArg) -> {
|
||||
if (varArg) aClass = aClass.getComponentType();
|
||||
Class<?>[] types = parameterMetaData.value();
|
||||
if (types == null) return true;
|
||||
for (Class<?> current : types) {
|
||||
@ -190,11 +186,11 @@ public abstract class AbstractSWCommand<T> {
|
||||
return valid;
|
||||
}
|
||||
|
||||
private boolean checkType(Annotation[] annotations, Class<?> clazz, Function<Annotation, Predicate<Class<?>>> toApplicableTypes, String warning) {
|
||||
private boolean checkType(Annotation[] annotations, Class<?> clazz, boolean varArg, Function<Annotation, BiPredicate<Class<?>, Boolean>> toApplicableTypes, String warning) {
|
||||
boolean valid = true;
|
||||
for (Annotation annotation : annotations) {
|
||||
Predicate<Class<?>> predicate = toApplicableTypes.apply(annotation);
|
||||
if (!predicate.test(clazz)) {
|
||||
BiPredicate<Class<?>, Boolean> predicate = toApplicableTypes.apply(annotation);
|
||||
if (!predicate.test(clazz, varArg)) {
|
||||
commandSystemWarning(() -> warning + " is using an unsupported annotation of type '" + annotation.annotationType().getName() + "'");
|
||||
valid = false;
|
||||
}
|
||||
@ -446,7 +442,7 @@ public abstract class AbstractSWCommand<T> {
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.PARAMETER})
|
||||
@CommandMetaData.ImplicitValidator(handler = ErrorMessage.Handler.class, order = 2)
|
||||
@CommandMetaData.ImplicitValidator(handler = ErrorMessage.Handler.class, order = Integer.MAX_VALUE)
|
||||
protected @interface ErrorMessage {
|
||||
/**
|
||||
* Error message to be displayed when the parameter is invalid.
|
||||
@ -486,7 +482,7 @@ public abstract class AbstractSWCommand<T> {
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.PARAMETER})
|
||||
@CommandMetaData.Parameter({int.class, Integer.class, long.class, Long.class, float.class, Float.class, double.class, Double.class})
|
||||
@CommandMetaData.ImplicitValidator(handler = Min.Handler.class, order = 3)
|
||||
@CommandMetaData.ImplicitValidator(handler = Min.Handler.class, order = 2)
|
||||
protected @interface Min {
|
||||
int intValue() default Integer.MIN_VALUE;
|
||||
|
||||
@ -519,7 +515,7 @@ public abstract class AbstractSWCommand<T> {
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.PARAMETER})
|
||||
@CommandMetaData.Parameter({int.class, Integer.class, long.class, Long.class, float.class, Float.class, double.class, Double.class})
|
||||
@CommandMetaData.ImplicitValidator(handler = Max.Handler.class, order = 3)
|
||||
@CommandMetaData.ImplicitValidator(handler = Max.Handler.class, order = 2)
|
||||
protected @interface Max {
|
||||
int intValue() default Integer.MAX_VALUE;
|
||||
|
||||
@ -618,11 +614,9 @@ public abstract class AbstractSWCommand<T> {
|
||||
|
||||
int max() default Integer.MAX_VALUE;
|
||||
|
||||
String errorMessage() default "";
|
||||
|
||||
class Type implements Predicate<Class<?>> {
|
||||
class Type implements BiPredicate<Class<?>, Boolean> {
|
||||
@Override
|
||||
public boolean test(Class<?> clazz) {
|
||||
public boolean test(Class<?> clazz, Boolean isVarArgs) {
|
||||
return clazz.isArray();
|
||||
}
|
||||
}
|
||||
@ -631,26 +625,17 @@ public abstract class AbstractSWCommand<T> {
|
||||
|
||||
private int min;
|
||||
private int max;
|
||||
private String errorMessage;
|
||||
|
||||
public Handler(ArrayLength arrayLength) {
|
||||
this.min = arrayLength.min();
|
||||
this.max = arrayLength.max();
|
||||
this.errorMessage = arrayLength.errorMessage();
|
||||
if (this.errorMessage.isEmpty()) {
|
||||
this.errorMessage = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(T sender, Object value, MessageSender messageSender) {
|
||||
if (value == null) return true;
|
||||
int length = Array.getLength(value);
|
||||
boolean valid = length >= min && length <= max;
|
||||
if (errorMessage != null) {
|
||||
messageSender.send(!valid, errorMessage);
|
||||
}
|
||||
return valid;
|
||||
return length >= min && length <= max;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -67,9 +67,4 @@ public class ArgumentCommand extends TestSWCommand {
|
||||
public void minAndMaxLengthArgument(String sender, @Length(min = 3, max = 3) @StaticValue({"wo", "world"}) String arg) {
|
||||
throw new ExecutionIdentifier("RunLengthArgument with String");
|
||||
}
|
||||
|
||||
@Register
|
||||
public void arrayLengthArgument(String sender, @ArrayLength(min = 2) int... args) {
|
||||
throw new ExecutionIdentifier("RunArrayLengthArgument with Integer");
|
||||
}
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren