use of cn.taketoday.expression.MethodExecutor in project today-infrastructure by TAKETODAY.
the class SpelReproTests method varargsAndPrimitives_SPR8174.
@Test
void varargsAndPrimitives_SPR8174() throws Exception {
EvaluationContext emptyEvalContext = new StandardEvaluationContext();
List<TypeDescriptor> args = new ArrayList<>();
args.add(TypeDescriptor.fromObject(34L));
ReflectionUtil<Integer> ru = new ReflectionUtil<>();
MethodExecutor me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "methodToCall", args);
args.set(0, TypeDescriptor.fromObject(23));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, 45);
args.set(0, TypeDescriptor.fromObject(23f));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, 45f);
args.set(0, TypeDescriptor.fromObject(23d));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, 23d);
args.set(0, TypeDescriptor.fromObject((short) 23));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, (short) 23);
args.set(0, TypeDescriptor.fromObject(23L));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, 23L);
args.set(0, TypeDescriptor.fromObject((char) 65));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, (char) 65);
args.set(0, TypeDescriptor.fromObject((byte) 23));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, (byte) 23);
args.set(0, TypeDescriptor.fromObject(true));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, true);
// trickier:
args.set(0, TypeDescriptor.fromObject(12));
args.add(TypeDescriptor.fromObject(23f));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "bar", args);
me.execute(emptyEvalContext, ru, 12, 23f);
}
use of cn.taketoday.expression.MethodExecutor in project today-infrastructure by TAKETODAY.
the class SpelReproTests method wideningPrimitiveConversion_SPR8224.
/**
* Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a
* method accepting 'long' is ok.
*/
@Test
void wideningPrimitiveConversion_SPR8224() throws Exception {
class WideningPrimitiveConversion {
public int getX(long i) {
return 10;
}
}
final Integer INTEGER_VALUE = 7;
WideningPrimitiveConversion target = new WideningPrimitiveConversion();
EvaluationContext emptyEvalContext = new StandardEvaluationContext();
List<TypeDescriptor> args = new ArrayList<>();
args.add(TypeDescriptor.fromObject(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);
assertThat(actual).isEqualTo(compiler);
}
use of cn.taketoday.expression.MethodExecutor in project today-infrastructure by TAKETODAY.
the class Spr7538Tests method repro.
@Test
void repro() throws Exception {
AlwaysTrueReleaseStrategy target = new AlwaysTrueReleaseStrategy();
BeanFactoryTypeConverter converter = new BeanFactoryTypeConverter();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setTypeConverter(converter);
List<Foo> arguments = Collections.emptyList();
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.fromObject(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 cn.taketoday.expression.MethodExecutor in project today-framework by TAKETODAY.
the class SpelReproTests method SPR9495.
@Test
void SPR9495() {
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);
assertThat(result).isNotNull();
assertThat(result.getClass().isArray()).isTrue();
assertThat(Array.get(result, 0)).isEqualTo(ABC.A);
assertThat(Array.get(result, 1)).isEqualTo(ABC.B);
assertThat(Array.get(result, 2)).isEqualTo(ABC.C);
context.addMethodResolver(new MethodResolver() {
@Override
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List<TypeDescriptor> argumentTypes) throws AccessException {
return (context1, target, arguments) -> {
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);
assertThat(result).isNotNull();
assertThat(result.getClass().isArray()).isTrue();
assertThat(Array.get(result, 0)).isEqualTo(XYZ.X);
assertThat(Array.get(result, 1)).isEqualTo(XYZ.Y);
assertThat(Array.get(result, 2)).isEqualTo(XYZ.Z);
}
use of cn.taketoday.expression.MethodExecutor in project today-framework by TAKETODAY.
the class SpelReproTests method wideningPrimitiveConversion_SPR8224.
/**
* Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a
* method accepting 'long' is ok.
*/
@Test
void wideningPrimitiveConversion_SPR8224() throws Exception {
class WideningPrimitiveConversion {
public int getX(long i) {
return 10;
}
}
final Integer INTEGER_VALUE = 7;
WideningPrimitiveConversion target = new WideningPrimitiveConversion();
EvaluationContext emptyEvalContext = new StandardEvaluationContext();
List<TypeDescriptor> args = new ArrayList<>();
args.add(TypeDescriptor.fromObject(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);
assertThat(actual).isEqualTo(compiler);
}
Aggregations