Search in sources :

Example 1 with ConstructorResolver

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));
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) AccessException(org.springframework.expression.AccessException) ConstructorResolver(org.springframework.expression.ConstructorResolver) ReflectiveConstructorExecutor(org.springframework.expression.spel.support.ReflectiveConstructorExecutor) ConstructorExecutor(org.springframework.expression.ConstructorExecutor) EvaluationContext(org.springframework.expression.EvaluationContext)

Example 2 with ConstructorResolver

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);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ConstructorResolver(org.springframework.expression.ConstructorResolver) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Aggregations

ConstructorResolver (org.springframework.expression.ConstructorResolver)2 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1 AccessException (org.springframework.expression.AccessException)1 ConstructorExecutor (org.springframework.expression.ConstructorExecutor)1 EvaluationContext (org.springframework.expression.EvaluationContext)1 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)1 ReflectiveConstructorExecutor (org.springframework.expression.spel.support.ReflectiveConstructorExecutor)1 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)1