/** * 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()); }
/** * 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()); } }
@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()); }
public ExpressionCommandMessageProcessor(MethodFilter methodFilter, BeanFactory beanFactory) { if (beanFactory != null) { this.setBeanFactory(beanFactory); } if (methodFilter != null) { MethodResolver methodResolver = new ExpressionCommandMethodResolver(methodFilter); this.getEvaluationContext(false).setMethodResolvers(Collections.singletonList(methodResolver)); } }
public ExpressionCommandMessageProcessor(MethodFilter methodFilter, BeanFactory beanFactory) { if (beanFactory != null) { this.setBeanFactory(beanFactory); } if (methodFilter != null) { MethodResolver methodResolver = new ExpressionCommandMethodResolver(methodFilter); this.getEvaluationContext(false).setMethodResolvers(Collections.singletonList(methodResolver)); } }
public MapExpressionMethods(StandardEvaluationContext evaluationContext, boolean autoCustomize, boolean replaceMethodResolver) { Assert.notNull(evaluationContext, "Evaluation context cannot be null"); if (autoCustomize) { MethodResolver methodResolver = new PartitionKeyMethodResolver(); if (replaceMethodResolver) { List<MethodResolver> methodResolvers = new ArrayList<MethodResolver>(); methodResolvers.add(methodResolver); evaluationContext.setMethodResolvers(methodResolvers); } else { evaluationContext.addMethodResolver(methodResolver); } } if (autoCustomize) { evaluationContext.addPropertyAccessor(new MapPartitionKeyPropertyAccessor()); } this.context = evaluationContext; }
/** * Instantiates a new message expression methods with * a {@link StandardEvaluationContext}. * * @param evaluationContext the spel evaluation context * @param autoCustomize auto customize method resolver and property accessor * @param replaceMethodResolver replace context method resolver */ public MessageExpressionMethods(StandardEvaluationContext evaluationContext, boolean autoCustomize, boolean replaceMethodResolver) { Assert.notNull(evaluationContext, "Evaluation context cannot be null"); if (autoCustomize) { MethodResolver methodResolver = new MessagePartitionKeyMethodResolver(); if (replaceMethodResolver) { List<MethodResolver> methodResolvers = new ArrayList<MethodResolver>(); methodResolvers.add(methodResolver); evaluationContext.setMethodResolvers(methodResolvers); } else { evaluationContext.addMethodResolver(methodResolver); } } if (autoCustomize) { evaluationContext.addPropertyAccessor(new MessagePartitionKeyPropertyAccessor()); } this.context = evaluationContext; }
/** * Creates a configured SpEL evaluation context * * @param rootObject the root object to transform * @param allowUnknownKeys flag to control what helper functions are available * @return an evaluation context hooked with helper functions and correct ACL via whitelisting */ public StandardEvaluationContext buildEvaluationContext(Object rootObject, boolean allowUnknownKeys) { ReturnTypeRestrictor returnTypeRestrictor = new ReturnTypeRestrictor(allowedReturnTypes); StandardEvaluationContext evaluationContext = new StandardEvaluationContext(rootObject); evaluationContext.setTypeLocator(new WhitelistTypeLocator()); evaluationContext.setMethodResolvers(Collections.singletonList( new FilteredMethodResolver(returnTypeRestrictor))); evaluationContext.setPropertyAccessors(Arrays.asList(new MapPropertyAccessor(allowUnknownKeys), new FilteredPropertyAccessor(returnTypeRestrictor))); try { for (Map.Entry<String, List<Class<?>>> m : registeredHelperFunctions.entrySet()) { registerFunction(evaluationContext, m.getKey(), m.getValue().toArray(new Class<?>[0])); } } catch (NoSuchMethodException e) { // Indicates a function was not properly registered. This should not happen. Please fix the faulty function LOGGER.error("Failed to register helper functions for rootObject {}", rootObject, e); } return evaluationContext; } }
StandardEvaluationContext evaluationContext = new StandardEvaluationContext(rootObject); evaluationContext.setTypeLocator(new WhitelistTypeLocator()); evaluationContext.setMethodResolvers(Collections.singletonList(new FilteredMethodResolver())); evaluationContext.setPropertyAccessors(Arrays.asList(new MapPropertyAccessor(allowUnknownKeys), new FilteredPropertyAccessor()));