Package org.springframework.expression

Examples of org.springframework.expression.TypedValue


    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

  }

  @SuppressWarnings("unchecked")
  @Override
  public TypedValue getValueInternal(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 TypedValue(result);
              }
              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 TypedValue(null);
      }
      if (variant == LAST) {
        Map resultMap = new HashMap();
        Object lastValue = result.get(lastKey);
        resultMap.put(lastKey,lastValue);
        return new TypedValue(resultMap);
      }
      return new TypedValue(result);
    } 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 TypedValue(element);
              }
              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 TypedValue.NULL;
      }
      if (variant == LAST) {
        return new TypedValue(result.get(result.size() - 1));
      }
      if (operand instanceof Collection) {
        return new TypedValue(result);
      }
      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 TypedValue(resultArray);
      }
    } else {
      if (operand==null) {
        if (nullSafe) {
          return TypedValue.NULL;
View Full Code Here

  }


  @Override
  public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    TypedValue result = readProperty(state, this.name);
   
    // Dynamically create the objects if the user has requested that optional behaviour
    if (result.getValue() == null && state.getConfiguration().isAutoGrowNullReferences() &&
        nextChildIs(Indexer.class, PropertyOrFieldReference.class)) {
      TypeDescriptor resultDescriptor = result.getTypeDescriptor();
      // Creating lists and maps
      if ((resultDescriptor.getType().equals(List.class) || resultDescriptor.getType().equals(Map.class))) {
        // Create a new collection or map ready for the indexer
        if (resultDescriptor.getType().equals(List.class)) {
          try {
            if (isWritable(state)) {
              List newList = ArrayList.class.newInstance();
              writeProperty(state, this.name, newList);
              result = readProperty(state, this.name);
            }
          }
          catch (InstantiationException ex) {
            throw new SpelEvaluationException(getStartPosition(), ex,
                SpelMessage.UNABLE_TO_CREATE_LIST_FOR_INDEXING);
          }
          catch (IllegalAccessException ex) {
            throw new SpelEvaluationException(getStartPosition(), ex,
                SpelMessage.UNABLE_TO_CREATE_LIST_FOR_INDEXING);
          }
        }
        else {
          try {
            if (isWritable(state)) {
              Map newMap = HashMap.class.newInstance();
              writeProperty(state, name, newMap);
              result = readProperty(state, this.name);
            }
          }
          catch (InstantiationException ex) {
            throw new SpelEvaluationException(getStartPosition(), ex,
                SpelMessage.UNABLE_TO_CREATE_MAP_FOR_INDEXING);
          }
          catch (IllegalAccessException ex) {
            throw new SpelEvaluationException(getStartPosition(), ex,
                SpelMessage.UNABLE_TO_CREATE_MAP_FOR_INDEXING);
          }
        }
      }
      else {
        // 'simple' object
        try {
          if (isWritable(state)) {
            Object newObject  = result.getTypeDescriptor().getType().newInstance();
            writeProperty(state, name, newObject);
            result = readProperty(state, this.name);
          }
        }
        catch (InstantiationException ex) {
          throw new SpelEvaluationException(getStartPosition(), ex,
              SpelMessage.UNABLE_TO_DYNAMICALLY_CREATE_OBJECT, result.getTypeDescriptor().getType());
        }
        catch (IllegalAccessException ex) {
          throw new SpelEvaluationException(getStartPosition(), ex,
              SpelMessage.UNABLE_TO_DYNAMICALLY_CREATE_OBJECT, result.getTypeDescriptor().getType());
        }       
      }
    }
    return result;
  }
View Full Code Here

   * @param name the name of the property
   * @return the value of the property
   * @throws SpelEvaluationException if any problem accessing the property or it cannot be found
   */
  private TypedValue readProperty(ExpressionState state, String name) throws EvaluationException {
    TypedValue contextObject = state.getActiveContextObject();
    Object targetObject = contextObject.getValue();

    if (targetObject == null && this.nullSafe) {
      return TypedValue.NULL;
    }

    PropertyAccessor accessorToUse = this.cachedReadAccessor;
    if (accessorToUse != null) {
      try {
        return accessorToUse.read(state.getEvaluationContext(), contextObject.getValue(), name);
      }
      catch (AccessException ae) {
        // this is OK - it may have gone stale due to a class change,
        // let's try to get a new one and call it before giving up
        this.cachedReadAccessor = null;
      }
    }

    Class<?> contextObjectClass = getObjectClass(contextObject.getValue());
    List<PropertyAccessor> accessorsToTry = getPropertyAccessorsToTry(contextObjectClass, state);
    EvaluationContext eContext = state.getEvaluationContext();

    // Go through the accessors that may be able to resolve it. If they are a cacheable accessor then
    // get the accessor and use it. If they are not cacheable but report they can read the property
    // then ask them to read it
    if (accessorsToTry != null) {
      try {
        for (PropertyAccessor accessor : accessorsToTry) {
          if (accessor.canRead(eContext, contextObject.getValue(), name)) {
            if (accessor instanceof ReflectivePropertyAccessor) {
              accessor = ((ReflectivePropertyAccessor) accessor).createOptimalAccessor(
                  eContext, contextObject.getValue(), name);
            }
            this.cachedReadAccessor = accessor;
            return accessor.read(eContext, contextObject.getValue(), name);
          }
        }
      }
      catch (AccessException ae) {
        throw new SpelEvaluationException(ae, SpelMessage.EXCEPTION_DURING_PROPERTY_READ, name, ae.getMessage());
      }
    }
    if (contextObject.getValue() == null) {
      throw new SpelEvaluationException(SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL, name);
    }
    else {
      throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE, name,
          FormatHelper.formatClassNameForMessage(contextObjectClass));
View Full Code Here

          FormatHelper.formatClassNameForMessage(contextObjectClass));
    }
  }

  private void writeProperty(ExpressionState state, String name, Object newValue) throws SpelEvaluationException {
    TypedValue contextObject = state.getActiveContextObject();
    EvaluationContext eContext = state.getEvaluationContext();
   
    if (contextObject.getValue() == null && nullSafe) {
      return;
    }

    PropertyAccessor accessorToUse = this.cachedWriteAccessor;
    if (accessorToUse != null) {
      try {       
        accessorToUse.write(state.getEvaluationContext(), contextObject.getValue(), name, newValue);
        return;
      }
      catch (AccessException ae) {
        // this is OK - it may have gone stale due to a class change,
        // let's try to get a new one and call it before giving up
        this.cachedWriteAccessor = null;
      }
    }

    Class<?> contextObjectClass = getObjectClass(contextObject.getValue());

    List<PropertyAccessor> accessorsToTry = getPropertyAccessorsToTry(contextObjectClass, state);
    if (accessorsToTry != null) {
      try {
        for (PropertyAccessor accessor : accessorsToTry) {
          if (accessor.canWrite(eContext, contextObject.getValue(), name)) {
            this.cachedWriteAccessor = accessor;
            accessor.write(eContext, contextObject.getValue(), name, newValue);
            return;
          }
        }
      }
      catch (AccessException ae) {
        throw new SpelEvaluationException(getStartPosition(), ae, SpelMessage.EXCEPTION_DURING_PROPERTY_WRITE,
            name, ae.getMessage());
      }
    }
    if (contextObject.getValue()==null) {
      throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL, name);
    }
    else {
      throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE, name,
          FormatHelper.formatClassNameForMessage(contextObjectClass));
View Full Code Here

    this.nullSafe = nullSafe;
  }

  @Override
  public TypedValue getValueInternal(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 TypedValue(result); // 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 TypedValue(resultArray);
      }
      return new TypedValue(result);
    }
    else {
      if (operand==null) {
        if (this.nullSafe) {
          return TypedValue.NULL;
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 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());
      }
    }
    Object result = state.operate(Operation.DIVIDE, operandOne, operandTwo);
    return new TypedValue(result);
  }
View Full Code Here

  }


  @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

  }

  private MethodExecutor findAccessorForMethod(String name, List<TypeDescriptor> argumentTypes, ExpressionState state)
      throws SpelEvaluationException {

    TypedValue context = state.getActiveContextObject();
    Object contextObject = context.getValue();
    EvaluationContext eContext = state.getEvaluationContext();

    List<MethodResolver> mResolvers = eContext.getMethodResolvers();
    if (mResolvers != null) {
      for (MethodResolver methodResolver : mResolvers) {
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.