org.springframework.expression.MethodResolver Java Examples

The following examples show how to use org.springframework.expression.MethodResolver. 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 File: MethodReference.java    From spring-analysis-note with 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 File: EvaluationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Verifies behavior requested in SPR-9621.
 */
@Test
public void customMethodFilter() {
	StandardEvaluationContext context = new StandardEvaluationContext();

	// Register a custom MethodResolver...
	List<MethodResolver> customResolvers = new ArrayList<>();
	customResolvers.add(new CustomMethodResolver());
	context.setMethodResolvers(customResolvers);

	// or simply...
	// context.setMethodResolvers(new ArrayList<MethodResolver>());

	// Register a custom MethodFilter...
	MethodFilter filter = new CustomMethodFilter();
	try {
		context.registerMethodFilter(String.class, filter);
		fail("should have failed");
	}
	catch (IllegalStateException ise) {
		assertEquals(
				"Method filter cannot be set as the reflective method resolver is not in use",
				ise.getMessage());
	}
}
 
Example #3
Source File: MethodInvocationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAddingMethodResolvers() {
	StandardEvaluationContext ctx = new StandardEvaluationContext();

	// reflective method accessor is the only one by default
	List<MethodResolver> methodResolvers = ctx.getMethodResolvers();
	assertEquals(1, methodResolvers.size());

	MethodResolver dummy = new DummyMethodResolver();
	ctx.addMethodResolver(dummy);
	assertEquals(2, ctx.getMethodResolvers().size());

	List<MethodResolver> copy = new ArrayList<>();
	copy.addAll(ctx.getMethodResolvers());
	assertTrue(ctx.removeMethodResolver(dummy));
	assertFalse(ctx.removeMethodResolver(dummy));
	assertEquals(1, ctx.getMethodResolvers().size());

	ctx.setMethodResolvers(copy);
	assertEquals(2, ctx.getMethodResolvers().size());
}
 
Example #4
Source File: SpelReproTests.java    From java-technology-stack with 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 #5
Source File: MethodReference.java    From lams with 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 #6
Source File: MethodReference.java    From java-technology-stack with 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 #7
Source File: SpelReproTests.java    From spring-analysis-note with 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 #8
Source File: MethodInvocationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAddingMethodResolvers() {
	StandardEvaluationContext ctx = new StandardEvaluationContext();

	// reflective method accessor is the only one by default
	List<MethodResolver> methodResolvers = ctx.getMethodResolvers();
	assertEquals(1, methodResolvers.size());

	MethodResolver dummy = new DummyMethodResolver();
	ctx.addMethodResolver(dummy);
	assertEquals(2, ctx.getMethodResolvers().size());

	List<MethodResolver> copy = new ArrayList<>();
	copy.addAll(ctx.getMethodResolvers());
	assertTrue(ctx.removeMethodResolver(dummy));
	assertFalse(ctx.removeMethodResolver(dummy));
	assertEquals(1, ctx.getMethodResolvers().size());

	ctx.setMethodResolvers(copy);
	assertEquals(2, ctx.getMethodResolvers().size());
}
 
Example #9
Source File: EvaluationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Verifies behavior requested in SPR-9621.
 */
@Test
public void customMethodFilter() {
	StandardEvaluationContext context = new StandardEvaluationContext();

	// Register a custom MethodResolver...
	List<MethodResolver> customResolvers = new ArrayList<>();
	customResolvers.add(new CustomMethodResolver());
	context.setMethodResolvers(customResolvers);

	// or simply...
	// context.setMethodResolvers(new ArrayList<MethodResolver>());

	// Register a custom MethodFilter...
	MethodFilter filter = new CustomMethodFilter();
	try {
		context.registerMethodFilter(String.class, filter);
		fail("should have failed");
	}
	catch (IllegalStateException ise) {
		assertEquals(
				"Method filter cannot be set as the reflective method resolver is not in use",
				ise.getMessage());
	}
}
 
Example #10
Source File: MethodReference.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private MethodExecutor findAccessorForMethod(String name, List<TypeDescriptor> argumentTypes,
		Object targetObject, EvaluationContext evaluationContext) throws SpelEvaluationException {

	List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
	if (methodResolvers != null) {
		for (MethodResolver methodResolver : methodResolvers) {
			try {
				MethodExecutor methodExecutor = methodResolver.resolve(
						evaluationContext, targetObject, name, argumentTypes);
				if (methodExecutor != null) {
					return methodExecutor;
				}
			}
			catch (AccessException ex) {
				throw new SpelEvaluationException(getStartPosition(), ex,
						SpelMessage.PROBLEM_LOCATING_METHOD, name, targetObject.getClass());
			}
		}
	}

	throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND,
			FormatHelper.formatMethodForMessage(name, argumentTypes),
			FormatHelper.formatClassNameForMessage(
					targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass()));
}
 
Example #11
Source File: MethodReference.java    From spring4-understanding with 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 #12
Source File: MethodReference.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private MethodExecutor findAccessorForMethod(String name, List<TypeDescriptor> argumentTypes,
		Object targetObject, EvaluationContext evaluationContext) throws SpelEvaluationException {

	List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
	if (methodResolvers != null) {
		for (MethodResolver methodResolver : methodResolvers) {
			try {
				MethodExecutor methodExecutor = methodResolver.resolve(
						evaluationContext, targetObject, name, argumentTypes);
				if (methodExecutor != null) {
					return methodExecutor;
				}
			}
			catch (AccessException ex) {
				throw new SpelEvaluationException(getStartPosition(), ex,
						SpelMessage.PROBLEM_LOCATING_METHOD, name, targetObject.getClass());
			}
		}
	}

	throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND,
			FormatHelper.formatMethodForMessage(name, argumentTypes),
			FormatHelper.formatClassNameForMessage(
					targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass()));
}
 
Example #13
Source File: EvaluationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Verifies behavior requested in SPR-9621.
 */
@Test
public void customMethodFilter() throws Exception {
	StandardEvaluationContext context = new StandardEvaluationContext();

	// Register a custom MethodResolver...
	List<MethodResolver> customResolvers = new ArrayList<MethodResolver>();
	customResolvers.add(new CustomMethodResolver());
	context.setMethodResolvers(customResolvers);

	// or simply...
	// context.setMethodResolvers(new ArrayList<MethodResolver>());

	// Register a custom MethodFilter...
	MethodFilter filter = new CustomMethodFilter();
	try {
		context.registerMethodFilter(String.class, filter);
		fail("should have failed");
	} catch (IllegalStateException ise) {
		assertEquals(
				"Method filter cannot be set as the reflective method resolver is not in use",
				ise.getMessage());
	}
}
 
Example #14
Source File: MethodInvocationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddingMethodResolvers() {
	StandardEvaluationContext ctx = new StandardEvaluationContext();

	// reflective method accessor is the only one by default
	List<MethodResolver> methodResolvers = ctx.getMethodResolvers();
	assertEquals(1, methodResolvers.size());

	MethodResolver dummy = new DummyMethodResolver();
	ctx.addMethodResolver(dummy);
	assertEquals(2, ctx.getMethodResolvers().size());

	List<MethodResolver> copy = new ArrayList<MethodResolver>();
	copy.addAll(ctx.getMethodResolvers());
	assertTrue(ctx.removeMethodResolver(dummy));
	assertFalse(ctx.removeMethodResolver(dummy));
	assertEquals(1, ctx.getMethodResolvers().size());

	ctx.setMethodResolvers(copy);
	assertEquals(2, ctx.getMethodResolvers().size());
}
 
Example #15
Source File: SpelReproTests.java    From spring4-understanding with 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 #16
Source File: StandardEvaluationContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private synchronized void initializeMethodResolvers() {
	if (this.methodResolvers == null) {
		List<MethodResolver> defaultResolvers = new ArrayList<MethodResolver>();
		this.reflectiveMethodResolver = new ReflectiveMethodResolver();
		defaultResolvers.add(this.reflectiveMethodResolver);
		this.methodResolvers = defaultResolvers;
	}
}
 
Example #17
Source File: StandardEvaluationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private synchronized void initializeMethodResolvers() {
	if (this.methodResolvers == null) {
		List<MethodResolver> defaultResolvers = new ArrayList<MethodResolver>();
		this.reflectiveMethodResolver = new ReflectiveMethodResolver();
		defaultResolvers.add(this.reflectiveMethodResolver);
		this.methodResolvers = defaultResolvers;
	}
}
 
Example #18
Source File: MethodReference.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private MethodExecutor findAccessorForMethod(List<TypeDescriptor> argumentTypes, Object targetObject,
		EvaluationContext evaluationContext) throws SpelEvaluationException {

	AccessException accessException = null;
	List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
	for (MethodResolver methodResolver : methodResolvers) {
		try {
			MethodExecutor methodExecutor = methodResolver.resolve(
					evaluationContext, targetObject, this.name, argumentTypes);
			if (methodExecutor != null) {
				return methodExecutor;
			}
		}
		catch (AccessException ex) {
			accessException = ex;
			break;
		}
	}

	String method = FormatHelper.formatMethodForMessage(this.name, argumentTypes);
	String className = FormatHelper.formatClassNameForMessage(
			targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass());
	if (accessException != null) {
		throw new SpelEvaluationException(
				getStartPosition(), accessException, SpelMessage.PROBLEM_LOCATING_METHOD, method, className);
	}
	else {
		throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND, method, className);
	}
}
 
Example #19
Source File: StandardEvaluationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
private List<MethodResolver> initMethodResolvers() {
	List<MethodResolver> resolvers = this.methodResolvers;
	if (resolvers == null) {
		resolvers = new ArrayList<>(1);
		this.reflectiveMethodResolver = new ReflectiveMethodResolver();
		resolvers.add(this.reflectiveMethodResolver);
		this.methodResolvers = resolvers;
	}
	return resolvers;
}
 
Example #20
Source File: SimpleEvaluationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private SimpleEvaluationContext(List<PropertyAccessor> accessors, List<MethodResolver> resolvers,
		@Nullable TypeConverter converter, @Nullable TypedValue rootObject) {

	this.propertyAccessors = accessors;
	this.methodResolvers = resolvers;
	this.typeConverter = (converter != null ? converter : new StandardTypeConverter());
	this.rootObject = (rootObject != null ? rootObject : TypedValue.NULL);
}
 
Example #21
Source File: SimpleEvaluationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Register the specified {@link MethodResolver} delegates for
 * a combination of property access and method resolution.
 * @param resolvers the resolver delegates to use
 * @see #withInstanceMethods()
 * @see SimpleEvaluationContext#forPropertyAccessors
 */
public Builder withMethodResolvers(MethodResolver... resolvers) {
	for (MethodResolver resolver : resolvers) {
		if (resolver.getClass() == ReflectiveMethodResolver.class) {
			throw new IllegalArgumentException("SimpleEvaluationContext is not designed for use with a plain " +
					"ReflectiveMethodResolver. Consider using DataBindingMethodResolver or a custom subclass.");
		}
	}
	this.resolvers = Arrays.asList(resolvers);
	return this;
}
 
Example #22
Source File: SimpleEvaluationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Register the specified {@link MethodResolver} delegates for
 * a combination of property access and method resolution.
 * @param resolvers the resolver delegates to use
 * @see #withInstanceMethods()
 * @see SimpleEvaluationContext#forPropertyAccessors
 */
public Builder withMethodResolvers(MethodResolver... resolvers) {
	for (MethodResolver resolver : resolvers) {
		if (resolver.getClass() == ReflectiveMethodResolver.class) {
			throw new IllegalArgumentException("SimpleEvaluationContext is not designed for use with a plain " +
					"ReflectiveMethodResolver. Consider using DataBindingMethodResolver or a custom subclass.");
		}
	}
	this.resolvers = Arrays.asList(resolvers);
	return this;
}
 
Example #23
Source File: SimpleEvaluationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
private SimpleEvaluationContext(List<PropertyAccessor> accessors, List<MethodResolver> resolvers,
		@Nullable TypeConverter converter, @Nullable TypedValue rootObject) {

	this.propertyAccessors = accessors;
	this.methodResolvers = resolvers;
	this.typeConverter = (converter != null ? converter : new StandardTypeConverter());
	this.rootObject = (rootObject != null ? rootObject : TypedValue.NULL);
}
 
Example #24
Source File: MethodReference.java    From java-technology-stack with MIT License 5 votes vote down vote up
private MethodExecutor findAccessorForMethod(List<TypeDescriptor> argumentTypes, Object targetObject,
		EvaluationContext evaluationContext) throws SpelEvaluationException {

	AccessException accessException = null;
	List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
	for (MethodResolver methodResolver : methodResolvers) {
		try {
			MethodExecutor methodExecutor = methodResolver.resolve(
					evaluationContext, targetObject, this.name, argumentTypes);
			if (methodExecutor != null) {
				return methodExecutor;
			}
		}
		catch (AccessException ex) {
			accessException = ex;
			break;
		}
	}

	String method = FormatHelper.formatMethodForMessage(this.name, argumentTypes);
	String className = FormatHelper.formatClassNameForMessage(
			targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass());
	if (accessException != null) {
		throw new SpelEvaluationException(
				getStartPosition(), accessException, SpelMessage.PROBLEM_LOCATING_METHOD, method, className);
	}
	else {
		throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND, method, className);
	}
}
 
Example #25
Source File: StandardEvaluationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private List<MethodResolver> initMethodResolvers() {
	List<MethodResolver> resolvers = this.methodResolvers;
	if (resolvers == null) {
		resolvers = new ArrayList<>(1);
		this.reflectiveMethodResolver = new ReflectiveMethodResolver();
		resolvers.add(this.reflectiveMethodResolver);
		this.methodResolvers = resolvers;
	}
	return resolvers;
}
 
Example #26
Source File: StandardEvaluationContext.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public boolean removeMethodResolver(MethodResolver methodResolver) {
	ensureMethodResolversInitialized();
	return this.methodResolvers.remove(methodResolver);
}
 
Example #27
Source File: SpelTaskEvaluator.java    From piper with Apache License 2.0 4 votes vote down vote up
private MethodResolver methodResolver () {
  return (ctx,target,name,args) -> {
    return methodExecutors.get(name);
  };
}
 
Example #28
Source File: SimpleEvaluationContext.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Return the specified {@link MethodResolver} delegates, if any.
 * @see Builder#withMethodResolvers
 */
@Override
public List<MethodResolver> getMethodResolvers() {
	return this.methodResolvers;
}
 
Example #29
Source File: StandardEvaluationContext.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public void setMethodResolvers(List<MethodResolver> methodResolvers) {
	this.methodResolvers = methodResolvers;
}
 
Example #30
Source File: StandardEvaluationContext.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public List<MethodResolver> getMethodResolvers() {
	ensureMethodResolversInitialized();
	return this.methodResolvers;
}