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

The following examples show how to use org.springframework.expression.spel.support.SimpleEvaluationContext. 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: PropertyAccessTests.java    From java-technology-stack with 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 #2
Source File: SpelQueryEngineUnitTests.java    From spring-data-keyvalue with Apache License 2.0 6 votes vote down vote up
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 #3
Source File: SpelQueryCreatorUnitTests.java    From spring-data-keyvalue with Apache License 2.0 6 votes vote down vote up
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 #4
Source File: SpelTagValueExpressionResolver.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: PropertyAccessTests.java    From java-technology-stack with 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 File: PropertyAccessTests.java    From java-technology-stack with 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 #7
Source File: PropertyAccessTests.java    From spring-analysis-note with 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 #8
Source File: PropertyAccessTests.java    From spring-analysis-note with 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 #9
Source File: PropertyAccessTests.java    From spring-analysis-note with 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 #10
Source File: PropertyAccessTests.java    From spring-analysis-note with 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 #11
Source File: PropertyAccessTests.java    From spring-analysis-note with 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 #12
Source File: PropertyAccessTests.java    From java-technology-stack with 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 File: PropertyAccessTests.java    From java-technology-stack with 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
Source File: DiscoveryClientRouteDefinitionLocator.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
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
Source File: DiscoveryClientRouteDefinitionLocator.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
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 File: PropertyAccessTests.java    From java-technology-stack with 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 File: PropertyAccessTests.java    From spring-analysis-note with 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 File: PropertyAccessTests.java    From spring-analysis-note with 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 File: PropertyAccessTests.java    From java-technology-stack with 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 File: SpelCriteria.java    From spring-data-keyvalue with 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());
}