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

The following examples show how to use org.springframework.expression.spel.support.SimpleEvaluationContext. 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: PropertyAccessTests.java    License: MIT License 6 votes vote down vote up
@Test
public void propertyReadOnly() {
	EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();

	Expression expr = parser.parseExpression("name");
	Person target = new Person("p1");
	assertEquals("p1", expr.getValue(context, target));
	target.setName("p2");
	assertEquals("p2", expr.getValue(context, target));

	try {
		parser.parseExpression("name='p3'").getValue(context, target);
		fail("Should have thrown SpelEvaluationException");
	}
	catch (SpelEvaluationException ex) {
		// expected
	}
}
 
Example 2
Source Project: spring-analysis-note   Source File: PropertyAccessTests.java    License: MIT License 6 votes vote down vote up
@Test
public void propertyReadWrite() {
	EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();

	Expression expr = parser.parseExpression("name");
	Person target = new Person("p1");
	assertEquals("p1", expr.getValue(context, target));
	target.setName("p2");
	assertEquals("p2", expr.getValue(context, target));

	parser.parseExpression("name='p3'").getValue(context, target);
	assertEquals("p3", target.getName());
	assertEquals("p3", expr.getValue(context, target));

	expr.setValue(context, target, "p4");
	assertEquals("p4", target.getName());
	assertEquals("p4", expr.getValue(context, target));
}
 
Example 3
Source Project: spring-analysis-note   Source File: PropertyAccessTests.java    License: MIT License 6 votes vote down vote up
@Test
public void propertyReadWriteWithRootObject() {
	Person target = new Person("p1");
	EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().withRootObject(target).build();
	assertSame(target, context.getRootObject().getValue());

	Expression expr = parser.parseExpression("name");
	assertEquals("p1", expr.getValue(context, target));
	target.setName("p2");
	assertEquals("p2", expr.getValue(context, target));

	parser.parseExpression("name='p3'").getValue(context, target);
	assertEquals("p3", target.getName());
	assertEquals("p3", expr.getValue(context, target));

	expr.setValue(context, target, "p4");
	assertEquals("p4", target.getName());
	assertEquals("p4", expr.getValue(context, target));
}
 
Example 4
Source Project: java-technology-stack   Source File: PropertyAccessTests.java    License: MIT License 6 votes vote down vote up
@Test
public void propertyReadOnly() {
	EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();

	Expression expr = parser.parseExpression("name");
	Person target = new Person("p1");
	assertEquals("p1", expr.getValue(context, target));
	target.setName("p2");
	assertEquals("p2", expr.getValue(context, target));

	try {
		parser.parseExpression("name='p3'").getValue(context, target);
		fail("Should have thrown SpelEvaluationException");
	}
	catch (SpelEvaluationException ex) {
		// expected
	}
}
 
Example 5
Source Project: java-technology-stack   Source File: PropertyAccessTests.java    License: MIT License 6 votes vote down vote up
@Test
public void propertyReadWrite() {
	EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();

	Expression expr = parser.parseExpression("name");
	Person target = new Person("p1");
	assertEquals("p1", expr.getValue(context, target));
	target.setName("p2");
	assertEquals("p2", expr.getValue(context, target));

	parser.parseExpression("name='p3'").getValue(context, target);
	assertEquals("p3", target.getName());
	assertEquals("p3", expr.getValue(context, target));

	expr.setValue(context, target, "p4");
	assertEquals("p4", target.getName());
	assertEquals("p4", expr.getValue(context, target));
}
 
Example 6
Source Project: java-technology-stack   Source File: PropertyAccessTests.java    License: MIT License 6 votes vote down vote up
@Test
public void propertyReadWriteWithRootObject() {
	Person target = new Person("p1");
	EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().withRootObject(target).build();
	assertSame(target, context.getRootObject().getValue());

	Expression expr = parser.parseExpression("name");
	assertEquals("p1", expr.getValue(context, target));
	target.setName("p2");
	assertEquals("p2", expr.getValue(context, target));

	parser.parseExpression("name='p3'").getValue(context, target);
	assertEquals("p3", target.getName());
	assertEquals("p3", expr.getValue(context, target));

	expr.setValue(context, target, "p4");
	assertEquals("p4", target.getName());
	assertEquals("p4", expr.getValue(context, target));
}
 
Example 7
@Override
public String resolve(String expression, Object parameter) {
	try {
		SimpleEvaluationContext context = SimpleEvaluationContext
				.forReadOnlyDataBinding().build();
		ExpressionParser expressionParser = new SpelExpressionParser();
		Expression expressionToEvaluate = expressionParser
				.parseExpression(expression);
		return expressionToEvaluate.getValue(context, parameter, String.class);
	}
	catch (Exception ex) {
		log.error("Exception occurred while tying to evaluate the SPEL expression ["
				+ expression + "]", ex);
	}
	return parameter.toString();
}
 
Example 8
private KeyValueQuery<SpelExpression> createQueryForMethodWithArgs(String methodName, Object... args)
		throws NoSuchMethodException, SecurityException {

	Class<?>[] argTypes = new Class<?>[args.length];
	if (!ObjectUtils.isEmpty(args)) {

		for (int i = 0; i < args.length; i++) {
			argTypes[i] = args[i].getClass();
		}
	}

	Method method = PersonRepository.class.getMethod(methodName, argTypes);
	doReturn(Person.class).when(metadataMock).getReturnedDomainClass(method);

	PartTree partTree = new PartTree(method.getName(), method.getReturnType());
	SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor(
			new QueryMethod(method, metadataMock, new SpelAwareProxyProjectionFactory()).getParameters(), args));

	KeyValueQuery<SpelExpression> q = creator.createQuery();
	q.getCriteria().setEvaluationContext(
			SimpleEvaluationContext.forReadOnlyDataBinding().withRootObject(args).withInstanceMethods().build());

	return q;
}
 
Example 9
private static SpelCriteria createQueryForMethodWithArgs(String methodName, Object... args) throws Exception {

		List<Class<?>> types = new ArrayList<>(args.length);

		for (Object arg : args) {
			types.add(arg.getClass());
		}

		Method method = PersonRepository.class.getMethod(methodName, types.toArray(new Class<?>[types.size()]));
		RepositoryMetadata metadata = mock(RepositoryMetadata.class);
		doReturn(method.getReturnType()).when(metadata).getReturnedDomainClass(method);

		PartTree partTree = new PartTree(method.getName(), method.getReturnType());
		SpelQueryCreator creator = new SpelQueryCreator(partTree, new ParametersParameterAccessor(
				new QueryMethod(method, metadata, new SpelAwareProxyProjectionFactory()).getParameters(), args));

		return new SpelCriteria(creator.createQuery().getCriteria(),
				SimpleEvaluationContext.forReadOnlyDataBinding().withInstanceMethods().withRootObject(args).build());
	}
 
Example 10
Source Project: spring-analysis-note   Source File: PropertyAccessTests.java    License: MIT License 5 votes vote down vote up
@Test
public void propertyAccessWithoutMethodResolver() {
	EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
	Person target = new Person("p1");
	try {
		parser.parseExpression("name.substring(1)").getValue(context, target);
		fail("Should have thrown SpelEvaluationException");
	}
	catch (SpelEvaluationException ex) {
		// expected
	}
}
 
Example 11
Source Project: spring-analysis-note   Source File: PropertyAccessTests.java    License: MIT License 5 votes vote down vote up
@Test
public void propertyAccessWithInstanceMethodResolverAndTypedRootObject() {
	Person target = new Person("p1");
	EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().
			withInstanceMethods().withTypedRootObject(target, TypeDescriptor.valueOf(Object.class)).build();

	assertEquals("1", parser.parseExpression("name.substring(1)").getValue(context, target));
	assertSame(target, context.getRootObject().getValue());
	assertSame(Object.class, context.getRootObject().getTypeDescriptor().getType());
}
 
Example 12
Source Project: java-technology-stack   Source File: PropertyAccessTests.java    License: MIT License 5 votes vote down vote up
@Test
public void propertyAccessWithoutMethodResolver() {
	EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
	Person target = new Person("p1");
	try {
		parser.parseExpression("name.substring(1)").getValue(context, target);
		fail("Should have thrown SpelEvaluationException");
	}
	catch (SpelEvaluationException ex) {
		// expected
	}
}
 
Example 13
Source Project: java-technology-stack   Source File: PropertyAccessTests.java    License: MIT License 5 votes vote down vote up
@Test
public void propertyAccessWithInstanceMethodResolverAndTypedRootObject() {
	Person target = new Person("p1");
	EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().
			withInstanceMethods().withTypedRootObject(target, TypeDescriptor.valueOf(Object.class)).build();

	assertEquals("1", parser.parseExpression("name.substring(1)").getValue(context, target));
	assertSame(target, context.getRootObject().getValue());
	assertSame(Object.class, context.getRootObject().getTypeDescriptor().getType());
}
 
Example 14
private DiscoveryClientRouteDefinitionLocator(String discoveryClientName,
		DiscoveryLocatorProperties properties) {
	this.properties = properties;
	if (StringUtils.hasText(properties.getRouteIdPrefix())) {
		routeIdPrefix = properties.getRouteIdPrefix();
	}
	else {
		routeIdPrefix = discoveryClientName + "_";
	}
	evalCtxt = SimpleEvaluationContext.forReadOnlyDataBinding().withInstanceMethods()
			.build();
}
 
Example 15
String getValueFromExpr(SimpleEvaluationContext evalCtxt, SpelExpressionParser parser,
		ServiceInstance instance, Map.Entry<String, String> entry) {
	try {
		Expression valueExpr = parser.parseExpression(entry.getValue());
		return valueExpr.getValue(evalCtxt, instance, String.class);
	}
	catch (ParseException | EvaluationException e) {
		if (log.isDebugEnabled()) {
			log.debug("Unable to parse " + entry.getValue(), e);
		}
		throw e;
	}
}
 
Example 16
Source Project: spring-analysis-note   Source File: PropertyAccessTests.java    License: MIT License 4 votes vote down vote up
@Test(expected = SpelEvaluationException.class)
public void noGetClassAccess() {
	EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();

	parser.parseExpression("'a'.class.name").getValue(context);
}
 
Example 17
Source Project: spring-analysis-note   Source File: PropertyAccessTests.java    License: MIT License 4 votes vote down vote up
@Test
public void propertyAccessWithInstanceMethodResolver() {
	EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().withInstanceMethods().build();
	Person target = new Person("p1");
	assertEquals("1", parser.parseExpression("name.substring(1)").getValue(context, target));
}
 
Example 18
Source Project: java-technology-stack   Source File: PropertyAccessTests.java    License: MIT License 4 votes vote down vote up
@Test(expected = SpelEvaluationException.class)
public void noGetClassAccess() {
	EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();

	parser.parseExpression("'a'.class.name").getValue(context);
}
 
Example 19
Source Project: java-technology-stack   Source File: PropertyAccessTests.java    License: MIT License 4 votes vote down vote up
@Test
public void propertyAccessWithInstanceMethodResolver() {
	EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().withInstanceMethods().build();
	Person target = new Person("p1");
	assertEquals("1", parser.parseExpression("name.substring(1)").getValue(context, target));
}
 
Example 20
Source Project: spring-data-keyvalue   Source File: SpelCriteria.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new {@link SpelCriteria} for the given {@link SpelExpression}.
 *
 * @param expression must not be {@literal null}.
 */
public SpelCriteria(SpelExpression expression) {
	this(expression, SimpleEvaluationContext.forReadOnlyDataBinding().withInstanceMethods().build());
}