Search in sources :

Example 11 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class TypeDescriptorTests method parameterMap.

@Test
public void parameterMap() throws Exception {
    MethodParameter methodParameter = new MethodParameter(getClass().getMethod("testParameterMap", Map.class), 0);
    TypeDescriptor desc = new TypeDescriptor(methodParameter);
    assertEquals(Map.class, desc.getType());
    assertEquals(Map.class, desc.getObjectType());
    assertEquals("java.util.Map", desc.getName());
    assertEquals("java.util.Map<java.lang.Integer, java.util.List<java.lang.String>>", desc.toString());
    assertTrue(!desc.isPrimitive());
    assertEquals(0, desc.getAnnotations().length);
    assertFalse(desc.isCollection());
    assertFalse(desc.isArray());
    assertTrue(desc.isMap());
    assertEquals(TypeDescriptor.nested(methodParameter, 1), desc.getMapValueTypeDescriptor());
    assertEquals(TypeDescriptor.nested(methodParameter, 2), desc.getMapValueTypeDescriptor().getElementTypeDescriptor());
    assertEquals(Integer.class, desc.getMapKeyTypeDescriptor().getType());
    assertEquals(List.class, desc.getMapValueTypeDescriptor().getType());
    assertEquals(String.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getType());
}
Also used : MethodParameter(org.springframework.core.MethodParameter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Example 12 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class TypeDescriptorTests method equality.

@Test
public void equality() throws Exception {
    TypeDescriptor t1 = TypeDescriptor.valueOf(String.class);
    TypeDescriptor t2 = TypeDescriptor.valueOf(String.class);
    TypeDescriptor t3 = TypeDescriptor.valueOf(Date.class);
    TypeDescriptor t4 = TypeDescriptor.valueOf(Date.class);
    TypeDescriptor t5 = TypeDescriptor.valueOf(List.class);
    TypeDescriptor t6 = TypeDescriptor.valueOf(List.class);
    TypeDescriptor t7 = TypeDescriptor.valueOf(Map.class);
    TypeDescriptor t8 = TypeDescriptor.valueOf(Map.class);
    assertEquals(t1, t2);
    assertEquals(t3, t4);
    assertEquals(t5, t6);
    assertEquals(t7, t8);
    TypeDescriptor t9 = new TypeDescriptor(getClass().getField("listField"));
    TypeDescriptor t10 = new TypeDescriptor(getClass().getField("listField"));
    assertEquals(t9, t10);
    TypeDescriptor t11 = new TypeDescriptor(getClass().getField("mapField"));
    TypeDescriptor t12 = new TypeDescriptor(getClass().getField("mapField"));
    assertEquals(t11, t12);
    MethodParameter testAnnotatedMethod = new MethodParameter(getClass().getMethod("testAnnotatedMethod", String.class), 0);
    TypeDescriptor t13 = new TypeDescriptor(testAnnotatedMethod);
    TypeDescriptor t14 = new TypeDescriptor(testAnnotatedMethod);
    assertEquals(t13, t14);
    TypeDescriptor t15 = new TypeDescriptor(testAnnotatedMethod);
    TypeDescriptor t16 = new TypeDescriptor(new MethodParameter(getClass().getMethod("testAnnotatedMethodDifferentAnnotationValue", String.class), 0));
    assertNotEquals(t15, t16);
    TypeDescriptor t17 = new TypeDescriptor(testAnnotatedMethod);
    TypeDescriptor t18 = new TypeDescriptor(new MethodParameter(getClass().getMethod("test5", String.class), 0));
    assertNotEquals(t17, t18);
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 13 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class DefaultConversionServiceTests method testSpr7766.

@Test
public void testSpr7766() throws Exception {
    ConverterRegistry registry = (conversionService);
    registry.addConverter(new ColorConverter());
    @SuppressWarnings("unchecked") List<Color> colors = (List<Color>) conversionService.convert(new String[] { "ffffff", "#000000" }, TypeDescriptor.valueOf(String[].class), new TypeDescriptor(new MethodParameter(getClass().getMethod("handlerMethod", List.class), 0)));
    assertEquals(2, colors.size());
    assertEquals(Color.WHITE, colors.get(0));
    assertEquals(Color.BLACK, colors.get(1));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Color(java.awt.Color) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 14 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class FunctionReference method executeFunctionJLRMethod.

/**
	 * Execute a function represented as a java.lang.reflect.Method.
	 * @param state the expression evaluation state
	 * @param method the method to invoke
	 * @return the return value of the invoked Java method
	 * @throws EvaluationException if there is any problem invoking the method
	 */
private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException {
    this.method = null;
    Object[] functionArgs = getArguments(state);
    if (!method.isVarArgs() && method.getParameterCount() != functionArgs.length) {
        throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, functionArgs.length, method.getParameterCount());
    }
    // Only static methods can be called in this way
    if (!Modifier.isStatic(method.getModifiers())) {
        throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_MUST_BE_STATIC, ClassUtils.getQualifiedMethodName(method), this.name);
    }
    argumentConversionOccurred = false;
    // Convert arguments if necessary and remap them for varargs if required
    if (functionArgs != null) {
        TypeConverter converter = state.getEvaluationContext().getTypeConverter();
        argumentConversionOccurred = ReflectionHelper.convertAllArguments(converter, functionArgs, method);
    }
    if (method.isVarArgs()) {
        functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(method.getParameterTypes(), functionArgs);
    }
    try {
        ReflectionUtils.makeAccessible(method);
        Object result = method.invoke(method.getClass(), functionArgs);
        if (!argumentConversionOccurred) {
            this.method = method;
            this.exitTypeDescriptor = CodeFlow.toDescriptor(method.getReturnType());
        }
        return new TypedValue(result, new TypeDescriptor(new MethodParameter(method, -1)).narrow(result));
    } catch (Exception ex) {
        throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL, this.name, ex.getMessage());
    }
}
Also used : TypeConverter(org.springframework.expression.TypeConverter) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) MethodParameter(org.springframework.core.MethodParameter) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) EvaluationException(org.springframework.expression.EvaluationException) TypedValue(org.springframework.expression.TypedValue)

Example 15 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class ReflectionHelper method convertArguments.

/**
	 * Takes an input set of argument values and converts them to the types specified as the
	 * required parameter types. The arguments are converted 'in-place' in the input array.
	 * @param converter the type converter to use for attempting conversions
	 * @param arguments the actual arguments that need conversion
	 * @param executable the target Method or Constructor
	 * @param varargsPosition the known position of the varargs argument, if any
	 * ({@code null} if not varargs)
	 * @return {@code true} if some kind of conversion occurred on an argument
	 * @throws EvaluationException if a problem occurs during conversion
	 */
static boolean convertArguments(TypeConverter converter, Object[] arguments, Executable executable, Integer varargsPosition) throws EvaluationException {
    boolean conversionOccurred = false;
    if (varargsPosition == null) {
        for (int i = 0; i < arguments.length; i++) {
            TypeDescriptor targetType = new TypeDescriptor(MethodParameter.forExecutable(executable, i));
            Object argument = arguments[i];
            arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
            conversionOccurred |= (argument != arguments[i]);
        }
    } else {
        // Convert everything up to the varargs position
        for (int i = 0; i < varargsPosition; i++) {
            TypeDescriptor targetType = new TypeDescriptor(MethodParameter.forExecutable(executable, i));
            Object argument = arguments[i];
            arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
            conversionOccurred |= (argument != arguments[i]);
        }
        MethodParameter methodParam = MethodParameter.forExecutable(executable, varargsPosition);
        if (varargsPosition == arguments.length - 1) {
            // If the target is varargs and there is just one more argument
            // then convert it here
            TypeDescriptor targetType = new TypeDescriptor(methodParam);
            Object argument = arguments[varargsPosition];
            TypeDescriptor sourceType = TypeDescriptor.forObject(argument);
            arguments[varargsPosition] = converter.convertValue(argument, sourceType, targetType);
            // 3) the input argument was the wrong type and got converted and put into an array
            if (argument != arguments[varargsPosition] && !isFirstEntryInArray(argument, arguments[varargsPosition])) {
                // case 3
                conversionOccurred = true;
            }
        } else {
            // Convert remaining arguments to the varargs element type
            TypeDescriptor targetType = new TypeDescriptor(methodParam).getElementTypeDescriptor();
            for (int i = varargsPosition; i < arguments.length; i++) {
                Object argument = arguments[i];
                arguments[i] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
                conversionOccurred |= (argument != arguments[i]);
            }
        }
    }
    return conversionOccurred;
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) MethodParameter(org.springframework.core.MethodParameter)

Aggregations

MethodParameter (org.springframework.core.MethodParameter)320 Test (org.junit.Test)251 Method (java.lang.reflect.Method)64 ArrayList (java.util.ArrayList)35 RequestParam (org.springframework.web.bind.annotation.RequestParam)30 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)27 HandlerMethod (org.springframework.web.method.HandlerMethod)27 Before (org.junit.Before)25 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)25 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)23 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)23 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)23 ResolvableType (org.springframework.core.ResolvableType)21 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)21 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)20 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)20 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)20 Mono (reactor.core.publisher.Mono)19