Java Code Examples for org.springframework.expression.spel.support.StandardEvaluationContext#addMethodResolver()

The following examples show how to use org.springframework.expression.spel.support.StandardEvaluationContext#addMethodResolver() . 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: 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 2
Source File: ScenariosForSpringSecurity.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
	public void testScenario04_ControllingWhichMethodsRun() throws Exception {
		SpelExpressionParser parser = new SpelExpressionParser();
		StandardEvaluationContext ctx = new StandardEvaluationContext();

		ctx.setRootObject(new Supervisor("Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it);

		ctx.addMethodResolver(new MyMethodResolver()); // NEEDS TO OVERRIDE THE REFLECTION ONE - SHOW REORDERING MECHANISM
		// Might be better with a as a variable although it would work as a property too...
		// Variable references using a '#'
//		SpelExpression expr = parser.parseExpression("(hasRole('SUPERVISOR') or (#a <  1.042)) and hasIpAddress('10.10.0.0/16')");
		Expression expr = parser.parseRaw("(hasRole(3) or (#a <  1.042)) and hasIpAddress('10.10.0.0/16')");

		Boolean value = null;

		ctx.setVariable("a",1.0d); // referenced as #a in the expression
		value = expr.getValue(ctx,Boolean.class);
		assertTrue(value);

//			ctx.setRootObject(new Manager("Luke"));
//			ctx.setVariable("a",1.043d);
//			value = (Boolean)expr.getValue(ctx,Boolean.class);
//			assertFalse(value);
	}
 
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: ScenariosForSpringSecurity.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
	public void testScenario04_ControllingWhichMethodsRun() throws Exception {
		SpelExpressionParser parser = new SpelExpressionParser();
		StandardEvaluationContext ctx = new StandardEvaluationContext();

		ctx.setRootObject(new Supervisor("Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it);

		ctx.addMethodResolver(new MyMethodResolver()); // NEEDS TO OVERRIDE THE REFLECTION ONE - SHOW REORDERING MECHANISM
		// Might be better with a as a variable although it would work as a property too...
		// Variable references using a '#'
//		SpelExpression expr = parser.parseExpression("(hasRole('SUPERVISOR') or (#a <  1.042)) and hasIpAddress('10.10.0.0/16')");
		Expression expr = parser.parseRaw("(hasRole(3) or (#a <  1.042)) and hasIpAddress('10.10.0.0/16')");

		Boolean value = null;

		ctx.setVariable("a",1.0d); // referenced as #a in the expression
		value = expr.getValue(ctx,Boolean.class);
		assertTrue(value);

//			ctx.setRootObject(new Manager("Luke"));
//			ctx.setVariable("a",1.043d);
//			value = (Boolean)expr.getValue(ctx,Boolean.class);
//			assertFalse(value);
	}
 
Example 5
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 6
Source File: ScenariosForSpringSecurity.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
	public void testScenario04_ControllingWhichMethodsRun() throws Exception {
		SpelExpressionParser parser = new SpelExpressionParser();
		StandardEvaluationContext ctx = new StandardEvaluationContext();

		ctx.setRootObject(new Supervisor("Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it);

		ctx.addMethodResolver(new MyMethodResolver()); // NEEDS TO OVERRIDE THE REFLECTION ONE - SHOW REORDERING MECHANISM
		// Might be better with a as a variable although it would work as a property too...
		// Variable references using a '#'
//		SpelExpression expr = parser.parseExpression("(hasRole('SUPERVISOR') or (#a <  1.042)) and hasIpAddress('10.10.0.0/16')");
		Expression expr = parser.parseRaw("(hasRole(3) or (#a <  1.042)) and hasIpAddress('10.10.0.0/16')");

		Boolean value = null;

		ctx.setVariable("a",1.0d); // referenced as #a in the expression
		value = expr.getValue(ctx,Boolean.class);
		assertTrue(value);

//			ctx.setRootObject(new Manager("Luke"));
//			ctx.setVariable("a",1.043d);
//			value = (Boolean)expr.getValue(ctx,Boolean.class);
//			assertFalse(value);
	}
 
Example 7
Source File: SpelTaskEvaluator.java    From piper with Apache License 2.0 4 votes vote down vote up
private StandardEvaluationContext createEvaluationContext(Context aContext) {
  StandardEvaluationContext context = new StandardEvaluationContext(aContext);
  context.addPropertyAccessor(new MapPropertyAccessor());
  context.addMethodResolver(methodResolver());
  return context;
}