Package org.springframework.expression.spel

Examples of org.springframework.expression.spel.SpelEvaluationException


  public boolean isWritable(ExpressionState expressionState) throws EvaluationException {
    return false;
  }

  public void setValue(ExpressionState expressionState, Object newValue) throws EvaluationException {
    throw new SpelEvaluationException(getStartPosition(), SpelMessage.SETVALUE_NOT_SUPPORTED, getClass());
  }
View Full Code Here


  public int getEndPosition() {
    return (pos&0xffff);
  }

  protected ValueRef getValueRef(ExpressionState state) throws EvaluationException {
    throw new SpelEvaluationException(pos,SpelMessage.NOT_ASSIGNABLE,toStringAST());
  }
View Full Code Here

  public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) {
    try {
      return this.conversionService.convert(value, sourceType, targetType);
    }
    catch (ConverterNotFoundException cenfe) {
      throw new SpelEvaluationException(cenfe, SpelMessage.TYPE_CONVERSION_ERROR, sourceType.toString(), targetType.toString());
    }
    catch (ConversionException ce) {
      throw new SpelEvaluationException(ce, SpelMessage.TYPE_CONVERSION_ERROR, sourceType.toString(), targetType.toString());
    }
  }
View Full Code Here

      if (closeMatch != null) {
        return new ReflectiveMethodExecutor(closeMatch, null);
      }
      else if (matchRequiringConversion != null) {
        if (multipleOptions) {
          throw new SpelEvaluationException(SpelMessage.MULTIPLE_POSSIBLE_METHODS, name);
        }
        return new ReflectiveMethodExecutor(matchRequiringConversion, argsToConvert);
      }
      else {
        return null;
View Full Code Here

              }
              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;
        } else {
          throw new SpelEvaluationException(getStartPosition(), SpelMessage.INVALID_TYPE_FOR_SELECTION,
              "null");
        }
      } else {
        throw new SpelEvaluationException(getStartPosition(), SpelMessage.INVALID_TYPE_FOR_SELECTION,
            operand.getClass().getName());
      }       
    }
  }
View Full Code Here

      }
      try {
        Object argument = arguments[argPosition];
        if (argument != null && !targetType.getObjectType().isInstance(argument)) {
          if (converter == null) {
            throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, argument.getClass().getName(), targetType);
          }
          arguments[argPosition] = converter.convertValue(argument, TypeDescriptor.forObject(argument), targetType);
        }
      }
      catch (EvaluationException ex) {
        // allows for another type converter throwing a different kind of EvaluationException
        if (ex instanceof SpelEvaluationException) {
          throw (SpelEvaluationException)ex;
        }
        else {
          throw new SpelEvaluationException(ex, SpelMessage.TYPE_CONVERSION_ERROR,arguments[argPosition].getClass().getName(), targetType);
        }
      }
    }
  }
View Full Code Here

  @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) {
View Full Code Here

   */
  private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException {
    Object[] functionArgs = getArguments(state);

    if (!method.isVarArgs() && method.getParameterTypes().length != functionArgs.length) {
      throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION,
          functionArgs.length, method.getParameterTypes().length);
    }
    // Only static methods can be called in this way
    if (!Modifier.isStatic(method.getModifiers())) {
      throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_MUST_BE_STATIC, method
          .getDeclaringClass().getName()
          + "." + method.getName(), name);
    }

    // Convert arguments if necessary and remap them for varargs if required
    if (functionArgs != null) {
      TypeConverter converter = state.getEvaluationContext().getTypeConverter();
      ReflectionHelper.convertAllArguments(converter, functionArgs, method);
    }
    if (method.isVarArgs()) {
      functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(method.getParameterTypes(), functionArgs);
    }

    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

    // 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

    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;
    }

    if (targetObjectTypeDescriptor.isArray()) {
      int idx = (Integer)state.convertValue(index, TypeDescriptor.valueOf(Integer.class));
      setArrayElement(state, contextObject.getValue(), idx, newValue, targetObjectTypeDescriptor.getElementType());
    }
    else if (targetObjectTypeDescriptor.isCollection()) {
      int idx = (Integer)state.convertValue(index, TypeDescriptor.valueOf(Integer.class));
      Collection c = (Collection) targetObject;
      if (idx >= c.size()) {
        throw new SpelEvaluationException(getStartPosition(),SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS, c.size(), idx);
      }
      if (targetObject instanceof List) {
        List list = (List)targetObject;
        Object possiblyConvertedValue = state.convertValue(newValue,TypeDescriptor.valueOf(targetObjectTypeDescriptor.getElementType()));
        list.set(idx,possiblyConvertedValue);
      }
      else {
        throw new SpelEvaluationException(getStartPosition(),SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, contextObject.getClass().getName());
      }
    } else {
      throw new SpelEvaluationException(getStartPosition(),SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, contextObject.getClass().getName());
    }
  }
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.