Search in sources :

Example 96 with TypeDescriptor

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

the class DefaultConversionServiceTests method convertStringToCollectionWithElementConversion.

@Test
@SuppressWarnings("rawtypes")
public void convertStringToCollectionWithElementConversion() throws Exception {
    List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericList")));
    assertEquals(3, result.size());
    assertEquals(1, result.get(0));
    assertEquals(2, result.get(1));
    assertEquals(3, result.get(2));
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Test(org.junit.Test)

Example 97 with TypeDescriptor

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

the class ReflectiveMethodExecutor method execute.

@Override
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
    try {
        if (arguments != null) {
            this.argumentConversionOccurred = ReflectionHelper.convertArguments(context.getTypeConverter(), arguments, this.method, this.varargsPosition);
        }
        if (this.method.isVarArgs()) {
            arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(this.method.getParameterTypes(), arguments);
        }
        ReflectionUtils.makeAccessible(this.method);
        Object value = this.method.invoke(target, arguments);
        return new TypedValue(value, new TypeDescriptor(new MethodParameter(this.method, -1)).narrow(value));
    } catch (Exception ex) {
        throw new AccessException("Problem invoking method: " + this.method, ex);
    }
}
Also used : AccessException(org.springframework.expression.AccessException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) MethodParameter(org.springframework.core.MethodParameter) AccessException(org.springframework.expression.AccessException) TypedValue(org.springframework.expression.TypedValue)

Example 98 with TypeDescriptor

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

the class ReflectivePropertyAccessor method write.

@Override
public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
    if (target == null) {
        throw new AccessException("Cannot write property on null target");
    }
    Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());
    Object possiblyConvertedNewValue = newValue;
    TypeDescriptor typeDescriptor = getTypeDescriptor(context, target, name);
    if (typeDescriptor != null) {
        try {
            possiblyConvertedNewValue = context.getTypeConverter().convertValue(newValue, TypeDescriptor.forObject(newValue), typeDescriptor);
        } catch (EvaluationException evaluationException) {
            throw new AccessException("Type conversion failure", evaluationException);
        }
    }
    PropertyCacheKey cacheKey = new PropertyCacheKey(type, name, target instanceof Class);
    Member cachedMember = this.writerCache.get(cacheKey);
    if (cachedMember == null || cachedMember instanceof Method) {
        Method method = (Method) cachedMember;
        if (method == null) {
            method = findSetterForProperty(name, type, target);
            if (method != null) {
                cachedMember = method;
                this.writerCache.put(cacheKey, cachedMember);
            }
        }
        if (method != null) {
            try {
                ReflectionUtils.makeAccessible(method);
                method.invoke(target, possiblyConvertedNewValue);
                return;
            } catch (Exception ex) {
                throw new AccessException("Unable to access property '" + name + "' through setter method", ex);
            }
        }
    }
    if (cachedMember == null || cachedMember instanceof Field) {
        Field field = (Field) cachedMember;
        if (field == null) {
            field = findField(name, type, target);
            if (field != null) {
                cachedMember = field;
                this.writerCache.put(cacheKey, cachedMember);
            }
        }
        if (field != null) {
            try {
                ReflectionUtils.makeAccessible(field);
                field.set(target, possiblyConvertedNewValue);
                return;
            } catch (Exception ex) {
                throw new AccessException("Unable to access field '" + name + "'", ex);
            }
        }
    }
    throw new AccessException("Neither setter method nor field found for property '" + name + "'");
}
Also used : Field(java.lang.reflect.Field) AccessException(org.springframework.expression.AccessException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) EvaluationException(org.springframework.expression.EvaluationException) Method(java.lang.reflect.Method) Member(java.lang.reflect.Member) EvaluationException(org.springframework.expression.EvaluationException) AccessException(org.springframework.expression.AccessException)

Example 99 with TypeDescriptor

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

the class ReflectivePropertyAccessor method canWrite.

@Override
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
    if (target == null) {
        return false;
    }
    Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());
    PropertyCacheKey cacheKey = new PropertyCacheKey(type, name, target instanceof Class);
    if (this.writerCache.containsKey(cacheKey)) {
        return true;
    }
    Method method = findSetterForProperty(name, type, target);
    if (method != null) {
        // Treat it like a property
        Property property = new Property(type, null, method);
        TypeDescriptor typeDescriptor = new TypeDescriptor(property);
        this.writerCache.put(cacheKey, method);
        this.typeDescriptorCache.put(cacheKey, typeDescriptor);
        return true;
    } else {
        Field field = findField(name, type, target);
        if (field != null) {
            this.writerCache.put(cacheKey, field);
            this.typeDescriptorCache.put(cacheKey, new TypeDescriptor(field));
            return true;
        }
    }
    return false;
}
Also used : Field(java.lang.reflect.Field) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Method(java.lang.reflect.Method) Property(org.springframework.core.convert.Property)

Example 100 with TypeDescriptor

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

the class ReflectivePropertyAccessor method canRead.

@Override
public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException {
    if (target == null) {
        return false;
    }
    Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());
    if (type.isArray() && name.equals("length")) {
        return true;
    }
    PropertyCacheKey cacheKey = new PropertyCacheKey(type, name, target instanceof Class);
    if (this.readerCache.containsKey(cacheKey)) {
        return true;
    }
    Method method = findGetterForProperty(name, type, target);
    if (method != null) {
        // Treat it like a property...
        // The readerCache will only contain gettable properties (let's not worry about setters for now).
        Property property = new Property(type, method, null);
        TypeDescriptor typeDescriptor = new TypeDescriptor(property);
        this.readerCache.put(cacheKey, new InvokerPair(method, typeDescriptor));
        this.typeDescriptorCache.put(cacheKey, typeDescriptor);
        return true;
    } else {
        Field field = findField(name, type, target);
        if (field != null) {
            TypeDescriptor typeDescriptor = new TypeDescriptor(field);
            this.readerCache.put(cacheKey, new InvokerPair(field, typeDescriptor));
            this.typeDescriptorCache.put(cacheKey, typeDescriptor);
            return true;
        }
    }
    return false;
}
Also used : Field(java.lang.reflect.Field) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Method(java.lang.reflect.Method) Property(org.springframework.core.convert.Property)

Aggregations

TypeDescriptor (org.springframework.core.convert.TypeDescriptor)114 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