Example usage for org.springframework.core.annotation SynthesizingMethodParameter SynthesizingMethodParameter

List of usage examples for org.springframework.core.annotation SynthesizingMethodParameter SynthesizingMethodParameter

Introduction

In this page you can find the example usage for org.springframework.core.annotation SynthesizingMethodParameter SynthesizingMethodParameter.

Prototype

public SynthesizingMethodParameter(Constructor<?> constructor, int parameterIndex) 

Source Link

Document

Create a new SynthesizingMethodParameter for the given constructor, with nesting level 1.

Usage

From source file:org.springframework.cloud.iot.coap.method.ResolvableMethod.java

/**
 * Return the declared return type of the resolved method.
 *///from w  w w  . j  av a  2  s . c om
public MethodParameter returnType() {
    return new SynthesizingMethodParameter(this.method, -1);
}

From source file:org.springframework.cloud.stream.reactive.StreamEmitterAnnotationBeanPostProcessor.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private void invokeSetupMethodOnToTargetChannel(Method method, Object bean, String outboundName) {
    Object[] arguments = new Object[method.getParameterCount()];
    Object targetBean = null;//from  w  w w .  jav  a2s  .c  o  m
    for (int parameterIndex = 0; parameterIndex < arguments.length; parameterIndex++) {
        MethodParameter methodParameter = new SynthesizingMethodParameter(method, parameterIndex);
        Class<?> parameterType = methodParameter.getParameterType();
        Object targetReferenceValue = null;
        if (methodParameter.hasParameterAnnotation(Output.class)) {
            targetReferenceValue = AnnotationUtils
                    .getValue(methodParameter.getParameterAnnotation(Output.class));
        } else if (arguments.length == 1 && StringUtils.hasText(outboundName)) {
            targetReferenceValue = outboundName;
        }
        if (targetReferenceValue != null) {
            targetBean = this.applicationContext.getBean((String) targetReferenceValue);
            for (StreamListenerParameterAdapter<?, Object> streamListenerParameterAdapter : this.streamListenerParameterAdapters) {
                if (streamListenerParameterAdapter.supports(targetBean.getClass(), methodParameter)) {
                    arguments[parameterIndex] = streamListenerParameterAdapter.adapt(targetBean,
                            methodParameter);
                    if (arguments[parameterIndex] instanceof FluxSender) {
                        closeableFluxResources.add((FluxSender) arguments[parameterIndex]);
                    }
                    break;
                }
            }
            Assert.notNull(arguments[parameterIndex], "Cannot convert argument " + parameterIndex + " of "
                    + method + "from " + targetBean.getClass() + " to " + parameterType);
        } else {
            throw new IllegalStateException(StreamEmitterErrorMessages.ATLEAST_ONE_OUTPUT);
        }
    }
    Object result;
    try {
        result = method.invoke(bean, arguments);
    } catch (Exception e) {
        throw new BeanInitializationException("Cannot setup StreamEmitter for " + method, e);
    }

    if (!Void.TYPE.equals(method.getReturnType())) {
        if (targetBean == null) {
            targetBean = this.applicationContext.getBean(outboundName);
        }
        boolean streamListenerResultAdapterFound = false;
        for (StreamListenerResultAdapter streamListenerResultAdapter : this.streamListenerResultAdapters) {
            if (streamListenerResultAdapter.supports(result.getClass(), targetBean.getClass())) {
                Closeable fluxDisposable = streamListenerResultAdapter.adapt(result, targetBean);
                closeableFluxResources.add(fluxDisposable);
                streamListenerResultAdapterFound = true;
                break;
            }
        }
        Assert.state(streamListenerResultAdapterFound,
                StreamEmitterErrorMessages.CANNOT_CONVERT_RETURN_TYPE_TO_ANY_AVAILABLE_RESULT_ADAPTERS);
    }
}

From source file:org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.java

private static UriComponents applyContributors(UriComponentsBuilder builder, Method method, Object... args) {
    CompositeUriComponentsContributor contributor = getConfiguredUriComponentsContributor();
    if (contributor == null) {
        logger.debug("Using default CompositeUriComponentsContributor");
        contributor = defaultUriComponentsContributor;
    }/* w ww  .  ja  v  a2 s.c  o m*/

    int paramCount = method.getParameterCount();
    int argCount = args.length;
    if (paramCount != argCount) {
        throw new IllegalArgumentException("Number of method parameters " + paramCount
                + " does not match number of argument values " + argCount);
    }

    final Map<String, Object> uriVars = new HashMap<>();
    for (int i = 0; i < paramCount; i++) {
        MethodParameter param = new SynthesizingMethodParameter(method, i);
        param.initParameterNameDiscovery(parameterNameDiscoverer);
        contributor.contributeMethodArgument(param, args[i], builder, uriVars);
    }

    // We may not have all URI var values, expand only what we have
    return builder.build().expand(new UriComponents.UriTemplateVariables() {
        @Override
        public Object getValue(@Nullable String name) {
            return uriVars.containsKey(name) ? uriVars.get(name)
                    : UriComponents.UriTemplateVariables.SKIP_VALUE;
        }
    });
}