
plus.extvos.mqtt.subscribe.ParameterModel Maven / Gradle / Ivy
package plus.extvos.mqtt.subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.NonNull;
import plus.extvos.mqtt.annotation.Payload;
import plus.extvos.mqtt.annotation.TopicVariable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.LinkedList;
/**
* @author tocrhz
*/
final class ParameterModel {
private final static Logger log = LoggerFactory.getLogger(ParameterModel.class);
private boolean sign;
private boolean required;
private Class> type;
private String name;
private Object defaultValue;
private LinkedList> converters;
private ParameterModel() {
}
public static LinkedList of(Method method) {
LinkedList parameters = new LinkedList<>();
Class>[] parameterTypes = method.getParameterTypes();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (int i = 0; i < parameterTypes.length; i++) {
ParameterModel model = new ParameterModel();
parameters.add(model);
model.type = parameterTypes[i];
model.defaultValue = defaultValue(model.type);
Annotation[] annotations = parameterAnnotations[i];
if (annotations != null && annotations.length > 0) {
for (Annotation annotation : annotations) {
if (annotation.annotationType() == TopicVariable.class) {
TopicVariable namedValue = (TopicVariable) annotation;
model.required = model.required || namedValue.required();
model.name = namedValue.value();
}
if (annotation.annotationType() == Payload.class) {
Payload payload = (Payload) annotation;
model.sign = true;
model.required = model.required || payload.required();
model.converters = toConverters(payload.value());
}
if (annotation.annotationType() == NonNull.class) {
model.required = true;
}
}
}
}
return parameters;
}
@SuppressWarnings("unchecked")
public static LinkedList> toConverters(Class extends Converter, ?>>[] classes) {
if (classes == null || classes.length == 0) {
return null;
} else {
LinkedList> converters = new LinkedList<>();
for (Class extends Converter, ?>> covert : classes) {
try {
converters.add((Converter
© 2015 - 2025 Weber Informatics LLC | Privacy Policy