Package org.springframework.expression

Examples of org.springframework.expression.TypedValue


    return getValueRef(state).getValue();
  }

  @Override
  protected ValueRef getValueRef(ExpressionState state) throws EvaluationException {
    TypedValue op = state.getActiveContextObject();

    Object operand = op.getValue();
    boolean operandIsArray = ObjectUtils.isArray(operand);
    // TypeDescriptor operandTypeDescriptor = op.getTypeDescriptor();
   
    // When the input is a map, we push a special context object on the stack
    // before calling the specified operation. This special context object
    // has two fields 'key' and 'value' that refer to the map entries key
    // and value, and they can be referenced in the operation
    // eg. {'a':'y','b':'n'}.!{value=='y'?key:null}" == ['a', null]
    if (operand instanceof Map) {
      Map<?, ?> mapData = (Map<?, ?>) operand;
      List<Object> result = new ArrayList<Object>();
      for (Map.Entry<?,?> entry : mapData.entrySet()) {
        try {
          state.pushActiveContextObject(new TypedValue(entry));
          result.add(this.children[0].getValueInternal(state).getValue());
        }
        finally {
          state.popActiveContextObject();
        }
      }
      return new ValueRef.TypedValueHolderValueRef(new TypedValue(result),this); // TODO unable to build correct type descriptor
    }
    else if (operand instanceof Collection || operandIsArray) {
      Collection<?> data = (operand instanceof Collection ? (Collection<?>) operand :
          Arrays.asList(ObjectUtils.toObjectArray(operand)));
      List<Object> result = new ArrayList<Object>();
      int idx = 0;
      Class<?> arrayElementType = null;
      for (Object element : data) {
        try {
          state.pushActiveContextObject(new TypedValue(element));
          state.enterScope("index", idx);
          Object value = children[0].getValueInternal(state).getValue();
          if (value != null && operandIsArray) {
            arrayElementType = determineCommonType(arrayElementType, value.getClass());
          }
          result.add(value);
        }
        finally {
          state.exitScope();
          state.popActiveContextObject();
        }
        idx++;
      }
      if (operandIsArray) {
        if (arrayElementType == null) {
          arrayElementType = Object.class;
        }
        Object resultArray = Array.newInstance(arrayElementType, result.size());
        System.arraycopy(result.toArray(), 0, resultArray, 0, result.size());
        return new ValueRef.TypedValueHolderValueRef(new TypedValue(resultArray),this);
      }
      return new ValueRef.TypedValueHolderValueRef(new TypedValue(result),this);
    }
    else {
      if (operand==null) {
        if (this.nullSafe) {
          return ValueRef.NullValueRef.instance;
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 {
        // TODO what about non-int result of the division?
        return new TypedValue(op1.intValue() / op2.intValue());
      }
    }
    return state.operate(Operation.DIVIDE, operandOne, operandTwo);
  }
View Full Code Here

      return new ValueRef.TypedValueHolderValueRef(state.getActiveContextObject(),this);
    }
    if (this.name.equals(ROOT)) {
      return new ValueRef.TypedValueHolderValueRef(state.getRootContextObject(),this);
    }
    TypedValue result = state.lookupVariable(this.name);
    // a null value will mean either the value was null or the variable was not found
    return new VariableRef(this.name,result,state.getEvaluationContext());
  }
View Full Code Here

      return state.getActiveContextObject();
    }
    if (this.name.equals(ROOT)) {
      return state.getRootContextObject();
    }
    TypedValue result = state.lookupVariable(this.name);
    // a null value will mean either the value was null or the variable was not found
    return result;
  }
View Full Code Here

    Object operandTwo = getRightOperand().getValueInternal(state).getValue();
    if (operandOne instanceof Number && operandTwo instanceof Number) {
      Number leftNumber = (Number) operandOne;
      Number rightNumber = (Number) operandTwo;
      if (leftNumber instanceof Double || rightNumber instanceof Double) {
        return new TypedValue(leftNumber.doubleValue() * rightNumber.doubleValue());
      }
      else if (leftNumber instanceof Float || rightNumber instanceof Float) {
        return new TypedValue(leftNumber.floatValue() * rightNumber.floatValue());
      }
      else if (leftNumber instanceof Long || rightNumber instanceof Long) {
        return new TypedValue(leftNumber.longValue() * rightNumber.longValue());
      }
      else {
        return new TypedValue(leftNumber.intValue() * rightNumber.intValue());
      }
    }
    else if (operandOne instanceof String && operandTwo instanceof Integer) {
      int repeats = (Integer) operandTwo;
      StringBuilder result = new StringBuilder();
      for (int i = 0; i < repeats; i++) {
        result.append(operandOne);
      }
      return new TypedValue(result.toString());
    }
    return state.operate(Operation.MULTIPLY, operandOne, operandTwo);
  }
View Full Code Here

    SpelNodeImpl rightOp = getRightOperand();
    if (rightOp == null) { // If only one operand, then this is unary plus
      Object operandOne = leftOp.getValueInternal(state).getValue();
      if (operandOne instanceof Number) {
        if (operandOne instanceof Double || operandOne instanceof Long) {
          return new TypedValue(operandOne);
        } else if (operandOne instanceof Float) {
          return new TypedValue(((Number) operandOne).floatValue());
        } else {
          return new TypedValue(((Number) operandOne).intValue());
        }
      }
      return state.operate(Operation.ADD, operandOne, null);
    }
    else {
      final TypedValue operandOneValue = leftOp.getValueInternal(state);
      final Object operandOne = operandOneValue.getValue();

      final TypedValue operandTwoValue = rightOp.getValueInternal(state);
      final Object operandTwo = operandTwoValue.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 { // TODO what about overflow?
          return new TypedValue(op1.intValue() + op2.intValue());
        }
      } else if (operandOne instanceof String && operandTwo instanceof String) {
        return new TypedValue(new StringBuilder((String) operandOne).append((String) operandTwo).toString());
      } else if (operandOne instanceof String) {
        StringBuilder result = new StringBuilder((String) operandOne);
        result.append((operandTwo == null ? "null" : convertTypedValueToString(operandTwoValue, state)));
        return new TypedValue(result.toString());
      } else if (operandTwo instanceof String) {
        StringBuilder result = new StringBuilder((operandOne == null ? "null" : convertTypedValueToString(
            operandOneValue, state)));
        result.append((String) operandTwo);
        return new TypedValue(result.toString());
      }
      return state.operate(Operation.ADD, operandOne, operandTwo);
    }
  }
View Full Code Here

    Object value = this.relatedContext.lookupVariable(name);
    if (value == null) {
      return TypedValue.NULL;
    }
    else {
      return new TypedValue(value);
    }
  }
View Full Code Here

  public TypedValue operate(Operation op, Object left, Object right) throws EvaluationException {
    OperatorOverloader overloader = this.relatedContext.getOperatorOverloader();
    if (overloader.overridesOperation(op, left, right)) {
      Object returnValue = overloader.operate(op, left, right);
      return new TypedValue(returnValue);
    }
    else {
      String leftType = (left==null?"null":left.getClass().getName());
      String rightType = (right==null?"null":right.getClass().getName());
      throw new SpelEvaluationException(SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES, op, leftType, rightType);
View Full Code Here

    if (type.isArray() && name.equals("length")) {
      if (target instanceof Class) {
        throw new AccessException("Cannot access length on array class itself");
      }
      return new TypedValue(Array.getLength(target));
    }

    CacheKey cacheKey = new CacheKey(type, name);
    InvokerPair invoker = this.readerCache.get(cacheKey);

    if (invoker == null || invoker.member instanceof Method) {
      Method method = (Method) (invoker != null ? invoker.member : null);
      if (method == null) {
        method = findGetterForProperty(name, type, target);
        if (method != null) {
          // TODO remove the duplication here between canRead and read
          // 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);
          invoker = new InvokerPair(method, typeDescriptor);
          this.readerCache.put(cacheKey, invoker);
        }
      }
      if (method != null) {
        try {
          ReflectionUtils.makeAccessible(method);
          Object value = method.invoke(target);
          return new TypedValue(value, invoker.typeDescriptor.narrow(value));
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access property '" + name + "' through getter", ex);
        }
      }
    }

    if (invoker == null || invoker.member instanceof Field) {
      Field field = (Field) (invoker == null ? null : invoker.member);
      if (field == null) {
        field = findField(name, type, target);
        if (field != null) {
          invoker = new InvokerPair(field, new TypeDescriptor(field));
          this.readerCache.put(cacheKey, invoker);
        }
      }
      if (field != null) {
        try {
          ReflectionUtils.makeAccessible(field);
          Object value = field.get(target);
          return new TypedValue(value, invoker.typeDescriptor.narrow(value));
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access field: " + name, ex);
        }
      }
View Full Code Here

        try {
          if (this.needsToBeMadeAccessible) {
            ReflectionUtils.makeAccessible((Method) this.member);
          }
          Object value = ((Method) this.member).invoke(target);
          return new TypedValue(value, this.typeDescriptor.narrow(value));
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access property '" + name + "' through getter", ex);
        }
      }
      if (this.member instanceof Field) {
        try {
          if (this.needsToBeMadeAccessible) {
            ReflectionUtils.makeAccessible((Field) this.member);
          }
          Object value = ((Field) this.member).get(target);
          return new TypedValue(value, this.typeDescriptor.narrow(value));
        }
        catch (Exception ex) {
          throw new AccessException("Unable to access field: " + name, ex);
        }
      }
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.