Java Code Examples for org.springframework.expression.spel.support.ReflectiveMethodResolver

The following examples show how to use org.springframework.expression.spel.support.ReflectiveMethodResolver. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   Source File: MethodReference.java    License: MIT License 6 votes vote down vote up
@Nullable
private MethodExecutor getCachedExecutor(EvaluationContext evaluationContext, Object value,
		@Nullable TypeDescriptor target, List<TypeDescriptor> argumentTypes) {

	List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
	if (methodResolvers.size() != 1 || !(methodResolvers.get(0) instanceof ReflectiveMethodResolver)) {
		// Not a default ReflectiveMethodResolver - don't know whether caching is valid
		return null;
	}

	CachedMethodExecutor executorToCheck = this.cachedExecutor;
	if (executorToCheck != null && executorToCheck.isSuitable(value, target, argumentTypes)) {
		return executorToCheck.get();
	}
	this.cachedExecutor = null;
	return null;
}
 
Example 2
Source Project: spring-analysis-note   Source File: SpelReproTests.java    License: MIT License 6 votes vote down vote up
/**
 * Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a
 * method accepting 'long' is ok.
 */
@Test
public void wideningPrimitiveConversion_SPR8224() throws Exception {

	class WideningPrimitiveConversion {
		public int getX(long i) {
			return 10;
		}
	}

	final Integer INTEGER_VALUE = Integer.valueOf(7);
	WideningPrimitiveConversion target = new WideningPrimitiveConversion();
	EvaluationContext emptyEvalContext = new StandardEvaluationContext();

	List<TypeDescriptor> args = new ArrayList<>();
	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);
}
 
Example 3
Source Project: spring-analysis-note   Source File: SpelReproTests.java    License: MIT License 6 votes vote down vote up
/**
 * Test the ability to subclass the ReflectiveMethodResolver and change how it
 * determines the set of methods for a type.
 */
@Test
public void customStaticFunctions_SPR9038() {
	ExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext context = new StandardEvaluationContext();
	List<MethodResolver> methodResolvers = new ArrayList<>();
	methodResolvers.add(new ReflectiveMethodResolver() {
		@Override
		protected Method[] getMethods(Class<?> type) {
			try {
				return new Method[] {Integer.class.getDeclaredMethod("parseInt", String.class, Integer.TYPE)};
			}
			catch (NoSuchMethodException ex) {
				return new Method[0];
			}
		}
	});

	context.setMethodResolvers(methodResolvers);
	Expression expression = parser.parseExpression("parseInt('-FF', 16)");

	Integer result = expression.getValue(context, "", Integer.class);
	assertEquals(-255, result.intValue());
}
 
Example 4
Source Project: java-technology-stack   Source File: MethodReference.java    License: MIT License 6 votes vote down vote up
@Nullable
private MethodExecutor getCachedExecutor(EvaluationContext evaluationContext, Object value,
		@Nullable TypeDescriptor target, List<TypeDescriptor> argumentTypes) {

	List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
	if (methodResolvers.size() != 1 || !(methodResolvers.get(0) instanceof ReflectiveMethodResolver)) {
		// Not a default ReflectiveMethodResolver - don't know whether caching is valid
		return null;
	}

	CachedMethodExecutor executorToCheck = this.cachedExecutor;
	if (executorToCheck != null && executorToCheck.isSuitable(value, target, argumentTypes)) {
		return executorToCheck.get();
	}
	this.cachedExecutor = null;
	return null;
}
 
Example 5
Source Project: java-technology-stack   Source File: SpelReproTests.java    License: MIT License 6 votes vote down vote up
/**
 * Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a
 * method accepting 'long' is ok.
 */
@Test
public void wideningPrimitiveConversion_SPR8224() throws Exception {

	class WideningPrimitiveConversion {
		public int getX(long i) {
			return 10;
		}
	}

	final Integer INTEGER_VALUE = Integer.valueOf(7);
	WideningPrimitiveConversion target = new WideningPrimitiveConversion();
	EvaluationContext emptyEvalContext = new StandardEvaluationContext();

	List<TypeDescriptor> args = new ArrayList<>();
	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);
}
 
Example 6
Source Project: java-technology-stack   Source File: SpelReproTests.java    License: MIT License 6 votes vote down vote up
/**
 * Test the ability to subclass the ReflectiveMethodResolver and change how it
 * determines the set of methods for a type.
 */
@Test
public void customStaticFunctions_SPR9038() {
	ExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext context = new StandardEvaluationContext();
	List<MethodResolver> methodResolvers = new ArrayList<>();
	methodResolvers.add(new ReflectiveMethodResolver() {
		@Override
		protected Method[] getMethods(Class<?> type) {
			try {
				return new Method[] {Integer.class.getDeclaredMethod("parseInt", String.class, Integer.TYPE)};
			}
			catch (NoSuchMethodException ex) {
				return new Method[0];
			}
		}
	});

	context.setMethodResolvers(methodResolvers);
	Expression expression = parser.parseExpression("parseInt('-FF', 16)");

	Integer result = expression.getValue(context, "", Integer.class);
	assertEquals(-255, result.intValue());
}
 
Example 7
Source Project: lams   Source File: MethodReference.java    License: GNU General Public License v2.0 6 votes vote down vote up
private MethodExecutor getCachedExecutor(EvaluationContext evaluationContext, Object value,
		TypeDescriptor target, List<TypeDescriptor> argumentTypes) {

	List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
	if (methodResolvers == null || methodResolvers.size() != 1 ||
			!(methodResolvers.get(0) instanceof ReflectiveMethodResolver)) {
		// Not a default ReflectiveMethodResolver - don't know whether caching is valid
		return null;
	}

	CachedMethodExecutor executorToCheck = this.cachedExecutor;
	if (executorToCheck != null && executorToCheck.isSuitable(value, target, argumentTypes)) {
		return executorToCheck.get();
	}
	this.cachedExecutor = null;
	return null;
}
 
Example 8
Source Project: spring4-understanding   Source File: MethodReference.java    License: Apache License 2.0 6 votes vote down vote up
private MethodExecutor getCachedExecutor(EvaluationContext evaluationContext, Object value,
		TypeDescriptor target, List<TypeDescriptor> argumentTypes) {

	List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
	if (methodResolvers == null || methodResolvers.size() != 1 ||
			!(methodResolvers.get(0) instanceof ReflectiveMethodResolver)) {
		// Not a default ReflectiveMethodResolver - don't know whether caching is valid
		return null;
	}

	CachedMethodExecutor executorToCheck = this.cachedExecutor;
	if (executorToCheck != null && executorToCheck.isSuitable(value, target, argumentTypes)) {
		return executorToCheck.get();
	}
	this.cachedExecutor = null;
	return null;
}
 
Example 9
Source Project: spring4-understanding   Source File: SpelReproTests.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a
 * method accepting 'long' is ok.
 */
@Test
public void wideningPrimitiveConversion_8224() throws Exception {

	class WideningPrimitiveConversion {
		public int getX(long i) {
			return 10;
		}
	}

	final Integer INTEGER_VALUE = Integer.valueOf(7);
	WideningPrimitiveConversion target = new WideningPrimitiveConversion();
	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);
}
 
Example 10
Source Project: spring4-understanding   Source File: SpelReproTests.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Test the ability to subclass the ReflectiveMethodResolver and change how it
 * determines the set of methods for a type.
 */
@Test
public void customStaticFunctions_SPR9038() {
	ExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext context = new StandardEvaluationContext();
	List<MethodResolver> methodResolvers = new ArrayList<MethodResolver>();
	methodResolvers.add(new ReflectiveMethodResolver() {
		@Override
		protected Method[] getMethods(Class<?> type) {
			try {
				return new Method[] {
						Integer.class.getDeclaredMethod("parseInt", new Class<?>[] {String.class, Integer.TYPE})};
			}
			catch (NoSuchMethodException ex) {
				return new Method[0];
			}
		}
	});

	context.setMethodResolvers(methodResolvers);
	Expression expression = parser.parseExpression("parseInt('-FF', 16)");

	Integer result = expression.getValue(context, "", Integer.class);
	assertEquals(-255, result.intValue());
}
 
Example 11
public DefaultEntitySanitizer(VerifierMode verifierMode,
                              List<Function<Object, Optional<Object>>> sanitizers,
                              boolean annotationSanitizersEnabled,
                              boolean stdValueSanitizersEnabled,
                              Function<Class<?>, Boolean> includesPredicate,
                              Function<String, Optional<Object>> templateResolver,
                              Map<String, Method> registeredFunctions,
                              Map<String, Object> registeredBeans,
                              Function<Class<?>, Optional<ConstraintValidator<?, ?>>> applicationValidatorFactory) {

    Supplier<EvaluationContext> spelContextFactory = () -> {
        StandardEvaluationContext context = new StandardEvaluationContext();
        registeredFunctions.forEach(context::registerFunction);
        context.setBeanResolver((ctx, beanName) -> registeredBeans.get(beanName));
        context.setMethodResolvers(Collections.singletonList(new ReflectiveMethodResolver()));
        return context;
    };

    this.validator = Validation.buildDefaultValidatorFactory()
            .usingContext()
            .constraintValidatorFactory(new ConstraintValidatorFactoryWrapper(verifierMode, applicationValidatorFactory, spelContextFactory))
            .messageInterpolator(new SpELMessageInterpolator(spelContextFactory))
            .getValidator();

    List<Function<Object, Optional<Object>>> allSanitizers = new ArrayList<>();
    if (annotationSanitizersEnabled) {
        allSanitizers.add(new AnnotationBasedSanitizer(spelContextFactory.get(), includesPredicate));
    }
    if (stdValueSanitizersEnabled) {
        allSanitizers.add(new StdValueSanitizer(includesPredicate));
    }
    allSanitizers.add(new TemplateSanitizer(templateResolver, includesPredicate));
    allSanitizers.addAll(sanitizers);
    this.sanitizers = allSanitizers;
}
 
Example 12
Source Project: titus-control-plane   Source File: TestValidator.java    License: Apache License 2.0 5 votes vote down vote up
public static Validator testValidator(String alias, Object bean) {
    Supplier<EvaluationContext> spelContextFactory = () -> {
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setBeanResolver((ctx, beanName) -> beanName.equals(alias) ? bean : null);
        context.setMethodResolvers(Collections.singletonList(new ReflectiveMethodResolver()));
        return context;
    };

    return Validation.buildDefaultValidatorFactory()
            .usingContext()
            .constraintValidatorFactory(new ConstraintValidatorFactoryWrapper(VerifierMode.Strict, type -> Optional.empty(), spelContextFactory))
            .messageInterpolator(new SpELMessageInterpolator(spelContextFactory))
            .getValidator();
}
 
Example 13
Source Project: spring-analysis-note   Source File: SpelReproTests.java    License: MIT License 4 votes vote down vote up
@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);
}
 
Example 14
Source Project: java-technology-stack   Source File: SpelReproTests.java    License: MIT License 4 votes vote down vote up
@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);
}
 
Example 15
Source Project: spring4-understanding   Source File: SpelReproTests.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void varargsAndPrimitives_SPR8174() throws Exception {
	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);
}