forked from SteamWar/SteamWar
Fix MapperHandler, SupplierHandler, ValidatorHandler
Improve HandlerForVariableElement.getAnnotations() Improve HandlerForTypeMirror.isEnum Add HandlerForTypeMirror.getTypeName
This commit is contained in:
@@ -185,7 +185,7 @@ public abstract class AbstractCommand<T> {
|
||||
handlerMap.get(runPriority).forEach((dataMethodAnnotationTriple, handlerMethods) -> {
|
||||
handlerMethods.forEach(annotationHandlerMethod -> {
|
||||
try {
|
||||
annotationHandlerMethod.check(dataMethodAnnotationTriple.c, dataMethodAnnotationTriple.b, dataMethodAnnotationTriple.a);
|
||||
annotationHandlerMethod.check(dataMethodAnnotationTriple.c, new Handler.MethodWrapper.ForMethod(dataMethodAnnotationTriple.b), dataMethodAnnotationTriple.a);
|
||||
} catch (Exception e) {
|
||||
throw new UnsupportedOperationException("Method check failed for " + dataMethodAnnotationTriple.b.getName(), e);
|
||||
}
|
||||
|
||||
@@ -106,12 +106,7 @@ public interface Handler {
|
||||
}
|
||||
|
||||
interface HandlerMethod<T extends Annotation> extends Handler {
|
||||
default void check(T annotation, Method method, DataCheckable dataCheckable) throws Exception {
|
||||
check(annotation, new MethodWrapper.ForMethod(method), dataCheckable);
|
||||
}
|
||||
|
||||
default void check(T annotation, MethodWrapper method, DataCheckable dataCheckable) throws HandlerException {
|
||||
}
|
||||
void check(T annotation, MethodWrapper method, DataCheckable dataCheckable) throws HandlerException;
|
||||
|
||||
int getRunPriority();
|
||||
|
||||
@@ -119,12 +114,7 @@ public interface Handler {
|
||||
}
|
||||
|
||||
interface HandlerParameter<T extends Annotation, A, B> extends Handler {
|
||||
default void check(T annotation, Parameter parameter, int index, DataCheckable dataCheckable) throws Exception {
|
||||
check(annotation, new MethodWrapper.ForMethod((Method) parameter.getDeclaringExecutable()), new ParameterWrapper.ForParameter(parameter), index, dataCheckable);
|
||||
}
|
||||
|
||||
default void check(T annotation, MethodWrapper methodWrapper, ParameterWrapper parameter, int index, DataCheckable dataCheckable) throws HandlerException {
|
||||
}
|
||||
void check(T annotation, MethodWrapper methodWrapper, ParameterWrapper parameter, int index, DataCheckable dataCheckable) throws HandlerException;
|
||||
|
||||
default boolean needsParentTypeMapper() {
|
||||
return false;
|
||||
@@ -263,6 +253,8 @@ public interface Handler {
|
||||
|
||||
String getName();
|
||||
|
||||
String getTypeName();
|
||||
|
||||
class ForClass implements TypeWrapper {
|
||||
@Getter
|
||||
private final Class<?> clazz;
|
||||
@@ -321,6 +313,11 @@ public interface Handler {
|
||||
public String getName() {
|
||||
return this.clazz.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTypeName() {
|
||||
return this.clazz.getTypeName();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -82,7 +82,7 @@ public final class MapperHandler {
|
||||
throw new HandlerException("Mapper annotation cannot be used on first parameter");
|
||||
}
|
||||
if (!dataCheckable.hasMapper(annotation.value())) {
|
||||
throw new UnsupportedOperationException("Mapper '" + annotation.value() + "' not found");
|
||||
throw new HandlerException("Mapper '" + annotation.value() + "' not found");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-7
@@ -26,8 +26,6 @@ import de.steamwar.command.annotations.Name;
|
||||
import de.steamwar.command.annotations.Supplier;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
public final class SupplierHandler {
|
||||
@@ -80,21 +78,22 @@ public final class SupplierHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void check(Supplier annotation, Parameter parameter, int index, DataCheckable dataCheckable) throws Exception {
|
||||
public void check(Supplier annotation, MethodWrapper methodWrapper, ParameterWrapper parameter, int index, DataCheckable dataCheckable) throws HandlerException {
|
||||
if (index == 0) {
|
||||
throw new UnsupportedOperationException("Supplier annotation cannot be used on the first parameter");
|
||||
throw new HandlerException("Supplier annotation cannot be used on the first parameter");
|
||||
}
|
||||
String s = annotation.value() != null && !annotation.value().isEmpty() ? annotation.value() : parameter.getType().getTypeName();
|
||||
if (!dataCheckable.hasSupplier(s)) {
|
||||
throw new UnsupportedOperationException("Supplier '" + annotation.value() + "' not found");
|
||||
throw new HandlerException("Supplier '" + annotation.value() + "' not found");
|
||||
}
|
||||
boolean hasDisallowedAnnotations = Arrays.stream(parameter.getAnnotations())
|
||||
boolean hasDisallowedAnnotations = parameter.getAnnotations()
|
||||
.stream()
|
||||
.filter(anno -> !(anno instanceof Supplier))
|
||||
.filter(anno -> !(anno instanceof AllowNull))
|
||||
.filter(anno -> !(anno instanceof Name))
|
||||
.anyMatch(anno -> anno.getClass().isAnnotationPresent(Implementation.class));
|
||||
if (hasDisallowedAnnotations) {
|
||||
throw new UnsupportedOperationException("Only AllowNull or Name can be used in conjunction with Supplier annotation");
|
||||
throw new HandlerException("Only AllowNull or Name can be used in conjunction with Supplier annotation");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -77,10 +77,10 @@ public final class ValidatorHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void check(Validator annotation, Parameter parameter, int index, DataCheckable dataCheckable) throws Exception {
|
||||
public void check(Validator annotation, MethodWrapper methodWrapper, ParameterWrapper parameter, int index, DataCheckable dataCheckable) throws HandlerException {
|
||||
String s = annotation.value() != null && !annotation.value().isEmpty() ? annotation.value() : parameter.getType().getTypeName();
|
||||
if (!dataCheckable.hasValidator(s)) {
|
||||
throw new UnsupportedOperationException("Validator '" + annotation.value() + "' not found");
|
||||
throw new HandlerException("Validator '" + annotation.value() + "' not found");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user