Search in sources :

Example 1 with InvalidActionMapperException

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)));
}
Also used : FlagParameterDescriptor(com.cognifide.apm.core.actions.ParameterDescriptor.FlagParameterDescriptor) Named(com.cognifide.apm.api.actions.annotations.Named) Action(com.cognifide.apm.api.actions.Action) RequiredParameterDescriptor(com.cognifide.apm.core.actions.ParameterDescriptor.RequiredParameterDescriptor) FlagsParameterDescriptor(com.cognifide.apm.core.actions.ParameterDescriptor.FlagsParameterDescriptor) FlagParameterDescriptor(com.cognifide.apm.core.actions.ParameterDescriptor.FlagParameterDescriptor) NamedParameterDescriptor(com.cognifide.apm.core.actions.ParameterDescriptor.NamedParameterDescriptor) Mapping(com.cognifide.apm.api.actions.annotations.Mapping) Flags(com.cognifide.apm.api.actions.annotations.Flags) RequiredParameterDescriptor(com.cognifide.apm.core.actions.ParameterDescriptor.RequiredParameterDescriptor) Flag(com.cognifide.apm.api.actions.annotations.Flag) Annotation(java.lang.annotation.Annotation) ApmType(com.cognifide.apm.core.grammar.ApmType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) FlagsParameterDescriptor(com.cognifide.apm.core.actions.ParameterDescriptor.FlagsParameterDescriptor) Required(com.cognifide.apm.api.actions.annotations.Required) InvalidActionMapperException(com.cognifide.apm.api.exceptions.InvalidActionMapperException) NamedParameterDescriptor(com.cognifide.apm.core.actions.ParameterDescriptor.NamedParameterDescriptor)

Example 2 with InvalidActionMapperException

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));
}
Also used : Mapper(com.cognifide.apm.api.actions.annotations.Mapper) InvalidActionMapperException(com.cognifide.apm.api.exceptions.InvalidActionMapperException) ApmString(com.cognifide.apm.core.grammar.ApmString) Method(java.lang.reflect.Method)

Aggregations

InvalidActionMapperException (com.cognifide.apm.api.exceptions.InvalidActionMapperException)2 Action (com.cognifide.apm.api.actions.Action)1 Flag (com.cognifide.apm.api.actions.annotations.Flag)1 Flags (com.cognifide.apm.api.actions.annotations.Flags)1 Mapper (com.cognifide.apm.api.actions.annotations.Mapper)1 Mapping (com.cognifide.apm.api.actions.annotations.Mapping)1 Named (com.cognifide.apm.api.actions.annotations.Named)1 Required (com.cognifide.apm.api.actions.annotations.Required)1 FlagParameterDescriptor (com.cognifide.apm.core.actions.ParameterDescriptor.FlagParameterDescriptor)1 FlagsParameterDescriptor (com.cognifide.apm.core.actions.ParameterDescriptor.FlagsParameterDescriptor)1 NamedParameterDescriptor (com.cognifide.apm.core.actions.ParameterDescriptor.NamedParameterDescriptor)1 RequiredParameterDescriptor (com.cognifide.apm.core.actions.ParameterDescriptor.RequiredParameterDescriptor)1 ApmString (com.cognifide.apm.core.grammar.ApmString)1 ApmType (com.cognifide.apm.core.grammar.ApmType)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1