Package org.springframework.expression

Examples of org.springframework.expression.TypedValue


   * @throws EvaluationException if the condition does not evaluate correctly to a boolean or there is a problem
   * executing the chosen alternative
   */
  @Override
  public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    TypedValue value = children[0].getValueInternal(state);
    if (value.getValue()!=null) {
      return value;
    } else {
      return children[1].getValueInternal(state);
    }
  }
View Full Code Here


  public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    boolean leftValue;
    boolean rightValue;

    try {
      TypedValue typedValue = getLeftOperand().getValueInternal(state);
      this.assertTypedValueNotNull(typedValue);
      leftValue = (Boolean)state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
    }
    catch (SpelEvaluationException ee) {
      ee.setPosition(getLeftOperand().getStartPosition());
      throw ee;
    }

    if (leftValue == false) {
      return BooleanTypedValue.forValue(false); // no need to evaluate right operand
    }

    try {
      TypedValue typedValue = getRightOperand().getValueInternal(state);
      this.assertTypedValueNotNull(typedValue);
      rightValue = (Boolean)state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
    }
    catch (SpelEvaluationException ee) {
      ee.setPosition(getRightOperand().getStartPosition());
View Full Code Here

  @Override
  public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    boolean leftValue;
    boolean rightValue;
    try {
      TypedValue typedValue = getLeftOperand().getValueInternal(state);
      this.assertTypedValueNotNull(typedValue);
      leftValue = (Boolean)state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
    }
    catch (SpelEvaluationException see) {
      see.setPosition(getLeftOperand().getStartPosition());
      throw see;
    }

    if (leftValue == true) {
      return BooleanTypedValue.TRUE; // no need to evaluate right operand
    }

    try {
      TypedValue typedValue = getRightOperand().getValueInternal(state);
      this.assertTypedValueNotNull(typedValue);
      rightValue = (Boolean)state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
    }
    catch (SpelEvaluationException see) {
      see.setPosition(getRightOperand().getStartPosition()); // TODO end positions here and in similar situations
View Full Code Here

    super(pos,operands);
  }

  @Override
  public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    TypedValue newValue = children[1].getValueInternal(state);
    getChild(0).setValue(state, newValue.getValue());
    return newValue;
  }
View Full Code Here

   * @throws EvaluationException if there is a problem during conversion or conversion of the value to the specified
   * type is not supported
   */
  public static <T> T convert(EvaluationContext context, Object value, Class<T> targetType) throws EvaluationException {
    // TODO remove this function over time and use the one it delegates to
    return convertTypedValue(context,new TypedValue(value),targetType);
  }
View Full Code Here

        try {
          if (needsToBeMadeAccessible) {
            ReflectionUtils.makeAccessible((Method) member);
          }
          Object value = ((Method) member).invoke(target);
          return new TypedValue(value, typeDescriptor.narrow(value));
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access property '" + name + "' through getter", ex);
        }
      }   
      if (member instanceof Field) {
        try {
          if (needsToBeMadeAccessible) {
            ReflectionUtils.makeAccessible((Field)member);
          }
          Object value = ((Field)member).get(target);
          return new TypedValue(value, typeDescriptor.narrow(value));
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access field: " + name, ex);
        }
      }
View Full Code Here

    super(pos, expr);
  }

  @Override
  public TypedValue getValueInternal(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 && (children[0] instanceof PropertyOrFieldReference)) {
      PropertyOrFieldReference reference = (PropertyOrFieldReference)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 = 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());
      }
      Object value = ((Map<?, ?>) targetObject).get(key);
      return new TypedValue(value, targetObjectTypeDescriptor.getMapValueTypeDescriptor(value));
    }
   
    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 instanceof Collection || targetObject.getClass().isArray() || targetObject instanceof String) {
      int idx = (Integer) state.convertValue(index, TypeDescriptor.valueOf(Integer.class));   
      if (targetObject.getClass().isArray()) {
        Object arrayElement = accessArrayElement(targetObject, idx);
        return new TypedValue(arrayElement, targetObjectTypeDescriptor.elementTypeDescriptor(arrayElement));
      } else if (targetObject instanceof Collection) {
        Collection c = (Collection) targetObject;
        if (idx >= c.size()) {
          if (!growCollection(state, targetObjectTypeDescriptor, idx, c)) {
            throw new SpelEvaluationException(getStartPosition(),SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS, c.size(), idx);
          }
        }
        int pos = 0;
        for (Object o : c) {
          if (pos == idx) {
            return new TypedValue(o, targetObjectTypeDescriptor.elementTypeDescriptor(o));
          }
          pos++;
        }
      } else if (targetObject instanceof String) {
        String ctxString = (String) targetObject;
        if (idx >= ctxString.length()) {
          throw new SpelEvaluationException(getStartPosition(),SpelMessage.STRING_INDEX_OUT_OF_BOUNDS, ctxString.length(), idx);
        }
        return new TypedValue(String.valueOf(ctxString.charAt(idx)));
      }
    }
   
    // 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) {
      Class<?> targetObjectRuntimeClass = getObjectClass(targetObject);
      String name = (String)indexValue.getValue();
      EvaluationContext eContext = state.getEvaluationContext();

      try {
        if (cachedReadName!=null && cachedReadName.equals(name) && cachedReadTargetType!=null && cachedReadTargetType.equals(targetObjectRuntimeClass)) {
          // it is OK to use the cached accessor
View Full Code Here

  }
 
  @SuppressWarnings("unchecked")
  @Override
  public void setValue(ExpressionState state, Object newValue) throws EvaluationException {
    TypedValue contextObject = state.getActiveContextObject();
    Object targetObject = contextObject.getValue();
    TypeDescriptor targetObjectTypeDescriptor = contextObject.getTypeDescriptor();
    TypedValue index = children[0].getValueInternal(state);

    if (targetObject == null) {
      throw new SpelEvaluationException(SpelMessage.CANNOT_INDEX_INTO_NULL_VALUE);
    }
    // Indexing into a Map
    if (targetObject instanceof Map) {
      Map map = (Map) targetObject;
      Object key = index.getValue();
      if (targetObjectTypeDescriptor.getMapKeyTypeDescriptor() != null) {
        key = state.convertValue(index, targetObjectTypeDescriptor.getMapKeyTypeDescriptor());
      }
      if (targetObjectTypeDescriptor.getMapValueTypeDescriptor() != null) {
        newValue = state.convertValue(newValue, targetObjectTypeDescriptor.getMapValueTypeDescriptor());       
      }
      map.put(key, newValue);
      return;
    }

    if (targetObjectTypeDescriptor.isArray()) {
      int idx = (Integer)state.convertValue(index, TypeDescriptor.valueOf(Integer.class));
      setArrayElement(state, contextObject.getValue(), idx, newValue, targetObjectTypeDescriptor.getElementTypeDescriptor().getType());
      return;
    }
    else if (targetObject instanceof Collection) {
      int idx = (Integer) state.convertValue(index, TypeDescriptor.valueOf(Integer.class));
      Collection c = (Collection) targetObject;
      if (idx >= c.size()) {
        if (!growCollection(state, targetObjectTypeDescriptor, idx, c)) {
          throw new SpelEvaluationException(getStartPosition(),SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS, c.size(), idx);
        }
      }
      if (targetObject instanceof List) {
        List list = (List) targetObject;
        if (targetObjectTypeDescriptor.getElementTypeDescriptor() != null) {
          newValue = state.convertValue(newValue, targetObjectTypeDescriptor.getElementTypeDescriptor());
        }
        list.set(idx, newValue);
        return;
      }
      else {
        throw new SpelEvaluationException(getStartPosition(),SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, targetObjectTypeDescriptor.toString());
      }
    }
   
    // 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 (index.getTypeDescriptor().getType() == String.class) {
      Class<?> contextObjectClass = getObjectClass(contextObject.getValue());
      String name = (String)index.getValue();
      EvaluationContext eContext = state.getEvaluationContext();
      try {
        if (cachedWriteName!=null && cachedWriteName.equals(name) && cachedWriteTargetType!=null && cachedWriteTargetType.equals(contextObjectClass)) {
          // it is OK to use the cached accessor
          cachedWriteAccessor.write(eContext, targetObject, name,newValue);
View Full Code Here

    setRootObject(rootObject);
  }


  public void setRootObject(Object rootObject, TypeDescriptor typeDescriptor) {
    this.rootObject = new TypedValue(rootObject, typeDescriptor);
  }
View Full Code Here

  public void setRootObject(Object rootObject, TypeDescriptor typeDescriptor) {
    this.rootObject = new TypedValue(rootObject, typeDescriptor);
  }

  public void setRootObject(Object rootObject) {
    this.rootObject = (rootObject != null ? new TypedValue(rootObject) : TypedValue.NULL);
  }
View Full Code Here

TOP

Related Classes of org.springframework.expression.TypedValue

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.