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

io.cucumber.java8.Java8StepDefinition Maven / Gradle / Ivy

There is a newer version: 7.20.1
Show newest version
package io.cucumber.java8;

import io.cucumber.core.backend.ParameterInfo;
import io.cucumber.core.backend.StepDefinition;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static net.jodah.typetools.TypeResolver.resolveRawArguments;

final class Java8StepDefinition extends AbstractGlueDefinition implements StepDefinition {

    private final List parameterInfos;
    private final String expression;
    private  Java8StepDefinition(String expression,
                                                               Class bodyClass,
                                                               T body) {
        super(body, new Exception().getStackTrace()[3]);
        this.expression = requireNonNull(expression, "cucumber-expression may not be null");
        this.parameterInfos = fromTypes(expression, location, resolveRawArguments(bodyClass, body.getClass()));
    }

    public static  Java8StepDefinition create(
        String expression, Class bodyClass, T body) {
        return new Java8StepDefinition(expression, bodyClass, body);
    }

    private static List fromTypes(String expression, StackTraceElement location, Type[] genericParameterTypes) {
        return Arrays.stream(genericParameterTypes)
            .map(type -> new LambdaTypeResolver(type, expression, location))
            .map(Java8ParameterInfo::new)
            .collect(toList());
    }

    @Override
    public void execute(final Object[] args) {
        Invoker.invoke(this, body, method, args);
    }

    @Override
    public List parameterInfos() {
        return parameterInfos;
    }

    @Override
    public String getPattern() {
        return expression;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy