public SimpleEvaluationContext build() { return new SimpleEvaluationContext(this.accessors, this.resolvers, this.typeConverter, this.rootObject); } }
@Test(expected = SpelEvaluationException.class) public void noGetClassAccess() { EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build(); parser.parseExpression("'a'.class.name").getValue(context); }
Builder ecBuilder = SimpleEvaluationContext.forPropertyAccessors( new MapAccessor(), DataBindingPropertyAccessor.forReadOnlyAccess()) .withInstanceMethods();
@Override public SimpleEvaluationContext getObject() throws Exception { Collection<PropertyAccessor> accessors = getPropertyAccessors().values(); PropertyAccessor[] accessorArray = accessors.toArray(new PropertyAccessor[accessors.size() + 2]); accessorArray[accessors.size()] = new MapAccessor(); accessorArray[accessors.size() + 1] = DataBindingPropertyAccessor.forReadOnlyAccess(); SimpleEvaluationContext evaluationContext = SimpleEvaluationContext.forPropertyAccessors(accessorArray) .withTypeConverter(getTypeConverter()) .withInstanceMethods() .build(); for (Entry<String, Method> functionEntry : getFunctions().entrySet()) { evaluationContext.setVariable(functionEntry.getKey(), functionEntry.getValue()); } return evaluationContext; }
@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)); }
@Override public EvaluationContext createContext(Object rootObject) { return SimpleEvaluationContext .forPropertyAccessors(getAccessorsArray()) .withConversionService(conversionService.getDelegateConversionService()) .withRootObject(rootObject) .build(); }
@Override public SimpleEvaluationContext getObject() throws Exception { Collection<PropertyAccessor> accessors = getPropertyAccessors().values(); PropertyAccessor[] accessorArray = accessors.toArray(new PropertyAccessor[accessors.size() + 2]); accessorArray[accessors.size()] = new MapAccessor(); accessorArray[accessors.size() + 1] = DataBindingPropertyAccessor.forReadOnlyAccess(); SimpleEvaluationContext evaluationContext = SimpleEvaluationContext.forPropertyAccessors(accessorArray) .withTypeConverter(getTypeConverter()) .withInstanceMethods() .build(); for (Entry<String, Method> functionEntry : getFunctions().entrySet()) { evaluationContext.setVariable(functionEntry.getKey(), functionEntry.getValue()); } return evaluationContext; }
@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)); }
@Override public EvaluationContext createContext(Object rootObject) { return SimpleEvaluationContext .forPropertyAccessors(getAccessorsArray()) .withConversionService(conversionService.getDelegateConversionService()) .withRootObject(rootObject) .build(); }
@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(); }
public SimpleEvaluationContext build() { return new SimpleEvaluationContext(this.accessors, this.resolvers, this.typeConverter, this.rootObject); } }
.forPropertyAccessors(new PropertyTraversingMapAccessor(type, conversionService)) //
public DiscoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient, DiscoveryLocatorProperties properties) { this.discoveryClient = discoveryClient; this.properties = properties; if (StringUtils.hasText(properties.getRouteIdPrefix())) { this.routeIdPrefix = properties.getRouteIdPrefix(); } else { this.routeIdPrefix = this.discoveryClient.getClass().getSimpleName() + "_"; } evalCtxt = SimpleEvaluationContext.forReadOnlyDataBinding() .withInstanceMethods() .build(); }
public SimpleEvaluationContext build() { return new SimpleEvaluationContext(this.accessors, this.resolvers, this.typeConverter, this.rootObject); } }
Builder ecBuilder = SimpleEvaluationContext.forPropertyAccessors( new MapAccessor(), DataBindingPropertyAccessor.forReadOnlyAccess()) .withInstanceMethods();
@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 } }
@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)); }
@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 } }
@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()); }
@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(); }