Package org.springframework.expression.spel

Examples of org.springframework.expression.spel.SpelEvaluationException


        targetType = parameterTypes[i];
      }
      try {
        if (arguments[i] != null && arguments[i].getClass() != targetType) {
          if (converter == null) {
            throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, arguments[i].getClass().getName(),targetType);
          }
          arguments[i] = converter.convertValue(arguments[i], TypeDescriptor.forObject(arguments[i]), TypeDescriptor.valueOf(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[i].getClass().getName(),targetType);
        }
      }
    }
  }
View Full Code Here


          // User exception was the root cause - exit now
          if (rootCause instanceof RuntimeException) {
            throw (RuntimeException)rootCause;
          } else {
            String typename = (String) children[0].getValueInternal(state).getValue();
            throw new SpelEvaluationException(getStartPosition(), rootCause, SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM,
                typename,FormatHelper.formatMethodForMessage("", argumentTypes));
          }
        }
       
        // at this point we know it wasn't a user problem so worth a retry if a better candidate can be found
        this.cachedExecutor = null;
      }
    }

    // either there was no accessor or it no longer exists
    String typename = (String) children[0].getValueInternal(state).getValue();
    executorToUse = findExecutorForConstructor(typename, argumentTypes, state);
    try {
      this.cachedExecutor = executorToUse;
      TypedValue result = executorToUse.execute(state.getEvaluationContext(), arguments);
      return result;
    } catch (AccessException ae) {
      throw new SpelEvaluationException(getStartPosition(), ae, SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typename,
          FormatHelper.formatMethodForMessage("", argumentTypes));

    }
  }
View Full Code Here

              argumentTypes);
          if (cEx != null) {
            return cEx;
          }
        } catch (AccessException ex) {
          throw new SpelEvaluationException(getStartPosition(),ex, SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typename,
              FormatHelper.formatMethodForMessage("", argumentTypes));
        }
      }
    }
    throw new SpelEvaluationException(getStartPosition(),SpelMessage.CONSTRUCTOR_NOT_FOUND, typename, FormatHelper.formatMethodForMessage("",
        argumentTypes));
  }
View Full Code Here

  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

    }
    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) {
        // Two reasons this can occur:
        // 1. the method invoked actually threw a real exception
        // 2. the method invoked was not passed the arguments it expected and has become 'stale'
       
        // In the first case we should not retry, in the second case we should see if there is a
        // better suited method.
       
        // To determine which situation it is, the AccessException will contain a cause - this
        // will be the exception thrown by the reflective invocation.  Inside this exception there
        // may or may not be a root cause.  If there is a root cause it is a user created exception.
        // If there is no root cause it was a reflective invocation problem.
       
        Throwable causeOfAccessException = ae.getCause();
        Throwable rootCause = (causeOfAccessException==null?null:causeOfAccessException.getCause());
        if (rootCause!=null) {
          // User exception was the root cause - exit now
          if (rootCause instanceof RuntimeException) {
            throw (RuntimeException)rootCause;
          } else {
            throw new SpelEvaluationException( getStartPosition(), rootCause, SpelMessage.EXCEPTION_DURING_METHOD_INVOCATION,
              this.name, state.getActiveContextObject().getValue().getClass().getName(), rootCause.getMessage());
          }
        }
       
        // at this point we know it wasn't a user problem so worth a retry if a better candidate can be found
        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

              }
              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,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, op.getTypeDescriptor().getElementTypeDescriptor()));
          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, op.getTypeDescriptor().getElementTypeDescriptor());
              }
              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), op.getTypeDescriptor().getElementTypeDescriptor());
      }
      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) {
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)));
    }
    catch (Exception ex) {
      throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL,
          this.name, ex.getMessage());
    }
  }
View Full Code Here

              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

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.