use of com.cognifide.apm.api.exceptions.InvalidActionMapperException in project APM by Cognifide.
the class MapperDescriptorFactory method create.
private Optional<MappingDescriptor> create(Mapper mapper, Method method) {
Mapping mapping = method.getAnnotation(Mapping.class);
if (mapping == null) {
return Optional.empty();
}
if (!Action.class.equals(method.getReturnType())) {
throw new InvalidActionMapperException("Mapping method must have return type " + Action.class.getName());
}
List<ParameterDescriptor> parameterDescriptors = Lists.newArrayList();
Type[] types = method.getGenericParameterTypes();
Annotation[][] annotations = method.getParameterAnnotations();
int requiredIndex = 0;
for (int i = 0; i < types.length; i++) {
Type type = types[i];
Annotation[] parameterAnnotations = annotations[i];
Class<? extends ApmType> apmType = getApmType(type);
ParameterDescriptor parameterDescriptor = null;
if (containsAnnotation(parameterAnnotations, Named.class)) {
Named namedAnnotation = getAnnotation(parameterAnnotations, Named.class);
parameterDescriptor = new NamedParameterDescriptor(apmType, namedAnnotation);
} else if (containsAnnotation(parameterAnnotations, Flags.class)) {
Flags flagsAnnotation = getAnnotation(parameterAnnotations, Flags.class);
parameterDescriptor = new FlagsParameterDescriptor(apmType, flagsAnnotation.value());
} else if (containsAnnotation(parameterAnnotations, Flag.class)) {
Flag flagAnnotation = getAnnotation(parameterAnnotations, Flag.class);
parameterDescriptor = new FlagParameterDescriptor(apmType, flagAnnotation);
} else {
Required requiredAnnotation = getAnnotation(parameterAnnotations, Required.class);
parameterDescriptor = new RequiredParameterDescriptor(apmType, requiredIndex, requiredAnnotation);
requiredIndex++;
}
parameterDescriptors.add(parameterDescriptor);
}
return Optional.of(new MappingDescriptor(method, mapper, mapping, ImmutableList.copyOf(parameterDescriptors)));
}
use of com.cognifide.apm.api.exceptions.InvalidActionMapperException in project APM by Cognifide.
the class MapperDescriptorFactory method create.
public MapperDescriptor create(Class<?> mapperClass) {
Mapper mapperAnnotation = mapperClass.getDeclaredAnnotation(Mapper.class);
if (mapperAnnotation == null) {
throw new InvalidActionMapperException("Mapper must be annotated with " + Mapper.class.getName());
}
final Object mapper = createInstance(mapperClass);
final String name = mapperAnnotation.value();
final String group = mapperAnnotation.group();
final List<MappingDescriptor> mappingDescriptors = Lists.newArrayList();
for (Method method : mapperClass.getDeclaredMethods()) {
create(mapperAnnotation, method).ifPresent(mappingDescriptors::add);
}
return new MapperDescriptor(mapper, name, group, ImmutableList.copyOf(mappingDescriptors));
}
Aggregations