/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2023 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package de.steamwar.command;
import java.lang.annotation.*;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Optional;
import java.util.function.Function;
public interface Handler {
default Optional> getGenericTypeOfReturn(Method method) {
Type type;
try {
type = method.getGenericReturnType();
} catch (Exception e) {
return Optional.empty();
}
if (!(type instanceof ParameterizedType)) {
return Optional.empty();
}
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] generics = parameterizedType.getActualTypeArguments();
if (generics.length != 1 && generics.length != 2) {
return Optional.empty();
}
Type genericType = generics[generics.length - 1];
try {
return Optional.of(Class.forName(genericType.getTypeName()));
} catch (Exception e) {
return Optional.empty();
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
@interface Implementation {
Class extends Handler> value();
}
interface DataCheckable {
boolean hasExecutorMapper(Class> clazz);
boolean hasMapper(String key);
boolean hasValidator(String key);
boolean hasSupplier(String key);
}
interface DataReadable {
Function, ?> getExecutorMapper(Class> clazz);
AbstractTypeMapper getMapper(String key);
AbstractTypeValidator getValidator(String key);
AbstractTypeSupplier getSupplier(String key);
}
interface DataWritable {
Function, ?> getExecutorMapper(Class> clazz);
void addMapper(String key, boolean local, AbstractTypeMapper mapper);
void addValidator(String key, boolean local, AbstractTypeValidator validator);
void addSupplier(String key, boolean local, AbstractTypeSupplier supplier);
/**
* Invoking the same method twice will result in the exact same result.
*/
T invoke(Method method);
void addCommand(Method method, String[] subCommand, String[] description, boolean noTabComplete);
}
interface HandlerMethod extends Handler {
void check(T annotation, Method method, DataCheckable dataCheckable) throws Exception;
int getRunPriority();
void run(T annotation, Method method, DataWritable dataWritable);
}
interface HandlerParameter extends Handler {
void check(T annotation, Parameter parameter, int index, DataCheckable dataCheckable) throws Exception;
default boolean needsParentTypeMapper() {
return false;
}
/**
* Null values are allowed to be returned.
*/
default AbstractTypeMapper getTypeMapper(T annotation, Parameter parameter, int index, DataReadable dataReadable, AbstractTypeMapper parentTypeMapper) {
return null;
}
default int getValidatorPriority() {
return 0;
}
/**
* Null values are allowed to be returned.
*/
default AbstractTypeValidator getValidator(T annotation, Parameter parameter, int index, DataReadable dataReadable) {
return null;
}
}
}