use of org.springframework.expression.MethodExecutor in project spring-framework by spring-projects.
the class SpelReproTests method SPR9495.
@Test
public void SPR9495() throws Exception {
SpelParserConfiguration configuration = new SpelParserConfiguration(false, false);
ExpressionParser parser = new SpelExpressionParser(configuration);
StandardEvaluationContext context = new StandardEvaluationContext();
Expression spel = parser.parseExpression("#enumType.values()");
context.setVariable("enumType", ABC.class);
Object result = spel.getValue(context);
assertNotNull(result);
assertTrue(result.getClass().isArray());
assertEquals(ABC.A, Array.get(result, 0));
assertEquals(ABC.B, Array.get(result, 1));
assertEquals(ABC.C, Array.get(result, 2));
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");
Object value = method.invoke(target, arguments);
return new TypedValue(value, new TypeDescriptor(new MethodParameter(method, -1)).narrow(value));
} catch (Exception ex) {
throw new AccessException(ex.getMessage(), ex);
}
}
};
}
});
result = spel.getValue(context);
assertNotNull(result);
assertTrue(result.getClass().isArray());
assertEquals(XYZ.X, Array.get(result, 0));
assertEquals(XYZ.Y, Array.get(result, 1));
assertEquals(XYZ.Z, Array.get(result, 2));
}
use of org.springframework.expression.MethodExecutor in project spring-framework by spring-projects.
the class Spr7538Tests method repro.
@Ignore
@Test
public void repro() throws Exception {
AlwaysTrueReleaseStrategy target = new AlwaysTrueReleaseStrategy();
BeanFactoryTypeConverter converter = new BeanFactoryTypeConverter();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setTypeConverter(converter);
List<Foo> arguments = new ArrayList<>();
// !!!! With the below line commented you'll get NPE. Uncomment and everything is OK!
//arguments.add(new Foo());
List<TypeDescriptor> paramDescriptors = new ArrayList<>();
Method method = AlwaysTrueReleaseStrategy.class.getMethod("checkCompleteness", List.class);
paramDescriptors.add(new TypeDescriptor(new MethodParameter(method, 0)));
List<TypeDescriptor> argumentTypes = new ArrayList<>();
argumentTypes.add(TypeDescriptor.forObject(arguments));
ReflectiveMethodResolver resolver = new ReflectiveMethodResolver();
MethodExecutor executor = resolver.resolve(context, target, "checkCompleteness", argumentTypes);
Object result = executor.execute(context, target, arguments);
System.out.println("Result: " + result);
}
use of org.springframework.expression.MethodExecutor in project spring-framework by spring-projects.
the class MethodReference method getValueInternal.
private TypedValue getValueInternal(EvaluationContext evaluationContext, Object value, TypeDescriptor targetType, Object[] arguments) {
List<TypeDescriptor> argumentTypes = getArgumentTypes(arguments);
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, SpelMessage.EXCEPTION_DURING_METHOD_INVOCATION, this.name, value.getClass().getName(), ex.getMessage());
}
}
use of org.springframework.expression.MethodExecutor in project spring-framework by spring-projects.
the class SpelReproTests method conversionPriority_8224.
/**
* Test whether {@link ReflectiveMethodResolver} follows Java Method Invocation
* Conversion order. And more precisely that widening reference conversion is 'higher'
* than a unboxing conversion.
*/
@Test
public void conversionPriority_8224() throws Exception {
@SuppressWarnings("unused")
class ConversionPriority1 {
public int getX(Number i) {
return 20;
}
public int getX(int i) {
return 10;
}
}
@SuppressWarnings("unused")
class ConversionPriority2 {
public int getX(int i) {
return 10;
}
public int getX(Number i) {
return 20;
}
}
final Integer INTEGER = Integer.valueOf(7);
EvaluationContext emptyEvalContext = new StandardEvaluationContext();
List<TypeDescriptor> args = new ArrayList<>();
args.add(TypeDescriptor.forObject(new Integer(42)));
ConversionPriority1 target = new ConversionPriority1();
MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args);
// MethodInvoker chooses getX(int i) when passing Integer
final int actual = (Integer) me.execute(emptyEvalContext, target, new Integer(42)).getValue();
// Compiler chooses getX(Number i) when passing Integer
final int compiler = target.getX(INTEGER);
// Fails!
assertEquals(compiler, actual);
ConversionPriority2 target2 = new ConversionPriority2();
MethodExecutor me2 = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target2, "getX", args);
// MethodInvoker chooses getX(int i) when passing Integer
int actual2 = (Integer) me2.execute(emptyEvalContext, target2, new Integer(42)).getValue();
// Compiler chooses getX(Number i) when passing Integer
int compiler2 = target2.getX(INTEGER);
// Fails!
assertEquals(compiler2, actual2);
}
use of org.springframework.expression.MethodExecutor in project spring-framework by spring-projects.
the class SpelReproTests method varargsAndPrimitives_SPR8174.
@Test
public void varargsAndPrimitives_SPR8174() throws Exception {
EvaluationContext emptyEvalContext = new StandardEvaluationContext();
List<TypeDescriptor> args = new ArrayList<>();
args.add(TypeDescriptor.forObject(34L));
ReflectionUtil<Integer> ru = new ReflectionUtil<>();
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);
}
Aggregations