Search in sources :

Example 1 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-infrastructure by TAKETODAY.

the class ConstructorResolver method createArgumentArray.

/**
 * Create an array of arguments to invoke a constructor or factory method,
 * given the resolved constructor argument values.
 */
private ArgumentsHolder createArgumentArray(String beanName, RootBeanDefinition merged, @Nullable ConstructorArgumentValues resolvedValues, Class<?>[] paramTypes, @Nullable String[] paramNames, Executable executable, BeanWrapper wrapper, boolean autowiring, boolean fallback) throws UnsatisfiedDependencyException {
    TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
    TypeConverter converter = customConverter != null ? customConverter : wrapper;
    ArgumentsHolder args = new ArgumentsHolder(paramTypes.length);
    HashSet<ValueHolder> usedValueHolders = new HashSet<>(paramTypes.length);
    LinkedHashSet<String> autowiredBeanNames = new LinkedHashSet<>(4);
    for (int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++) {
        Class<?> paramType = paramTypes[paramIndex];
        String paramName = paramNames != null ? paramNames[paramIndex] : "";
        // Try to find matching constructor argument value, either indexed or generic.
        ValueHolder valueHolder = null;
        if (resolvedValues != null) {
            valueHolder = resolvedValues.getArgumentValue(paramIndex, paramType, paramName, usedValueHolders);
            // it could match after type conversion (for example, String -> int).
            if (valueHolder == null && (!autowiring || paramTypes.length == resolvedValues.getArgumentCount())) {
                valueHolder = resolvedValues.getGenericArgumentValue(null, null, usedValueHolders);
            }
        }
        if (valueHolder != null) {
            // We found a potential match - let's give it a try.
            // Do not consider the same value definition multiple times!
            usedValueHolders.add(valueHolder);
            Object originalValue = valueHolder.getValue();
            Object convertedValue;
            if (valueHolder.isConverted()) {
                convertedValue = valueHolder.getConvertedValue();
                args.preparedArguments[paramIndex] = convertedValue;
            } else {
                MethodParameter methodParam = MethodParameter.forExecutable(executable, paramIndex);
                try {
                    convertedValue = converter.convertIfNecessary(originalValue, paramType, methodParam);
                } catch (TypeMismatchException ex) {
                    throw new UnsatisfiedDependencyException(merged.getResourceDescription(), beanName, new InjectionPoint(methodParam), "Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(valueHolder.getValue()) + "] to required type [" + paramType.getName() + "]: " + ex.getMessage());
                }
                Object sourceHolder = valueHolder.getSource();
                if (sourceHolder instanceof ValueHolder) {
                    Object sourceValue = ((ValueHolder) sourceHolder).getValue();
                    args.resolveNecessary = true;
                    args.preparedArguments[paramIndex] = sourceValue;
                }
            }
            args.arguments[paramIndex] = convertedValue;
            args.rawArguments[paramIndex] = originalValue;
        } else {
            MethodParameter methodParam = MethodParameter.forExecutable(executable, paramIndex);
            // have to fail creating an argument array for the given constructor.
            if (!autowiring) {
                throw new UnsatisfiedDependencyException(merged.getResourceDescription(), beanName, new InjectionPoint(methodParam), "Ambiguous argument values for parameter of type [" + paramType.getName() + "] - did you specify the correct bean references as arguments?");
            }
            try {
                Object autowiredArgument = resolveAutowiredArgument(methodParam, beanName, autowiredBeanNames, converter, fallback);
                args.resolveNecessary = true;
                args.arguments[paramIndex] = autowiredArgument;
                args.rawArguments[paramIndex] = autowiredArgument;
                args.preparedArguments[paramIndex] = autowiredArgumentMarker;
            } catch (BeansException ex) {
                throw new UnsatisfiedDependencyException(merged.getResourceDescription(), beanName, new InjectionPoint(methodParam), ex);
            }
        }
    }
    for (String autowiredBeanName : autowiredBeanNames) {
        beanFactory.registerDependentBean(autowiredBeanName, beanName);
        if (log.isDebugEnabled()) {
            log.debug("Autowiring by type from bean name '{}' via {} to bean named '{}'", beanName, (executable instanceof Constructor ? "constructor" : "factory method"), autowiredBeanName);
        }
    }
    return args;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) Constructor(java.lang.reflect.Constructor) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) ValueHolder(cn.taketoday.beans.factory.config.ConstructorArgumentValues.ValueHolder) InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) TypeConverter(cn.taketoday.beans.TypeConverter) UnsatisfiedDependencyException(cn.taketoday.beans.factory.UnsatisfiedDependencyException) MethodParameter(cn.taketoday.core.MethodParameter) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) BeansException(cn.taketoday.beans.BeansException)

Example 2 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-infrastructure by TAKETODAY.

the class ParameterResolutionDelegate method resolveDependency.

/**
 * Resolve the dependency for the supplied {@link Parameter} from the
 * supplied {@link AutowireCapableBeanFactory}.
 * <p>Provides comprehensive autowiring support for individual method parameters
 * on par with Framework's dependency injection facilities for autowired fields and
 * methods, including support for {@link Autowired @Autowired},
 * {@link Qualifier @Qualifier}, and {@link Value @Value} with support for property
 * placeholders and EL expressions in {@code @Value} declarations.
 * <p>The dependency is required unless the parameter is annotated or meta-annotated
 * with {@link Autowired @Autowired} with the {@link Autowired#required required}
 * flag set to {@code false}.
 * <p>If an explicit <em>qualifier</em> is not declared, the name of the parameter
 * will be used as the qualifier for resolving ambiguities.
 *
 * @param parameter the parameter whose dependency should be resolved (must not be
 * {@code null})
 * @param parameterIndex the index of the parameter in the constructor or method
 * that declares the parameter
 * @param containingClass the concrete class that contains the parameter; this may
 * differ from the class that declares the parameter in that it may be a subclass
 * thereof, potentially substituting type variables (must not be {@code null})
 * @param beanFactory the {@code AutowireCapableBeanFactory} from which to resolve
 * the dependency (must not be {@code null})
 * @return the resolved object, or {@code null} if none found
 * @throws BeansException if dependency resolution failed
 * @see #isAutowirable
 * @see Autowired#required
 * @see SynthesizingMethodParameter#forExecutable(Executable, int)
 * @see AutowireCapableBeanFactory#resolveDependency(DependencyDescriptor, String)
 */
@Nullable
public static Object resolveDependency(Parameter parameter, int parameterIndex, Class<?> containingClass, AutowireCapableBeanFactory beanFactory) throws BeansException {
    Assert.notNull(parameter, "Parameter must not be null");
    Assert.notNull(containingClass, "Containing class must not be null");
    Assert.notNull(beanFactory, "AutowireCapableBeanFactory must not be null");
    AnnotatedElement annotatedParameter = getEffectiveAnnotatedParameter(parameter, parameterIndex);
    Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class);
    boolean required = (autowired == null || autowired.required());
    MethodParameter methodParameter = SynthesizingMethodParameter.forExecutable(parameter.getDeclaringExecutable(), parameterIndex);
    DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
    descriptor.setContainingClass(containingClass);
    return beanFactory.resolveDependency(descriptor, null);
}
Also used : DependencyDescriptor(cn.taketoday.beans.factory.config.DependencyDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement) MethodParameter(cn.taketoday.core.MethodParameter) SynthesizingMethodParameter(cn.taketoday.core.annotation.SynthesizingMethodParameter) Nullable(cn.taketoday.lang.Nullable)

Example 3 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-infrastructure by TAKETODAY.

the class Spr7538Tests method repro.

@Test
void repro() throws Exception {
    AlwaysTrueReleaseStrategy target = new AlwaysTrueReleaseStrategy();
    BeanFactoryTypeConverter converter = new BeanFactoryTypeConverter();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setTypeConverter(converter);
    List<Foo> arguments = Collections.emptyList();
    List<TypeDescriptor> paramDescriptors = new ArrayList<>();
    Method method = AlwaysTrueReleaseStrategy.class.getMethod("checkCompleteness", List.class);
    paramDescriptors.add(new TypeDescriptor(new MethodParameter(method, 0)));
    List<TypeDescriptor> argumentTypes = new ArrayList<>();
    argumentTypes.add(TypeDescriptor.fromObject(arguments));
    ReflectiveMethodResolver resolver = new ReflectiveMethodResolver();
    MethodExecutor executor = resolver.resolve(context, target, "checkCompleteness", argumentTypes);
    Object result = executor.execute(context, target, arguments);
    System.out.println("Result: " + result);
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TypeDescriptor(cn.taketoday.core.TypeDescriptor) MethodExecutor(cn.taketoday.expression.MethodExecutor) MethodParameter(cn.taketoday.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Example 4 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-infrastructure by TAKETODAY.

the class ReflectiveConstructorResolver method resolve.

/**
 * Locate a constructor on the type. There are three kinds of match that might occur:
 * <ol>
 * <li>An exact match where the types of the arguments match the types of the constructor
 * <li>An in-exact match where the types we are looking for are subtypes of those defined on the constructor
 * <li>A match where we are able to convert the arguments into those expected by the constructor, according to the
 * registered type converter.
 * </ol>
 */
@Override
@Nullable
public ConstructorExecutor resolve(EvaluationContext context, String typeName, List<TypeDescriptor> argumentTypes) throws AccessException {
    try {
        TypeConverter typeConverter = context.getTypeConverter();
        Class<?> type = context.getTypeLocator().findType(typeName);
        Constructor<?>[] ctors = type.getConstructors();
        Arrays.sort(ctors, Comparator.comparingInt(Constructor::getParameterCount));
        Constructor<?> closeMatch = null;
        Constructor<?> matchRequiringConversion = null;
        for (Constructor<?> ctor : ctors) {
            int paramCount = ctor.getParameterCount();
            List<TypeDescriptor> paramDescriptors = new ArrayList<>(paramCount);
            for (int i = 0; i < paramCount; i++) {
                paramDescriptors.add(new TypeDescriptor(new MethodParameter(ctor, i)));
            }
            ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
            if (ctor.isVarArgs() && argumentTypes.size() >= paramCount - 1) {
                // *sigh* complicated
                // Basically.. we have to have all parameters match up until the varargs one, then the rest of what is
                // being provided should be
                // the same type whilst the final argument to the method must be an array of that (oh, how easy...not) -
                // or the final parameter
                // we are supplied does match exactly (it is an array already).
                matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
            } else if (paramCount == argumentTypes.size()) {
                // worth a closer look
                matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
            }
            if (matchInfo != null) {
                if (matchInfo.isExactMatch()) {
                    return new ReflectiveConstructorExecutor(ctor);
                } else if (matchInfo.isCloseMatch()) {
                    closeMatch = ctor;
                } else if (matchInfo.isMatchRequiringConversion()) {
                    matchRequiringConversion = ctor;
                }
            }
        }
        if (closeMatch != null) {
            return new ReflectiveConstructorExecutor(closeMatch);
        } else if (matchRequiringConversion != null) {
            return new ReflectiveConstructorExecutor(matchRequiringConversion);
        } else {
            return null;
        }
    } catch (EvaluationException ex) {
        throw new AccessException("Failed to resolve constructor", ex);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) EvaluationException(cn.taketoday.expression.EvaluationException) TypeConverter(cn.taketoday.expression.TypeConverter) AccessException(cn.taketoday.expression.AccessException) TypeDescriptor(cn.taketoday.core.TypeDescriptor) MethodParameter(cn.taketoday.core.MethodParameter) Nullable(cn.taketoday.lang.Nullable)

Example 5 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-infrastructure by TAKETODAY.

the class ReflectiveMethodResolver method resolve.

/**
 * Locate a method on a type. There are three kinds of match that might occur:
 * <ol>
 * <li>an exact match where the types of the arguments match the types of the constructor
 * <li>an in-exact match where the types we are looking for are subtypes of those defined on the constructor
 * <li>a match where we are able to convert the arguments into those expected by the constructor,
 * according to the registered type converter
 * </ol>
 */
@Override
@Nullable
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> argumentTypes) throws AccessException {
    try {
        TypeConverter typeConverter = context.getTypeConverter();
        Class<?> type = (targetObject instanceof Class ? (Class<?>) targetObject : targetObject.getClass());
        ArrayList<Method> methods = new ArrayList<>(getMethods(type, targetObject));
        // If a filter is registered for this type, call it
        MethodFilter filter = (this.filters != null ? this.filters.get(type) : null);
        if (filter != null) {
            List<Method> filtered = filter.filter(methods);
            methods = (filtered instanceof ArrayList ? (ArrayList<Method>) filtered : new ArrayList<>(filtered));
        }
        // Sort methods into a sensible order
        if (methods.size() > 1) {
            methods.sort((m1, m2) -> {
                int m1pl = m1.getParameterCount();
                int m2pl = m2.getParameterCount();
                // vararg methods go last
                if (m1pl == m2pl) {
                    if (!m1.isVarArgs() && m2.isVarArgs()) {
                        return -1;
                    } else if (m1.isVarArgs() && !m2.isVarArgs()) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
                return Integer.compare(m1pl, m2pl);
            });
        }
        // Resolve any bridge methods
        for (int i = 0; i < methods.size(); i++) {
            methods.set(i, BridgeMethodResolver.findBridgedMethod(methods.get(i)));
        }
        // Remove duplicate methods (possible due to resolved bridge methods)
        Set<Method> methodsToIterate = new LinkedHashSet<>(methods);
        Method closeMatch = null;
        int closeMatchDistance = Integer.MAX_VALUE;
        Method matchRequiringConversion = null;
        boolean multipleOptions = false;
        for (Method method : methodsToIterate) {
            if (method.getName().equals(name)) {
                int paramCount = method.getParameterCount();
                List<TypeDescriptor> paramDescriptors = new ArrayList<>(paramCount);
                for (int i = 0; i < paramCount; i++) {
                    paramDescriptors.add(new TypeDescriptor(new MethodParameter(method, i)));
                }
                ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
                if (method.isVarArgs() && argumentTypes.size() >= (paramCount - 1)) {
                    // *sigh* complicated
                    matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
                } else if (paramCount == argumentTypes.size()) {
                    // Name and parameter number match, check the arguments
                    matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
                }
                if (matchInfo != null) {
                    if (matchInfo.isExactMatch()) {
                        return new ReflectiveMethodExecutor(method, type);
                    } else if (matchInfo.isCloseMatch()) {
                        if (this.useDistance) {
                            int matchDistance = ReflectionHelper.getTypeDifferenceWeight(paramDescriptors, argumentTypes);
                            if (closeMatch == null || matchDistance < closeMatchDistance) {
                                // This is a better match...
                                closeMatch = method;
                                closeMatchDistance = matchDistance;
                            }
                        } else {
                            // Take this as a close match if there isn't one already
                            if (closeMatch == null) {
                                closeMatch = method;
                            }
                        }
                    } else if (matchInfo.isMatchRequiringConversion()) {
                        if (matchRequiringConversion != null) {
                            multipleOptions = true;
                        }
                        matchRequiringConversion = method;
                    }
                }
            }
        }
        if (closeMatch != null) {
            return new ReflectiveMethodExecutor(closeMatch, type);
        } else if (matchRequiringConversion != null) {
            if (multipleOptions) {
                throw new SpelEvaluationException(SpelMessage.MULTIPLE_POSSIBLE_METHODS, name);
            }
            return new ReflectiveMethodExecutor(matchRequiringConversion, type);
        } else {
            return null;
        }
    } catch (EvaluationException ex) {
        throw new AccessException("Failed to resolve method", ex);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) MethodFilter(cn.taketoday.expression.MethodFilter) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) EvaluationException(cn.taketoday.expression.EvaluationException) SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) TypeConverter(cn.taketoday.expression.TypeConverter) AccessException(cn.taketoday.expression.AccessException) TypeDescriptor(cn.taketoday.core.TypeDescriptor) MethodParameter(cn.taketoday.core.MethodParameter) Nullable(cn.taketoday.lang.Nullable)

Aggregations

MethodParameter (cn.taketoday.core.MethodParameter)97 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)43 Method (java.lang.reflect.Method)43 Test (org.junit.jupiter.api.Test)42 ArrayList (java.util.ArrayList)30 HandlerMethod (cn.taketoday.web.handler.method.HandlerMethod)28 MappingJackson2HttpMessageConverter (cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter)24 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)23 ByteArrayHttpMessageConverter (cn.taketoday.http.converter.ByteArrayHttpMessageConverter)22 StringHttpMessageConverter (cn.taketoday.http.converter.StringHttpMessageConverter)22 Nullable (cn.taketoday.lang.Nullable)21 TypeDescriptor (cn.taketoday.core.TypeDescriptor)18 AllEncompassingFormHttpMessageConverter (cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter)17 ResourceHttpMessageConverter (cn.taketoday.http.converter.ResourceHttpMessageConverter)17 MappingJackson2XmlHttpMessageConverter (cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter)17 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)9 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 DependencyDescriptor (cn.taketoday.beans.factory.config.DependencyDescriptor)8 SynthesizingMethodParameter (cn.taketoday.core.annotation.SynthesizingMethodParameter)8