Package org.springframework.expression.spel

Examples of org.springframework.expression.spel.SpelEvaluationException


            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


            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

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

  private TypedValue createArray(ExpressionState state) throws EvaluationException {

    // First child gives us the array type which will either be a primitive or reference type
    Object intendedArrayType = getChild(0).getValue(state);
    if (!(intendedArrayType instanceof String)) {
      throw new SpelEvaluationException(getChild(0).getStartPosition(),
          SpelMessage.TYPE_NAME_EXPECTED_FOR_ARRAY_CONSTRUCTION, FormatHelper
              .formatClassNameForMessage(intendedArrayType.getClass()));
    }
    String type = (String) intendedArrayType;
    Class<?> componentType = null;
    TypeCode arrayTypeCode = TypeCode.forName(type);
    if (arrayTypeCode == TypeCode.OBJECT) {
      componentType = state.findType(type);
    } else {
      componentType = arrayTypeCode.getType();
    }

    TypeDescriptor td = TypeDescriptor.valueOf(componentType);

    Object newArray = null;

    if (!hasInitializer()) {
      // Confirm all dimensions were specified (for example [3][][5] is missing the 2nd dimension)
      for (int i = 0; i < dimensions.length; i++) {
        if (dimensions[i] == null) {
          throw new SpelEvaluationException(getStartPosition(), SpelMessage.MISSING_ARRAY_DIMENSION);
        }
      }
      TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();

      // Shortcut for 1 dimensional
      if (dimensions.length == 1) {
        TypedValue o = dimensions[0].getTypedValue(state);
        int arraySize = ExpressionUtils.toInt(typeConverter, o);
        newArray = Array.newInstance(componentType, arraySize);
      } else {
        // Multi-dimensional - hold onto your hat!
        int[] dims = new int[dimensions.length];
        for (int d = 0; d < dimensions.length; d++) {
          TypedValue o = dimensions[d].getTypedValue(state);
          dims[d] = ExpressionUtils.toInt(typeConverter, o);
        }
        newArray = Array.newInstance(componentType, dims);
      }
    } else {
      // There is an initializer
      if (dimensions.length > 1) {
        // There is an initializer but this is a multi-dimensional array (e.g. new int[][]{{1,2},{3,4}}) - this
        // is not currently supported
        throw new SpelEvaluationException(getStartPosition(),
            SpelMessage.MULTIDIM_ARRAY_INITIALIZER_NOT_SUPPORTED);
      }
      TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter();
      InlineList initializer = (InlineList) getChild(1);
      // If a dimension was specified, check it matches the initializer length
      if (dimensions[0] != null) {
        TypedValue dValue = dimensions[0].getTypedValue(state);
        int i = ExpressionUtils.toInt(typeConverter, dValue);
        if (i != initializer.getChildCount()) {
          throw new SpelEvaluationException(getStartPosition(), SpelMessage.INITIALIZER_LENGTH_INCORRECT);
        }
      }
      // Build the array and populate it
      int arraySize = initializer.getChildCount();
      newArray = Array.newInstance(componentType, arraySize);
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.
       
        throwSimpleExceptionIfPossible(state, ae);
       
        // 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) {
      // Same unwrapping exception handling as above in above catch block
      throwSimpleExceptionIfPossible(state, 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

    // Indexing into a Map
    if (targetObject instanceof Map) {
      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);
      }
    }
   
    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()) {
        return new TypedValue(accessArrayElement(targetObject, idx), targetObjectTypeDescriptor.getElementTypeDescriptor());
      } else if (targetObject instanceof Collection) {
        Collection c = (Collection) targetObject;
        if (idx >= c.size()) {
          if (!growCollection(state, targetObjectTypeDescriptor.getElementType(), 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.getElementTypeDescriptor());
          }
          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.asString());
      }
    }
     
    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());
      return;
    }
    else if (targetObjectTypeDescriptor.isCollection()) {
      int idx = (Integer)state.convertValue(index, TypeDescriptor.valueOf(Integer.class));
      Collection c = (Collection) targetObject;
      if (idx >= c.size()) {
        if (!growCollection(state, targetObjectTypeDescriptor.getElementType(), idx, c)) {
          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, targetObjectTypeDescriptor.getElementTypeDescriptor());
        list.set(idx,possiblyConvertedValue);
        return;
      }
      else {
        throw new SpelEvaluationException(getStartPosition(),SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, targetObjectTypeDescriptor.asString());
      }
    }
   
    // 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.asString());
  }
View Full Code Here

    if (state.getConfiguration().isAutoGrowCollections()) {
      Object newCollectionElement = null;
      try {
        int newElements = index-collection.size();
        if (elementType == null || elementType == Object.class) {
          throw new SpelEvaluationException(getStartPosition(), SpelMessage.UNABLE_TO_GROW_COLLECTION_UNKNOWN_ELEMENT_TYPE)
        }
        while (newElements>0) {
          collection.add(elementType.newInstance());
          newElements--;
        }
        newCollectionElement = elementType.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

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.