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

org.jtwig.reflection.resolver.BeanMethodResolverImpl Maven / Gradle / Ivy

There is a newer version: 5.87.0.RELEASE
Show newest version
package org.jtwig.reflection.resolver;

import com.google.common.base.Optional;
import org.jtwig.reflection.Executable;
import org.jtwig.reflection.model.Value;
import org.jtwig.reflection.model.bean.BeanMethod;
import org.jtwig.reflection.model.java.JavaMethodArgument;
import org.jtwig.reflection.resolver.argument.InputArgumentResolver;
import org.jtwig.reflection.resolver.argument.InputArgumentResolverFactory;
import org.jtwig.reflection.resolver.argument.ParameterResolver;

import java.util.ArrayList;
import java.util.List;

public class BeanMethodResolverImpl implements BeanMethodResolver {
    private final InputArgumentResolverFactory factory;
    private final ParameterResolver parameterResolver;

    public BeanMethodResolverImpl(InputArgumentResolverFactory factory,
                                  ParameterResolver parameterResolver) {
        this.factory = factory;
        this.parameterResolver = parameterResolver;
    }

    @Override
    public Optional resolve(BeanMethod beanMethod, List inputParameterList) {
        List arguments = new ArrayList();
        InputArgumentResolver inputArgumentResolver = factory.create(beanMethod, inputParameterList);

        for (JavaMethodArgument javaMethodArgument : beanMethod.method().arguments()) {
            Optional resolve = parameterResolver.resolve(inputArgumentResolver, javaMethodArgument);

            if (!resolve.isPresent()) {
                return Optional.absent();
            }

            arguments.add(resolve.get().getValue());
        }

        return inputArgumentResolver.isFullyResolved() ?
                Optional.of(new Executable(beanMethod, arguments.toArray())) :
                Optional.absent();
    }
}