Java Code Examples for org.springframework.expression.spel.support.StandardEvaluationContext#setMethodResolvers()
The following examples show how to use
org.springframework.expression.spel.support.StandardEvaluationContext#setMethodResolvers() .
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: EvaluationTests.java From spring-analysis-note with MIT License | 6 votes |
/** * 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 2
Source File: MethodInvocationTests.java From spring-analysis-note with MIT License | 6 votes |
@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 3
Source File: SpelReproTests.java From spring-analysis-note with MIT License | 6 votes |
/** * 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 4
Source File: EvaluationTests.java From java-technology-stack with MIT License | 6 votes |
/** * 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 5
Source File: MethodInvocationTests.java From java-technology-stack with MIT License | 6 votes |
@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 6
Source File: SpelReproTests.java From java-technology-stack with MIT License | 6 votes |
/** * 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 7
Source File: EvaluationTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * 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 8
Source File: MethodInvocationTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@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 9
Source File: SpelReproTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * 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 10
Source File: OptionalParserValidatorTest.java From scheduling with GNU Affero General Public License v3.0 | 6 votes |
@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 11
Source File: DefaultEntitySanitizer.java From titus-control-plane with Apache License 2.0 | 5 votes |
public DefaultEntitySanitizer(VerifierMode verifierMode, List<Function<Object, Optional<Object>>> sanitizers, boolean annotationSanitizersEnabled, boolean stdValueSanitizersEnabled, Function<Class<?>, Boolean> includesPredicate, Function<String, Optional<Object>> templateResolver, Map<String, Method> registeredFunctions, Map<String, Object> registeredBeans, Function<Class<?>, Optional<ConstraintValidator<?, ?>>> applicationValidatorFactory) { Supplier<EvaluationContext> spelContextFactory = () -> { StandardEvaluationContext context = new StandardEvaluationContext(); registeredFunctions.forEach(context::registerFunction); context.setBeanResolver((ctx, beanName) -> registeredBeans.get(beanName)); context.setMethodResolvers(Collections.singletonList(new ReflectiveMethodResolver())); return context; }; this.validator = Validation.buildDefaultValidatorFactory() .usingContext() .constraintValidatorFactory(new ConstraintValidatorFactoryWrapper(verifierMode, applicationValidatorFactory, spelContextFactory)) .messageInterpolator(new SpELMessageInterpolator(spelContextFactory)) .getValidator(); List<Function<Object, Optional<Object>>> allSanitizers = new ArrayList<>(); if (annotationSanitizersEnabled) { allSanitizers.add(new AnnotationBasedSanitizer(spelContextFactory.get(), includesPredicate)); } if (stdValueSanitizersEnabled) { allSanitizers.add(new StdValueSanitizer(includesPredicate)); } allSanitizers.add(new TemplateSanitizer(templateResolver, includesPredicate)); allSanitizers.addAll(sanitizers); this.sanitizers = allSanitizers; }
Example 12
Source File: TestValidator.java From titus-control-plane with Apache License 2.0 | 5 votes |
public static Validator testValidator(String alias, Object bean) { Supplier<EvaluationContext> spelContextFactory = () -> { StandardEvaluationContext context = new StandardEvaluationContext(); context.setBeanResolver((ctx, beanName) -> beanName.equals(alias) ? bean : null); context.setMethodResolvers(Collections.singletonList(new ReflectiveMethodResolver())); return context; }; return Validation.buildDefaultValidatorFactory() .usingContext() .constraintValidatorFactory(new ConstraintValidatorFactoryWrapper(VerifierMode.Strict, type -> Optional.empty(), spelContextFactory)) .messageInterpolator(new SpELMessageInterpolator(spelContextFactory)) .getValidator(); }
Example 13
Source File: ModelValidatorContext.java From scheduling with GNU Affero General Public License v3.0 | 5 votes |
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 14
Source File: SpelValidatorTest.java From scheduling with GNU Affero General Public License v3.0 | 5 votes |
@Before public void before() { context = new StandardEvaluationContext(); context.setTypeLocator(new RestrictedTypeLocator()); context.setMethodResolvers(Collections.singletonList(new RestrictedMethodResolver())); context.addPropertyAccessor(new RestrictedPropertyAccessor()); }
Example 15
Source File: ExpressionsSupport.java From kork with Apache License 2.0 | 5 votes |
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; }