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


  @Override
  public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    Object left = getLeftOperand().getValueInternal(state).getValue();
    Object right = getRightOperand().getValueInternal(state).getValue();
    if (!(right instanceof List) || ((List<?>) right).size() != 2) {
      throw new SpelEvaluationException(getRightOperand().getStartPosition(),
          SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST);
    }
    List<?> l = (List<?>) right;
    Object low = l.get(0);
    Object high = l.get(1);
View Full Code Here

    }
    if (currentContext.getValue() == null) {
      if (nullSafe) {
        return TypedValue.NULL;
      } else {
        throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_CALL_ON_NULL_OBJECT_NOT_ALLOWED,
            FormatHelper.formatMethodForMessage(name, getTypes(arguments)));
      }
    }

    MethodExecutor executorToUse = this.cachedExecutor;
    if (executorToUse != null) {
      try {
        return executorToUse.execute(
            state.getEvaluationContext(), state.getActiveContextObject().getValue(), arguments);
      }
      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.cachedExecutor = null;
      }
    }

    // either there was no accessor or it no longer existed
    executorToUse = findAccessorForMethod(this.name, getTypes(arguments), state);
    this.cachedExecutor = executorToUse;
    try {
      return executorToUse.execute(
          state.getEvaluationContext(), state.getActiveContextObject().getValue(), arguments);
    } catch (AccessException ae) {
      throw new SpelEvaluationException( getStartPosition(), ae, SpelMessage.EXCEPTION_DURING_METHOD_INVOCATION,
          this.name, state.getActiveContextObject().getValue().getClass().getName(), ae.getMessage());
    }
  }
View Full Code Here

          if (cEx != null) {
            return cEx;
          }
        }
        catch (AccessException ex) {
          throw new SpelEvaluationException(getStartPosition(),ex, SpelMessage.PROBLEM_LOCATING_METHOD, name, contextObject.getClass());
        }
      }
    }
    throw new SpelEvaluationException(getStartPosition(),SpelMessage.METHOD_NOT_FOUND, FormatHelper.formatMethodForMessage(name, argumentTypes),
        FormatHelper.formatClassNameForMessage(contextObject instanceof Class ? ((Class<?>) contextObject) : contextObject.getClass()));
  }
View Full Code Here

    if (left instanceof Comparable) {
      return ((Comparable) left).compareTo(right);
    }
   
    throw new SpelEvaluationException(SpelMessage.NOT_COMPARABLE, left.getClass(), right.getClass());
  }
View Full Code Here

                return new TypedValue(result);
              }
              result.put(entry.getKey(),entry.getValue());
            }
          } 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,TypeDescriptor.NULL);
      }
      if (variant == LAST) {
        Map resultMap = new HashMap();
        Object lastValue = result.get(lastKey);
        resultMap.put(lastKey,lastValue);
        return new TypedValue(resultMap,TypeDescriptor.valueOf(Map.class));
      }
      return new TypedValue(result,op.getTypeDescriptor());
    } 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,TypeDescriptor.valueOf(op.getTypeDescriptor().getElementType())));
          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,TypeDescriptor.valueOf(op.getTypeDescriptor().getElementType()));
              }
              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),TypeDescriptor.valueOf(op.getTypeDescriptor().getElementType()));
      }
      if (operand instanceof Collection) {
        return new TypedValue(result,op.getTypeDescriptor());
      }
      else {
        Class<?> elementType = ClassUtils.resolvePrimitiveIfNecessary(op.getTypeDescriptor().getElementType());
        Object resultArray = Array.newInstance(elementType, result.size());
        System.arraycopy(result.toArray(), 0, resultArray, 0, result.size());
        return new TypedValue(resultArray, op.getTypeDescriptor());
      }
    } else {
      if (operand==null) {
        if (nullSafe) {
          return TypedValue.NULL;
        } 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

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

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

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

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

    try {
      ReflectionUtils.makeAccessible(m);
      Object result = m.invoke(m.getClass(), functionArgs);
      return new TypedValue(result, new TypeDescriptor(new MethodParameter(m,-1)));
    } catch (IllegalArgumentException e) {
      throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL, name, e
          .getMessage());
    } catch (IllegalAccessException e) {
      throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL, name, e
          .getMessage());
    } catch (InvocationTargetException e) {
      throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL, name, e
          .getMessage());
    }
  }
View Full Code Here

              List newList = ArrayList.class.newInstance();
              writeProperty(state, name, newList);
              result = readProperty(state, this.name);
            }
          } catch (InstantiationException e) {
            throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_LIST_FOR_INDEXING);
          } catch (IllegalAccessException e) {
            throw new SpelEvaluationException(getStartPosition(), e, 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 e) {
            throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_MAP_FOR_INDEXING);
          } catch (IllegalAccessException e) {
            throw new SpelEvaluationException(getStartPosition(), e, 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 e) {
          throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_DYNAMICALLY_CREATE_OBJECT,result.getTypeDescriptor().getType());
        } catch (IllegalAccessException e) {
          throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_DYNAMICALLY_CREATE_OBJECT,result.getTypeDescriptor().getType());
        }       
      }
    }
    return result;
  }
View Full Code Here

            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

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.