Package org.springframework.core.convert

Examples of org.springframework.core.convert.TypeDescriptor


    return new TypedValue(newArray);
  }

  private void populateReferenceTypeArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
      InlineList initializer, Class<?> componentType) {
    TypeDescriptor toTypeDescriptor = TypeDescriptor.valueOf(componentType);
    Object[] newObjectArray = (Object[]) newArray;
    for (int i = 0; i < newObjectArray.length; i++) {
      SpelNode elementNode = initializer.getChild(i);
      Object arrayEntry = elementNode.getValue(state);
      newObjectArray[i] = typeConverter.convertValue(arrayEntry, TypeDescriptor.forObject(arrayEntry), toTypeDescriptor);
View Full Code Here


    sb.append("(");
    for (int i = 0; i < argumentTypes.size(); i++) {
      if (i > 0) {
        sb.append(",");
      }
      TypeDescriptor typeDescriptor = argumentTypes.get(i);
      if (typeDescriptor != null) {
        sb.append(formatClassNameForMessage(typeDescriptor.getType()));
      }
      else {
        sb.append(formatClassNameForMessage(null));       
      }
    }
View Full Code Here

  }

  public TypeDescriptor getPropertyTypeDescriptor(String propertyName) throws BeansException {
    Field field = this.fieldMap.get(propertyName);
    if (field != null) {
      return new TypeDescriptor(field);
    }
    return null;
  }
View Full Code Here

    Object oldValue = null;
    try {
      ReflectionUtils.makeAccessible(field);
      oldValue = field.get(this.target);
      Object convertedValue = this.typeConverterDelegate.convertIfNecessary(
          field.getName(), oldValue, newValue, field.getType(), new TypeDescriptor(field));
      field.set(this.target, convertedValue);
    }
    catch (ConverterNotFoundException ex) {
      PropertyChangeEvent pce = new PropertyChangeEvent(this.target, propertyName, oldValue, newValue);
      throw new ConversionNotSupportedException(pce, field.getType(), ex);
View Full Code Here

  @Override
  protected ValueRef getValueRef(ExpressionState state) throws EvaluationException {
    TypedValue context = state.getActiveContextObject();
    Object targetObject = context.getValue();
    TypeDescriptor targetObjectTypeDescriptor = context.getTypeDescriptor();
    TypedValue indexValue = null;
    Object index = null;

    // This first part of the if clause prevents a 'double dereference' of
    // the property (SPR-5847)
    if (targetObject instanceof Map && (this.children[0] instanceof PropertyOrFieldReference)) {
      PropertyOrFieldReference reference = (PropertyOrFieldReference) this.children[0];
      index = reference.getName();
      indexValue = new TypedValue(index);
    }
    else {
      // In case the map key is unqualified, we want it evaluated against
      // the root object so temporarily push that on whilst evaluating the key
      try {
        state.pushActiveContextObject(state.getRootContextObject());
        indexValue = this.children[0].getValueInternal(state);
        index = indexValue.getValue();
      }
      finally {
        state.popActiveContextObject();
      }
    }

    // Indexing into a Map
    if (targetObject instanceof Map) {
      Object key = index;
      if (targetObjectTypeDescriptor.getMapKeyTypeDescriptor() != null) {
        key = state.convertValue(key, targetObjectTypeDescriptor.getMapKeyTypeDescriptor());
      }
      return new MapIndexingValueRef(state.getTypeConverter(), (Map<?, ?>) targetObject, key,
          targetObjectTypeDescriptor);
    }

    if (targetObject == null) {
      throw new SpelEvaluationException(getStartPosition(), SpelMessage.CANNOT_INDEX_INTO_NULL_VALUE);
    }

    // if the object is something that looks indexable by an integer,
    // attempt to treat the index value as a number
    if (targetObject.getClass().isArray() || targetObject instanceof Collection || targetObject instanceof String) {
      int idx = (Integer) state.convertValue(index, TypeDescriptor.valueOf(Integer.class));
      if (targetObject.getClass().isArray()) {
        return new ArrayIndexingValueRef(state.getTypeConverter(), targetObject, idx, targetObjectTypeDescriptor);
      }
      else if (targetObject instanceof Collection) {
        return new CollectionIndexingValueRef((Collection<?>) targetObject, idx, targetObjectTypeDescriptor,
            state.getTypeConverter(), state.getConfiguration().isAutoGrowCollections());
      }
      else if (targetObject instanceof String) {
        return new StringIndexingLValue((String) targetObject, idx, targetObjectTypeDescriptor);
      }
    }

    // Try and treat the index value as a property of the context object
    // TODO could call the conversion service to convert the value to a String
    if (indexValue.getTypeDescriptor().getType() == String.class) {
      return new PropertyIndexingValueRef(targetObject, (String) indexValue.getValue(),
          state.getEvaluationContext(), targetObjectTypeDescriptor);
    }

    throw new SpelEvaluationException(getStartPosition(), SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE,
        targetObjectTypeDescriptor.toString());
  }
View Full Code Here

   */
  private void growCollection(TypeDescriptor targetType, int index, Collection<Object> collection) {
    if (targetType.getElementTypeDescriptor() == null) {
      throw new SpelEvaluationException(getStartPosition(), SpelMessage.UNABLE_TO_GROW_COLLECTION_UNKNOWN_ELEMENT_TYPE);
    }
    TypeDescriptor elementType = targetType.getElementTypeDescriptor();
    Object newCollectionElement;
    try {
      int newElements = index - collection.size();
      while (newElements > 0) {
        collection.add(elementType.getType().newInstance());
        newElements--;
      }
      newCollectionElement = elementType.getType().newInstance();
    }
    catch (Exception ex) {
      throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.UNABLE_TO_GROW_COLLECTION);
    }
    collection.add(newCollectionElement);
View Full Code Here

          if (pd.getReadMethod() != null || pd.getWriteMethod() != null) {
            return TypeDescriptor.nested(property(pd), tokens.keys.length);
          }
        } else {
          if (pd.getReadMethod() != null || pd.getWriteMethod() != null) {
            return new TypeDescriptor(property(pd));
          }
        }
      }
    }
    catch (InvalidPropertyException ex) {
View Full Code Here

  }

  private Object convertForProperty(String propertyName, Object oldValue, Object newValue, PropertyDescriptor pd)
      throws TypeMismatchException {

    return convertIfNecessary(propertyName, oldValue, newValue, pd.getPropertyType(), new TypeDescriptor(property(pd)));
  }
View Full Code Here

          else if (value instanceof Map) {
            Map map = (Map) value;
            Class<?> mapKeyType = GenericCollectionTypeResolver.getMapKeyReturnType(pd.getReadMethod(), i + 1);
            // IMPORTANT: Do not pass full property name in here - property editors
            // must not kick in for map keys but rather only for map values.
            TypeDescriptor typeDescriptor = mapKeyType != null ? TypeDescriptor.valueOf(mapKeyType) : TypeDescriptor.valueOf(Object.class);
            Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType, typeDescriptor);
            value = map.get(convertedMapKey);
          }
          else {
            throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName,
View Full Code Here

        Class mapValueType = GenericCollectionTypeResolver.getMapValueReturnType(
            pd.getReadMethod(), tokens.keys.length);
        Map map = (Map) propValue;
        // IMPORTANT: Do not pass full property name in here - property editors
        // must not kick in for map keys but rather only for map values.
        TypeDescriptor typeDescriptor = (mapKeyType != null ?
            TypeDescriptor.valueOf(mapKeyType) : TypeDescriptor.valueOf(Object.class));
        Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType, typeDescriptor);
        Object oldValue = null;
        if (isExtractOldValueForEditor()) {
          oldValue = map.get(convertedMapKey);
View Full Code Here

TOP

Related Classes of org.springframework.core.convert.TypeDescriptor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.