Search in sources :

Example 1 with SynthesizingMethodParameter

use of cn.taketoday.core.annotation.SynthesizingMethodParameter 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 2 with SynthesizingMethodParameter

use of cn.taketoday.core.annotation.SynthesizingMethodParameter in project today-framework by TAKETODAY.

the class ModelAttributeMethodProcessorTests method setup.

@BeforeEach
public void setup() throws Throwable {
    this.request = new ServletRequestContext(null, new MockHttpServletRequest(), null);
    this.container = new BindingContext();
    request.setBindingContext(container);
    this.processor = new ModelAttributeMethodProcessor(false);
    Method method = ModelAttributeHandler.class.getDeclaredMethod("modelAttribute", TestBean.class, Errors.class, int.class, TestBean.class, TestBean.class, TestBean.class, TestBeanWithConstructorArgs.class);
    this.paramNamedValidModelAttr = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 0));
    this.paramErrors = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 1));
    this.paramInt = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 2));
    this.paramModelAttr = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 3));
    this.paramBindingDisabledAttr = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 4));
    this.paramNonSimpleType = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 5));
    this.beanWithConstructorArgs = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 6));
    method = getClass().getDeclaredMethod("annotatedReturnValue");
    this.returnParamNamedModelAttrHandler = new HandlerMethod(this, method);
    method = getClass().getDeclaredMethod("notAnnotatedReturnValue");
    this.returnParamNonSimpleTypeHandler = new HandlerMethod(this, method);
}
Also used : SynthesizingMethodParameter(cn.taketoday.core.annotation.SynthesizingMethodParameter) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) Method(java.lang.reflect.Method) BindingContext(cn.taketoday.web.BindingContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with SynthesizingMethodParameter

use of cn.taketoday.core.annotation.SynthesizingMethodParameter in project today-framework by TAKETODAY.

the class CookieMethodArgumentResolversTests method setUp.

@BeforeEach
public void setUp() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.refresh();
    CookieParameterResolver.register(strategies, context.getBeanFactory());
    ServletParameterResolvers.register(strategies, servletContext);
    Method method = getClass().getMethod("params", Cookie.class, String.class, String.class);
    paramNamedCookie = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 0));
    paramNamedDefaultValueString = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 1));
    paramString = new ResolvableMethodParameter(new SynthesizingMethodParameter(method, 2));
    request = new MockHttpServletRequest();
    webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
}
Also used : SynthesizingMethodParameter(cn.taketoday.core.annotation.SynthesizingMethodParameter) AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) Method(java.lang.reflect.Method) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with SynthesizingMethodParameter

use of cn.taketoday.core.annotation.SynthesizingMethodParameter in project today-framework by TAKETODAY.

the class RequestHeaderMethodArgumentResolverTests method getParameter.

@NonNull
private SynthesizingMethodParameter getParameter(Method method, int parameterIndex) {
    SynthesizingMethodParameter parameter = new SynthesizingMethodParameter(method, parameterIndex);
    parameter.initParameterNameDiscovery(discoverer);
    return parameter;
}
Also used : SynthesizingMethodParameter(cn.taketoday.core.annotation.SynthesizingMethodParameter) NonNull(cn.taketoday.lang.NonNull)

Example 5 with SynthesizingMethodParameter

use of cn.taketoday.core.annotation.SynthesizingMethodParameter in project today-framework by TAKETODAY.

the class ResolvableParameterFactory method createArray.

@Nullable
public ResolvableMethodParameter[] createArray(Method method) {
    final int length = method.getParameterCount();
    if (length == 0) {
        return null;
    }
    final ResolvableMethodParameter[] ret = new ResolvableMethodParameter[length];
    for (int i = 0; i < length; i++) {
        MethodParameter parameter = new SynthesizingMethodParameter(method, i);
        parameter.initParameterNameDiscovery(parameterNameDiscoverer);
        ret[i] = createParameter(parameter);
    }
    return ret;
}
Also used : SynthesizingMethodParameter(cn.taketoday.core.annotation.SynthesizingMethodParameter) SynthesizingMethodParameter(cn.taketoday.core.annotation.SynthesizingMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) Nullable(cn.taketoday.lang.Nullable)

Aggregations

SynthesizingMethodParameter (cn.taketoday.core.annotation.SynthesizingMethodParameter)9 MethodParameter (cn.taketoday.core.MethodParameter)4 Nullable (cn.taketoday.lang.Nullable)4 Method (java.lang.reflect.Method)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)3 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)3 DependencyDescriptor (cn.taketoday.beans.factory.config.DependencyDescriptor)2 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)2 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)2 AnnotationConfigApplicationContext (cn.taketoday.context.annotation.AnnotationConfigApplicationContext)1 NonNull (cn.taketoday.lang.NonNull)1 BindingContext (cn.taketoday.web.BindingContext)1