Package org.springframework.expression

Examples of org.springframework.expression.TypedValue


  private void populateBooleanArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
      InlineList initializer) {
    boolean[] newBooleanArray = (boolean[]) newArray;
    for (int i = 0; i < newBooleanArray.length; i++) {
      TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
      newBooleanArray[i] = ExpressionUtils.toBoolean(typeConverter, typedValue);
    }
  }
View Full Code Here


  private void populateIntArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
      InlineList initializer) {
    int[] newIntArray = (int[]) newArray;
    for (int i = 0; i < newIntArray.length; i++) {
      TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
      newIntArray[i] = ExpressionUtils.toInt(typeConverter, typedValue);
    }
  }
View Full Code Here

    Object operandTwo = getRightOperand().getValueInternal(state).getValue();
    if (operandOne instanceof Number && operandTwo instanceof Number) {
      Number op1 = (Number) operandOne;
      Number op2 = (Number) operandTwo;
      if (op1 instanceof Double || op2 instanceof Double) {
        return new TypedValue(op1.doubleValue() % op2.doubleValue());
      } else if (op1 instanceof Float || op2 instanceof Float) {
        return new TypedValue(op1.floatValue() % op2.floatValue());
      } else if (op1 instanceof Long || op2 instanceof Long) {
        return new TypedValue(op1.longValue() % op2.longValue());
      } else {
        return new TypedValue(op1.intValue() % op2.intValue());
      }
    }
    return state.operate(Operation.MODULUS, operandOne, operandTwo);
  }
View Full Code Here

    BeanResolver beanResolver = state.getEvaluationContext().getBeanResolver();
    if (beanResolver==null) {
      throw new SpelEvaluationException(getStartPosition(),SpelMessage.NO_BEAN_RESOLVER_REGISTERED, beanname);
    }
    try {
       TypedValue bean = new TypedValue(beanResolver.resolve(state.getEvaluationContext(),beanname));
       return bean;
    } catch (AccessException ae) {
      throw new SpelEvaluationException( getStartPosition(), ae, SpelMessage.EXCEPTION_DURING_BEAN_RESOLUTION,
        beanname, ae.getMessage());
    }
View Full Code Here

    if (rightOp == null) {// If only one operand, then this is unary minus
      Object operand = leftOp.getValueInternal(state).getValue();
      if (operand instanceof Number) {
        Number n = (Number) operand;
        if (operand instanceof Double) {
          return new TypedValue(0 - n.doubleValue());
        } else if (operand instanceof Float) {
          return new TypedValue(0 - n.floatValue());
        } else if (operand instanceof Long) {
          return new TypedValue(0 - n.longValue());
        } else {
          return new TypedValue(0 - n.intValue());
        }
      }
      return state.operate(Operation.SUBTRACT, operand, null);
    } else {
      Object left = leftOp.getValueInternal(state).getValue();
      Object right = rightOp.getValueInternal(state).getValue();
      if (left instanceof Number && right instanceof Number) {
        Number op1 = (Number) left;
        Number op2 = (Number) right;
        if (op1 instanceof Double || op2 instanceof Double) {
          return new TypedValue(op1.doubleValue() - op2.doubleValue());
        } else if (op1 instanceof Float || op2 instanceof Float) {
          return new TypedValue(op1.floatValue() - op2.floatValue());
        } else if (op1 instanceof Long || op2 instanceof Long) {
          return new TypedValue(op1.longValue() - op2.longValue());
        } else {
          return new TypedValue(op1.intValue() - op2.intValue());
        }
      } else if (left instanceof String && right instanceof Integer && ((String)left).length()==1) {
        String theString = (String) left;
        Integer theInteger = (Integer) right;
        // implements character - int (ie. b - 1 = a)
        return new TypedValue(Character.toString((char) (theString.charAt(0) - theInteger)));
      }
      return state.operate(Operation.SUBTRACT, left, right);
    }
  }
View Full Code Here

public class FloatLiteral extends Literal {
  private final TypedValue value;

  FloatLiteral(String payload, int pos, float value) {
    super(payload, pos);
    this.value = new TypedValue(value);
  }
View Full Code Here

  }

  @Override
  protected ValueRef getValueRef(ExpressionState state) throws EvaluationException {
    TypedValue currentContext = state.getActiveContextObject();
    Object[] arguments = new Object[getChildCount()];
    for (int i = 0; i < arguments.length; i++) {
      // Make the root object the active context again for evaluating the parameter
      // expressions
      try {
        state.pushActiveContextObject(state.getRootContextObject());
        arguments[i] = children[i].getValueInternal(state).getValue();
      }
      finally {
        state.popActiveContextObject();
      }
    }
    if (currentContext.getValue() == null) {
      if (nullSafe) {
        return ValueRef.NullValueRef.instance;
      }
      else {
        throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_CALL_ON_NULL_OBJECT_NOT_ALLOWED,
View Full Code Here

    return new MethodValueRef(state,state.getEvaluationContext(),state.getActiveContextObject().getValue(),arguments);
  }

  @Override
  public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    TypedValue currentContext = state.getActiveContextObject();
    Object[] arguments = new Object[getChildCount()];
    for (int i = 0; i < arguments.length; i++) {
      // Make the root object the active context again for evaluating the parameter
      // expressions
      try {
        state.pushActiveContextObject(state.getRootContextObject());
        arguments[i] = children[i].getValueInternal(state).getValue();
      }
      finally {
        state.popActiveContextObject();
      }
    }
    if (currentContext.getValue() == null) {
      if (this.nullSafe) {
        return TypedValue.NULL;
      }
      else {
        throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_CALL_ON_NULL_OBJECT_NOT_ALLOWED,
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

    Object operandTwo = rightOp.getValueInternal(state).getValue();
    if (operandOne instanceof Number && operandTwo instanceof Number) {
      Number op1 = (Number) operandOne;
      Number op2 = (Number) operandTwo;
      if (op1 instanceof Double || op2 instanceof Double) {
        return new TypedValue(Math.pow(op1.doubleValue(),op2.doubleValue()));
      } else if (op1 instanceof Float || op2 instanceof Float) {
        return new TypedValue(Math.pow(op1.floatValue(), op2.floatValue()));
      } else if (op1 instanceof Long || op2 instanceof Long) {
        double d= Math.pow(op1.longValue(), op2.longValue());
        return new TypedValue((long)d);
      } else {
        double d= Math.pow(op1.longValue(), op2.longValue());
        if (d > Integer.MAX_VALUE) {
          return new TypedValue((long)d);
        } else {
          return new TypedValue((int)d);
        }
      }
    }
    return state.operate(Operation.POWER, operandOne, operandTwo);
  }
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.