Package org.springframework.expression

Examples of org.springframework.expression.MethodExecutor


    EvaluationContext emptyEvalContext = new StandardEvaluationContext();

    List<TypeDescriptor> args = new ArrayList<TypeDescriptor>();
    args.add(TypeDescriptor.forObject(INTEGER_VALUE));

    MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args);
    final int actual = (Integer) me.execute(emptyEvalContext, target, INTEGER_VALUE).getValue();

    final int compiler = target.getX(INTEGER_VALUE);
    assertEquals(compiler, actual);
  }
View Full Code Here


    EvaluationContext emptyEvalContext = new StandardEvaluationContext();
    List<TypeDescriptor> args = new ArrayList<TypeDescriptor>();

    args.add(TypeDescriptor.forObject(34L));
    ReflectionUtil<Integer> ru = new ReflectionUtil<Integer>();
    MethodExecutor me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "methodToCall", args);

    args.set(0, TypeDescriptor.forObject(23));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 45);

    args.set(0, TypeDescriptor.forObject(23f));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 45f);

    args.set(0, TypeDescriptor.forObject(23d));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 23d);

    args.set(0, TypeDescriptor.forObject((short) 23));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, (short) 23);

    args.set(0, TypeDescriptor.forObject(23L));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 23L);

    args.set(0, TypeDescriptor.forObject((char) 65));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, (char) 65);

    args.set(0, TypeDescriptor.forObject((byte) 23));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, (byte) 23);

    args.set(0, TypeDescriptor.forObject(true));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, true);

    // trickier:
    args.set(0, TypeDescriptor.forObject(12));
    args.add(TypeDescriptor.forObject(23f));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "bar", args);
    me.execute(emptyEvalContext, ru, 12, 23f);
  }
View Full Code Here

    context.addMethodResolver(new MethodResolver() {
      @Override
      public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name,
          List<TypeDescriptor> argumentTypes) throws AccessException {
        return new MethodExecutor() {
          @Override
          public TypedValue execute(EvaluationContext context, Object target, Object... arguments)
              throws AccessException {
            try {
              Method method = XYZ.class.getMethod("values");
View Full Code Here

        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

    List<MethodResolver> mResolvers = eContext.getMethodResolvers();
    if (mResolvers != null) {
      for (MethodResolver methodResolver : mResolvers) {
        try {
          MethodExecutor cEx = methodResolver.resolve(
              state.getEvaluationContext(), contextObject, name, argumentTypes);
          if (cEx != null) {
            return cEx;
          }
        }
View Full Code Here

        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,
View Full Code Here

    List<MethodResolver> mResolvers = eContext.getMethodResolvers();
    if (mResolvers != null) {
      for (MethodResolver methodResolver : mResolvers) {
        try {
          MethodExecutor cEx = methodResolver.resolve(
              state.getEvaluationContext(), contextObject, name, argumentTypes);
          if (cEx != null) {
            return cEx;
          }
        }
View Full Code Here

    if (value == null) {
      throwIfNotNullSafe(argumentTypes);
      return TypedValue.NULL;
    }

    MethodExecutor executorToUse = getCachedExecutor(evaluationContext, value, targetType, argumentTypes);
    if (executorToUse != null) {
      try {
        return executorToUse.execute(evaluationContext, value, arguments);
      }
      catch (AccessException ex) {
        // 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 the situation, the AccessException will contain a cause.
        // If the cause is an InvocationTargetException, a user exception was
        // thrown inside the method. Otherwise the method could not be invoked.
        throwSimpleExceptionIfPossible(value, ex);

        // 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, argumentTypes, value, evaluationContext);
    this.cachedExecutor = new CachedMethodExecutor(
        executorToUse, (value instanceof Class ? (Class<?>) value : null), targetType, argumentTypes);
    try {
      return executorToUse.execute(evaluationContext, value, arguments);
    }
    catch (AccessException ex) {
      // Same unwrapping exception handling as above in above catch block
      throwSimpleExceptionIfPossible(value, ex);
      throw new SpelEvaluationException(getStartPosition(), ex,
View Full Code Here

    List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
    if (methodResolvers != null) {
      for (MethodResolver methodResolver : methodResolvers) {
        try {
          MethodExecutor methodExecutor = methodResolver.resolve(
              evaluationContext, targetObject, name, argumentTypes);
          if (methodExecutor != null) {
            return methodExecutor;
          }
        }
View Full Code Here

TOP

Related Classes of org.springframework.expression.MethodExecutor

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.