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

The following examples show how to use org.springframework.expression.spel.support.StandardEvaluationContext#setTypeLocator() . 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
@Before
public void init() throws SchedulerException, IOException {
    MockitoAnnotations.initMocks(this);
    when(context.getSpace()).thenReturn(schedulerSpaceInterface);
    when(schedulerSpaceInterface.checkFileExists(USERSPACE_NAME, existUserFilePath)).thenReturn(true);
    when(schedulerSpaceInterface.checkFileExists(USERSPACE_NAME, notExistUserFilePath)).thenReturn(false);
    when(schedulerSpaceInterface.checkFileExists(GLOBALSPACE_NAME, existGlobalFilePath)).thenReturn(true);
    when(schedulerSpaceInterface.checkFileExists(GLOBALSPACE_NAME, notExistGlobalFilePath)).thenReturn(false);
    when(context.getScheduler()).thenReturn(scheduler);
    when(scheduler.thirdPartyCredentialsKeySet()).thenReturn(Collections.singleton(existCredential));
    modelFile = testFolder.newFile("modelFile");
    FileUtils.writeStringToFile(modelFile, validModel, Charset.defaultCharset());
    StandardEvaluationContext spelContext = new StandardEvaluationContext();
    spelContext.setTypeLocator(new RestrictedTypeLocator());
    spelContext.setMethodResolvers(Collections.singletonList(new RestrictedMethodResolver()));
    spelContext.addPropertyAccessor(new RestrictedPropertyAccessor());
    when(context.getSpELContext()).thenReturn(spelContext);
}
 
Example 2
Source Project: spring-analysis-note   File: SpelReproTests.java    License: MIT License 5 votes vote down vote up
@Test
public void SPR5899() {
	StandardEvaluationContext context = new StandardEvaluationContext(new Spr5899Class());
	Expression expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(12)");
	assertEquals(12, expr.getValue(context));
	expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(null)");
	assertEquals(null, expr.getValue(context));
	try {
		expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull2(null)");
		expr.getValue();
		fail("Should have failed to find a method to which it could pass null");
	}
	catch (EvaluationException see) {
		// success
	}
	context.setTypeLocator(new MyTypeLocator());

	// varargs
	expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(null,'a','b')");
	assertEquals("ab", expr.getValue(context));

	// varargs 2 - null is packed into the varargs
	expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(12,'a',null,'c')");
	assertEquals("anullc", expr.getValue(context));

	// check we can find the ctor ok
	expr = new SpelExpressionParser().parseRaw("new Spr5899Class().toString()");
	assertEquals("instance", expr.getValue(context));

	expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null).toString()");
	assertEquals("instance", expr.getValue(context));

	// ctor varargs
	expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a','b').toString()");
	assertEquals("instance", expr.getValue(context));

	// ctor varargs 2
	expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a', null, 'b').toString()");
	assertEquals("instance", expr.getValue(context));
}
 
Example 3
Source Project: java-technology-stack   File: SpelReproTests.java    License: MIT License 5 votes vote down vote up
@Test
public void SPR5899() {
	StandardEvaluationContext context = new StandardEvaluationContext(new Spr5899Class());
	Expression expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(12)");
	assertEquals(12, expr.getValue(context));
	expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(null)");
	assertEquals(null, expr.getValue(context));
	try {
		expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull2(null)");
		expr.getValue();
		fail("Should have failed to find a method to which it could pass null");
	}
	catch (EvaluationException see) {
		// success
	}
	context.setTypeLocator(new MyTypeLocator());

	// varargs
	expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(null,'a','b')");
	assertEquals("ab", expr.getValue(context));

	// varargs 2 - null is packed into the varargs
	expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(12,'a',null,'c')");
	assertEquals("anullc", expr.getValue(context));

	// check we can find the ctor ok
	expr = new SpelExpressionParser().parseRaw("new Spr5899Class().toString()");
	assertEquals("instance", expr.getValue(context));

	expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null).toString()");
	assertEquals("instance", expr.getValue(context));

	// ctor varargs
	expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a','b').toString()");
	assertEquals("instance", expr.getValue(context));

	// ctor varargs 2
	expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a', null, 'b').toString()");
	assertEquals("instance", expr.getValue(context));
}
 
Example 4
@Test
public void qualifier() throws Exception {
  StandardEvaluationContext context = new StandardEvaluationContext();
  context.setTypeLocator(typeName -> {
    try {
      return Class.forName("com.github.zzt93.syncer.util." + typeName);
    } catch (ClassNotFoundException e) {
      throw new IllegalArgumentException(e);
    }
  });
  Class testClass = parser.parseExpression("T(SpringELTest)").getValue(context, Class.class);
  Assert.assertEquals(testClass, SpringELTest.class);
  Integer value = parser.parseExpression("T(SpringELTest).a").getValue(context, Integer.class);
  Assert.assertTrue(value == 1);
}
 
Example 5
@Test
public void typeLocator() throws Exception {
  StandardEvaluationContext context = new StandardEvaluationContext();
  context.setTypeLocator(new CommonTypeLocator());
  Class sync = parser.parseExpression("T(SyncUtil)").getValue(context, Class.class);
  Assert.assertEquals(sync, SyncUtil.class);
  Class map = parser.parseExpression("T(Map)").getValue(context, Class.class);
  Assert.assertEquals(map, Map.class);
}
 
Example 6
@Test
public void syncUtil() throws Exception {
  StandardEvaluationContext context = new StandardEvaluationContext();
  context.setTypeLocator(new CommonTypeLocator());
  context.setVariable("tags", "[\"AC\",\"BD\",\"CE\",\"DF\",\"GG\"]");
  String[] tags = parser.parseExpression("T(SyncUtil).fromJson(#tags, T(String[]))")
      .getValue(context, String[].class);
  Object tags2 = parser.parseExpression("T(SyncUtil).fromJson(#tags, T(String[]))")
      .getValue(context);
  Assert.assertEquals(tags.length, 5);
  Assert.assertEquals(tags2.getClass(), String[].class);
  Assert.assertEquals(((String[]) tags2).length, 5);
}
 
Example 7
@Test
public void projection() throws Exception {
  StandardEvaluationContext context = new StandardEvaluationContext();
  context.setTypeLocator(new CommonTypeLocator());
  context.addPropertyAccessor(new MapAccessor());
  context.setVariable("content",
      "{\"blocks\":[{\"data\":{},\"depth\":0,\"entityRanges\":[],\"inlineStyleRanges\":[],\"key\":\"ummxd\",\"text\":\"Test\",\"type\":\"unstyled\"}],\"entityMap\":{}}");
  String value = parser
      .parseExpression("T(SyncUtil).fromJson(#content,T(Map))['blocks'].![text]")
      .getValue(context, String.class);
  Assert.assertEquals(value, "Test");
}
 
Example 8
@Test
public void SPR5899() throws Exception {
	StandardEvaluationContext eContext = new StandardEvaluationContext(new Spr5899Class());
	Expression expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(12)");
	assertEquals(12, expr.getValue(eContext));
	expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(null)");
	assertEquals(null, expr.getValue(eContext));
	try {
		expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull2(null)");
		expr.getValue();
		fail("Should have failed to find a method to which it could pass null");
	}
	catch (EvaluationException see) {
		// success
	}
	eContext.setTypeLocator(new MyTypeLocator());

	// varargs
	expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(null,'a','b')");
	assertEquals("ab", expr.getValue(eContext));

	// varargs 2 - null is packed into the varargs
	expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(12,'a',null,'c')");
	assertEquals("anullc", expr.getValue(eContext));

	// check we can find the ctor ok
	expr = new SpelExpressionParser().parseRaw("new Spr5899Class().toString()");
	assertEquals("instance", expr.getValue(eContext));

	expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null).toString()");
	assertEquals("instance", expr.getValue(eContext));

	// ctor varargs
	expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a','b').toString()");
	assertEquals("instance", expr.getValue(eContext));

	// ctor varargs 2
	expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a', null, 'b').toString()");
	assertEquals("instance", expr.getValue(eContext));
}
 
Example 9
public ModelValidatorContext(Map<String, Serializable> variablesValues, Scheduler scheduler,
        SchedulerSpaceInterface space) {
    spELVariables = new SpELVariables(variablesValues);
    spelContext = new StandardEvaluationContext(spELVariables);
    spelContext.setTypeLocator(new RestrictedTypeLocator());
    spelContext.setMethodResolvers(Collections.singletonList(new RestrictedMethodResolver()));
    spelContext.addPropertyAccessor(new RestrictedPropertyAccessor());
    this.scheduler = scheduler;
    this.space = space;
}
 
Example 10
@Before
public void before() {
    context = new StandardEvaluationContext();
    context.setTypeLocator(new RestrictedTypeLocator());
    context.setMethodResolvers(Collections.singletonList(new RestrictedMethodResolver()));
    context.addPropertyAccessor(new RestrictedPropertyAccessor());
}
 
Example 11
Source Project: kork   File: ExpressionsSupport.java    License: Apache License 2.0 5 votes vote down vote up
private StandardEvaluationContext createEvaluationContext(
    Object rootObject, boolean allowUnknownKeys) {
  ReturnTypeRestrictor returnTypeRestrictor = new ReturnTypeRestrictor(allowedReturnTypes);

  StandardEvaluationContext evaluationContext = new StandardEvaluationContext(rootObject);
  evaluationContext.setTypeLocator(new AllowListTypeLocator());
  evaluationContext.setMethodResolvers(
      Collections.singletonList(new FilteredMethodResolver(returnTypeRestrictor)));
  evaluationContext.setPropertyAccessors(
      Arrays.asList(
          new MapPropertyAccessor(allowUnknownKeys),
          new FilteredPropertyAccessor(returnTypeRestrictor)));

  return evaluationContext;
}
 
Example 12
public static void afterRecover(SyncData data) {
  StandardEvaluationContext context = data.getContext();
  context.setTypeLocator(new CommonTypeLocator());
  context.setRootObject(data);
}