All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.chutneytesting.task.domain.TaskTemplateParserV2 Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package com.chutneytesting.task.domain;

import com.chutneytesting.task.domain.parameter.Parameter;
import com.chutneytesting.task.spi.Task;
import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TaskTemplateParserV2 implements TaskTemplateParser {
    private static final String CLASS_NAME_TASK_SUFFIX = "Task";
    private static final Converter CAMEL_TO_HYPHEN_CONVERTER = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_HYPHEN);

    @Override
    public ResultOrError parse(Class taskClass) {
        if (taskClass.getDeclaredConstructors().length > 1) {
            return ResultOrError.error(new ParsingError(taskClass, "More than one constructor"));
        }
        String taskName = computeTaskName(taskClass);
        Constructor constructor = (Constructor) taskClass.getDeclaredConstructors()[0];
        List parameters = extractParameters(constructor);
        return ResultOrError.result(new TaskTemplateV2(taskName, taskClass, constructor, parameters));
    }

    private List extractParameters(Constructor constructor) {
        return Arrays.stream(constructor.getParameters())
            .map(Parameter::fromJavaParameter)
            .collect(Collectors.toList());
    }

    private String computeTaskName(Class taskClass) {
        // TODO extract @taskIdentifier if present
        String taskName = taskClass.getSimpleName();
        if (taskName.endsWith(CLASS_NAME_TASK_SUFFIX)) {
            taskName = taskName.substring(0, taskName.length() - CLASS_NAME_TASK_SUFFIX.length());
        }
        taskName = CAMEL_TO_HYPHEN_CONVERTER.convert(taskName);
        return taskName;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy