use of org.springframework.expression.ConstructorResolver in project spring-framework by spring-projects.
the class ConstructorReference method findExecutorForConstructor.
/**
* Go through the list of registered constructor resolvers and see if any can find a
* constructor that takes the specified set of arguments.
* @param typeName the type trying to be constructed
* @param argumentTypes the types of the arguments supplied that the constructor must take
* @param state the current state of the expression
* @return a reusable ConstructorExecutor that can be invoked to run the constructor or null
* @throws SpelEvaluationException if there is a problem locating the constructor
*/
private ConstructorExecutor findExecutorForConstructor(String typeName, List<TypeDescriptor> argumentTypes, ExpressionState state) throws SpelEvaluationException {
EvaluationContext evalContext = state.getEvaluationContext();
List<ConstructorResolver> ctorResolvers = evalContext.getConstructorResolvers();
if (ctorResolvers != null) {
for (ConstructorResolver ctorResolver : ctorResolvers) {
try {
ConstructorExecutor ce = ctorResolver.resolve(state.getEvaluationContext(), typeName, argumentTypes);
if (ce != null) {
return ce;
}
} 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));
}
use of org.springframework.expression.ConstructorResolver in project spring-framework by spring-projects.
the class ConstructorInvocationTests method testAddingConstructorResolvers.
@Test
public void testAddingConstructorResolvers() {
StandardEvaluationContext ctx = new StandardEvaluationContext();
// reflective constructor accessor is the only one by default
List<ConstructorResolver> constructorResolvers = ctx.getConstructorResolvers();
assertThat(constructorResolvers.size()).isEqualTo(1);
ConstructorResolver dummy = new DummyConstructorResolver();
ctx.addConstructorResolver(dummy);
assertThat(ctx.getConstructorResolvers().size()).isEqualTo(2);
List<ConstructorResolver> copy = new ArrayList<>(ctx.getConstructorResolvers());
assertThat(ctx.removeConstructorResolver(dummy)).isTrue();
assertThat(ctx.removeConstructorResolver(dummy)).isFalse();
assertThat(ctx.getConstructorResolvers().size()).isEqualTo(1);
ctx.setConstructorResolvers(copy);
assertThat(ctx.getConstructorResolvers().size()).isEqualTo(2);
}
Aggregations