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();

    SpelNodeImpl selectionCriteria = children[0];
    if (operand instanceof Map) {
      Map<?, ?> mapdata = (Map<?, ?>) operand;
      // TODO don't lose generic info for the new map
      Map<Object,Object> result = new HashMap<Object,Object>();
      Object lastKey = null;
      for (Map.Entry<?,?> entry : mapdata.entrySet()) {
        try {
          TypedValue kvpair = new TypedValue(entry);
          state.pushActiveContextObject(kvpair);
          Object o = selectionCriteria.getValueInternal(state).getValue();
          if (o instanceof Boolean) {
            if (((Boolean) o).booleanValue() == true) {
              if (variant == FIRST) {
                result.put(entry.getKey(),entry.getValue());
                return new ValueRef.TypedValueHolderValueRef(new TypedValue(result),this);
              }
              result.put(entry.getKey(),entry.getValue());
              lastKey = entry.getKey();
            }
          } else {
            throw new SpelEvaluationException(selectionCriteria.getStartPosition(),
                SpelMessage.RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN);// ,selectionCriteria.stringifyAST());
          }
        } finally {
          state.popActiveContextObject();
        }
      }
      if ((variant == FIRST || variant == LAST) && result.size() == 0) {
        return new ValueRef.TypedValueHolderValueRef(new TypedValue(null),this);
      }
      if (variant == LAST) {
        Map<Object,Object> resultMap = new HashMap<Object,Object>();
        Object lastValue = result.get(lastKey);
        resultMap.put(lastKey,lastValue);
        return new ValueRef.TypedValueHolderValueRef(new TypedValue(resultMap),this);
      }
      return new ValueRef.TypedValueHolderValueRef(new TypedValue(result),this);
    } else if ((operand instanceof Collection) || ObjectUtils.isArray(operand)) {
      List<Object> data = new ArrayList<Object>();
      Collection<?> c = (operand instanceof Collection) ?
          (Collection<?>) operand : Arrays.asList(ObjectUtils.toObjectArray(operand));
      data.addAll(c);
      List<Object> result = new ArrayList<Object>();
      int idx = 0;
      for (Object element : data) {
        try {
          state.pushActiveContextObject(new TypedValue(element));
          state.enterScope("index", idx);
          Object o = selectionCriteria.getValueInternal(state).getValue();
          if (o instanceof Boolean) {
            if (((Boolean) o).booleanValue() == true) {
              if (variant == FIRST) {
                return new ValueRef.TypedValueHolderValueRef(new TypedValue(element),this);
              }
              result.add(element);
            }
          } else {
            throw new SpelEvaluationException(selectionCriteria.getStartPosition(),
                SpelMessage.RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN);// ,selectionCriteria.stringifyAST());
          }
          idx++;
        } finally {
          state.exitScope();
          state.popActiveContextObject();
        }
      }
      if ((variant == FIRST || variant == LAST) && result.size() == 0) {
        return ValueRef.NullValueRef.instance;
      }
      if (variant == LAST) {
        return new ValueRef.TypedValueHolderValueRef(new TypedValue(result.get(result.size() - 1)),this);
      }
      if (operand instanceof Collection) {
        return new ValueRef.TypedValueHolderValueRef(new TypedValue(result),this);
      }
      else {
        Class<?> elementType = ClassUtils.resolvePrimitiveIfNecessary(op.getTypeDescriptor().getElementTypeDescriptor().getType());
        Object resultArray = Array.newInstance(elementType, result.size());
        System.arraycopy(result.toArray(), 0, resultArray, 0, result.size());
        return new ValueRef.TypedValueHolderValueRef(new TypedValue(resultArray),this);
      }
    } else {
      if (operand==null) {
        if (nullSafe) {
          return ValueRef.NullValueRef.instance;
View Full Code Here


    name = functionName;
  }

  @Override
  public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    TypedValue o = state.lookupVariable(name);
    if (o == null) {
      throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_NOT_DEFINED, name);
    }

    // Two possibilities: a lambda function or a Java static method registered as a function
    if (!(o.getValue() instanceof Method)) {
      throw new SpelEvaluationException(SpelMessage.FUNCTION_REFERENCE_CANNOT_BE_INVOKED, name, o.getClass());
    }
    try {
      return executeFunctionJLRMethod(state, (Method) o.getValue());
    }
    catch (SpelEvaluationException se) {
      se.setPosition(getStartPosition());
      throw se;
    }
View Full Code Here

    }

    try {
      ReflectionUtils.makeAccessible(method);
      Object result = method.invoke(method.getClass(), functionArgs);
      return new TypedValue(result, new TypeDescriptor(new MethodParameter(method,-1)).narrow(result));
    }
    catch (Exception ex) {
      throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL,
          this.name, ex.getMessage());
    }
View Full Code Here

  public StringLiteral(String payload, int pos, String value) {
    super(payload,pos);
    // TODO should these have been skipped being created by the parser rules? or not?
    value = value.substring(1, value.length() - 1);
    this.value = new TypedValue(value.replaceAll("''", "'").replaceAll("\"\"", "\""));
  }
View Full Code Here

  }

  @SuppressWarnings("unchecked")
  @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 (targetObjectTypeDescriptor.isMap()) {
      if (targetObject == null) {
          // Current decision: attempt to index into null map == exception and does not just return null
        throw new SpelEvaluationException(getStartPosition(),SpelMessage.CANNOT_INDEX_INTO_NULL_VALUE);
      }
      Object possiblyConvertedKey = index;
      if (targetObjectTypeDescriptor.isMapEntryTypeKnown()) {
        possiblyConvertedKey = state.convertValue(index,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getMapKeyType()));
      }
      Object o = ((Map<?, ?>) targetObject).get(possiblyConvertedKey);
      if (targetObjectTypeDescriptor.isMapEntryTypeKnown()) {
        return new TypedValue(o, targetObjectTypeDescriptor.getMapValueTypeDescriptor());
      } else {
        return new TypedValue(o);
      }
    }

    int idx = (Integer)state.convertValue(index, TypeDescriptor.valueOf(Integer.class));

    if (targetObject == null) {
      throw new SpelEvaluationException(getStartPosition(),SpelMessage.CANNOT_INDEX_INTO_NULL_VALUE);
    }
   
    if (targetObject.getClass().isArray()) {
      return new TypedValue(accessArrayElement(targetObject, idx),TypeDescriptor.valueOf(targetObjectTypeDescriptor.getElementType()));
    } else if (targetObject instanceof Collection) {
      Collection c = (Collection) targetObject;
      if (idx >= c.size()) {
        if (state.getConfiguration().isAutoGrowCollections()) {
          // Grow the collection
          Object newCollectionElement = null;
          try {
            int newElements = idx-c.size();
            Class elementClass = targetObjectTypeDescriptor.getElementType();
            if (elementClass == null) {
              throw new SpelEvaluationException(getStartPosition(), SpelMessage.UNABLE_TO_GROW_COLLECTION_UNKNOWN_ELEMENT_TYPE)
            }
            while (newElements>0) {
              c.add(elementClass.newInstance());
              newElements--;
            }
            newCollectionElement = targetObjectTypeDescriptor.getElementType().newInstance();
          }
          catch (Exception ex) {
            throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.UNABLE_TO_GROW_COLLECTION);
          }
          c.add(newCollectionElement);
          return new TypedValue(newCollectionElement,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getElementType()));
        }
        else {
          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,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getElementType()));
        }
        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)));
    }
    throw new SpelEvaluationException(getStartPosition(),SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, targetObjectTypeDescriptor.asString());
  }
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 (targetObjectTypeDescriptor.isMap()) {
      Map map = (Map)targetObject;
      Object possiblyConvertedKey = index;
      Object possiblyConvertedValue = newValue;
      if (targetObjectTypeDescriptor.isMapEntryTypeKnown()) {
        possiblyConvertedKey = state.convertValue(index.getValue(),TypeDescriptor.valueOf(targetObjectTypeDescriptor.getMapKeyType()));
        possiblyConvertedValue = state.convertValue(newValue,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getMapValueType()));
      }
      map.put(possiblyConvertedKey,possiblyConvertedValue);
      return;
    }
View Full Code Here

  }
 
  @Override
  public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    try {
      TypedValue typedValue = children[0].getValueInternal(state);
      if (TypedValue.NULL.equals(typedValue)) {
        throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
      }
      boolean value = (Boolean) state.convertValue(typedValue, TypeDescriptor.valueOf(Boolean.class));
      return BooleanTypedValue.forValue(!value);
View Full Code Here

   * @return true if the left operand is an instanceof of the right operand, otherwise false
   * @throws EvaluationException if there is a problem evaluating the expression
   */
  @Override
  public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    TypedValue left = getLeftOperand().getValueInternal(state);
    TypedValue right = getRightOperand().getValueInternal(state);
    Object leftValue = left.getValue();
    Object rightValue = right.getValue();
    if (leftValue == null) {
      return BooleanTypedValue.FALSE;  // null is not an instanceof anything
    }
    if (rightValue == null || !(rightValue instanceof Class<?>)) {
      throw new SpelEvaluationException(getRightOperand().getStartPosition(),
View Full Code Here

  private final TypedValue value;

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

  private final TypedValue value;

  LongLiteral(String payload, int pos, long value) {
    super(payload, pos);
    this.value = new TypedValue(value);
  }
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.