Search in sources :

Example 11 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-boot by spring-projects.

the class RelaxedDataBinder method extendMapIfNecessary.

private void extendMapIfNecessary(BeanWrapper wrapper, BeanPath path, int index) {
    String name = path.prefix(index);
    TypeDescriptor parent = wrapper.getPropertyTypeDescriptor(name);
    if (parent == null) {
        return;
    }
    TypeDescriptor descriptor = parent.getMapValueTypeDescriptor();
    if (descriptor == null) {
        descriptor = TypeDescriptor.valueOf(Object.class);
    }
    if (!descriptor.isMap() && !descriptor.isCollection() && !descriptor.getType().equals(Object.class)) {
        return;
    }
    String extensionName = path.prefix(index + 1);
    if (wrapper.isReadableProperty(extensionName)) {
        Object currentValue = wrapper.getPropertyValue(extensionName);
        if ((descriptor.isCollection() && currentValue instanceof Collection) || (!descriptor.isCollection() && currentValue instanceof Map)) {
            return;
        }
    }
    Object extend = new LinkedHashMap<String, Object>();
    if (descriptor.isCollection()) {
        extend = new ArrayList<>();
    }
    if (descriptor.getType().equals(Object.class) && path.isLastNode(index)) {
        extend = BLANK;
    }
    wrapper.setPropertyValue(extensionName, extend);
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Collection(java.util.Collection) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) LinkedHashMap(java.util.LinkedHashMap)

Example 12 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-boot by spring-projects.

the class RelaxedDataBinder method extendCollectionIfNecessary.

private void extendCollectionIfNecessary(BeanWrapper wrapper, BeanPath path, int index) {
    String name = path.prefix(index);
    TypeDescriptor elementDescriptor = wrapper.getPropertyTypeDescriptor(name).getElementTypeDescriptor();
    if (!elementDescriptor.isMap() && !elementDescriptor.isCollection() && !elementDescriptor.getType().equals(Object.class)) {
        return;
    }
    Object extend = new LinkedHashMap<String, Object>();
    if (!elementDescriptor.isMap() && path.isArrayIndex(index)) {
        extend = new ArrayList<>();
    }
    wrapper.setPropertyValue(path.prefix(index + 1), extend);
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with TypeDescriptor

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

the class AbstractPropertyBindingResult method findEditor.

/**
	 * This implementation exposes a PropertyEditor adapter for a Formatter,
	 * if applicable.
	 */
@Override
public PropertyEditor findEditor(String field, Class<?> valueType) {
    Class<?> valueTypeForLookup = valueType;
    if (valueTypeForLookup == null) {
        valueTypeForLookup = getFieldType(field);
    }
    PropertyEditor editor = super.findEditor(field, valueTypeForLookup);
    if (editor == null && this.conversionService != null) {
        TypeDescriptor td = null;
        if (field != null) {
            TypeDescriptor ptd = getPropertyAccessor().getPropertyTypeDescriptor(fixedField(field));
            if (valueType == null || valueType.isAssignableFrom(ptd.getType())) {
                td = ptd;
            }
        }
        if (td == null) {
            td = TypeDescriptor.valueOf(valueTypeForLookup);
        }
        if (this.conversionService.canConvert(TypeDescriptor.valueOf(String.class), td)) {
            editor = new ConvertingPropertyEditorAdapter(this.conversionService, td);
        }
    }
    return editor;
}
Also used : ConvertingPropertyEditorAdapter(org.springframework.core.convert.support.ConvertingPropertyEditorAdapter) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) PropertyEditor(java.beans.PropertyEditor)

Example 14 with TypeDescriptor

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

the class ConstructorReference method createNewInstance.

/**
	 * Create a new ordinary object and return it.
	 * @param state the expression state within which this expression is being evaluated
	 * @return the new object
	 * @throws EvaluationException if there is a problem creating the object
	 */
private TypedValue createNewInstance(ExpressionState state) throws EvaluationException {
    Object[] arguments = new Object[getChildCount() - 1];
    List<TypeDescriptor> argumentTypes = new ArrayList<>(getChildCount() - 1);
    for (int i = 0; i < arguments.length; i++) {
        TypedValue childValue = this.children[i + 1].getValueInternal(state);
        Object value = childValue.getValue();
        arguments[i] = value;
        argumentTypes.add(TypeDescriptor.forObject(value));
    }
    ConstructorExecutor executorToUse = this.cachedExecutor;
    if (executorToUse != null) {
        try {
            return executorToUse.execute(state.getEvaluationContext(), arguments);
        } catch (AccessException ex) {
            // Otherwise the constructor could not be invoked.
            if (ex.getCause() instanceof InvocationTargetException) {
                // User exception was the root cause - exit now
                Throwable rootCause = ex.getCause().getCause();
                if (rootCause instanceof RuntimeException) {
                    throw (RuntimeException) rootCause;
                } else {
                    String typeName = (String) this.children[0].getValueInternal(state).getValue();
                    throw new SpelEvaluationException(getStartPosition(), rootCause, SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typeName, FormatHelper.formatMethodForMessage("", argumentTypes));
                }
            }
            // At this point we know it wasn't a user problem so worth a retry if a better candidate can be found
            this.cachedExecutor = null;
        }
    }
    // Either there was no accessor or it no longer exists
    String typeName = (String) this.children[0].getValueInternal(state).getValue();
    executorToUse = findExecutorForConstructor(typeName, argumentTypes, state);
    try {
        this.cachedExecutor = executorToUse;
        if (this.cachedExecutor instanceof ReflectiveConstructorExecutor) {
            this.exitTypeDescriptor = CodeFlow.toDescriptor(((ReflectiveConstructorExecutor) this.cachedExecutor).getConstructor().getDeclaringClass());
        }
        return executorToUse.execute(state.getEvaluationContext(), arguments);
    } catch (AccessException ex) {
        throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typeName, FormatHelper.formatMethodForMessage("", argumentTypes));
    }
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) ReflectiveConstructorExecutor(org.springframework.expression.spel.support.ReflectiveConstructorExecutor) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) AccessException(org.springframework.expression.AccessException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ReflectiveConstructorExecutor(org.springframework.expression.spel.support.ReflectiveConstructorExecutor) ConstructorExecutor(org.springframework.expression.ConstructorExecutor) TypedValue(org.springframework.expression.TypedValue)

Example 15 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor 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)

Aggregations

TypeDescriptor (org.springframework.core.convert.TypeDescriptor)115 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)35 List (java.util.List)20 Map (java.util.Map)16 HashMap (java.util.HashMap)14 LinkedHashMap (java.util.LinkedHashMap)13 LinkedList (java.util.LinkedList)12 MethodParameter (org.springframework.core.MethodParameter)12 Collection (java.util.Collection)11 ConversionFailedException (org.springframework.core.convert.ConversionFailedException)10 Method (java.lang.reflect.Method)9 AccessException (org.springframework.expression.AccessException)9 ConverterNotFoundException (org.springframework.core.convert.ConverterNotFoundException)8 TypedValue (org.springframework.expression.TypedValue)8 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)8 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)8 MultiValueMap (org.springframework.util.MultiValueMap)8 AbstractList (java.util.AbstractList)7 Field (java.lang.reflect.Field)6