Package org.springframework.expression.spel

Examples of org.springframework.expression.spel.SpelEvaluationException


    }
  }

  private void checkAccess(int arrayLength, int index) throws SpelEvaluationException {
    if (index > arrayLength) {
      throw new SpelEvaluationException(getStartPosition(), SpelMessage.ARRAY_INDEX_OUT_OF_BOUNDS, arrayLength, index);
    }
  }
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);
    }
    catch (SpelEvaluationException see) {
View Full Code Here

    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(),
          SpelMessage.INSTANCEOF_OPERATOR_NEEDS_CLASS_OPERAND,
          (rightValue == null ? "null" : rightValue.getClass().getName()));
    }
    Class<?> rightClass = (Class<?>) rightValue;
    return BooleanTypedValue.forValue(rightClass.isAssignableFrom(leftValue.getClass()));
View Full Code Here

    return /* leftValue && */BooleanTypedValue.forValue(rightValue);
  }

  private void assertTypedValueNotNull(TypedValue typedValue) {
    if (TypedValue.NULL.equals(typedValue)) {
      throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
    }
  }
View Full Code Here

    return BooleanTypedValue.forValue(leftValue || rightValue);
  }

  private void assertTypedValueNotNull(TypedValue typedValue) {
    if (TypedValue.NULL.equals(typedValue)) {
      throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
    }
  }
View Full Code Here

   */
  @Override
  public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    Boolean value = children[0].getValue(state, Boolean.class);
    if (value == null) {
      throw new SpelEvaluationException(getChild(0).getStartPosition(),
          SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
    }
    if (value.booleanValue()) {
      return children[1].getValueInternal(state);
    } else {
View Full Code Here

      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
          return cachedReadAccessor.read(eContext, targetObject, name);
        }
       
        List<PropertyAccessor> accessorsToTry = AstUtils.getPropertyAccessorsToTry(targetObjectRuntimeClass, state);
   
        if (accessorsToTry != null) {     
          for (PropertyAccessor accessor : accessorsToTry) {
              if (accessor.canRead(eContext, targetObject, name)) {
                if (accessor instanceof ReflectivePropertyAccessor) {
                  accessor = ((ReflectivePropertyAccessor)accessor).createOptimalAccessor(eContext, targetObject, name);
                }
                this.cachedReadAccessor = accessor;
                this.cachedReadName = name;
                this.cachedReadTargetType = targetObjectRuntimeClass;
                return accessor.read(eContext, targetObject, name);
              }
          }
        }
      } catch (AccessException e) {
        throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, targetObjectTypeDescriptor.toString());
      }
    }
     
    throw new SpelEvaluationException(getStartPosition(),SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, targetObjectTypeDescriptor.toString());
  }
View Full Code Here

    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);
          return;
        }
 
        List<PropertyAccessor> accessorsToTry = AstUtils.getPropertyAccessorsToTry(contextObjectClass, state);
        if (accessorsToTry != null) {
            for (PropertyAccessor accessor : accessorsToTry) {
              if (accessor.canWrite(eContext, contextObject.getValue(), name)) {
                this.cachedWriteName = name;
                this.cachedWriteTargetType = contextObjectClass;
                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());
      }

    }
   
    throw new SpelEvaluationException(getStartPosition(),SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, targetObjectTypeDescriptor.toString());
  }
View Full Code Here

  @SuppressWarnings("unchecked")
  private boolean growCollection(ExpressionState state, TypeDescriptor targetType, int index,
      Collection collection) {
    if (state.getConfiguration().isAutoGrowCollections()) {
      if (targetType.getElementTypeDescriptor() == null) {
        throw new SpelEvaluationException(getStartPosition(), SpelMessage.UNABLE_TO_GROW_COLLECTION_UNKNOWN_ELEMENT_TYPE);       
      }
      TypeDescriptor elementType = targetType.getElementTypeDescriptor();
      Object newCollectionElement = null;
      try {
        int newElements = index - collection.size();
        while (newElements>0) {
          collection.add(elementType.getType().newInstance());
          newElements--;
        }
        newCollectionElement = elementType.getType().newInstance();
      }
      catch (Exception ex) {
        throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.UNABLE_TO_GROW_COLLECTION);
      }
      collection.add(newCollectionElement);
      return true;
    }
    return false;
View Full Code Here

    }
  }

  private void checkAccess(int arrayLength, int index) throws SpelEvaluationException {
    if (index > arrayLength) {
      throw new SpelEvaluationException(getStartPosition(), SpelMessage.ARRAY_INDEX_OUT_OF_BOUNDS, arrayLength, index);
    }
  }
View Full Code Here

TOP

Related Classes of org.springframework.expression.spel.SpelEvaluationException

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.