Search in sources :

Example 1 with MapAccessor

use of org.springframework.context.expression.MapAccessor in project spring-boot by spring-projects.

the class BindingPreparationTests method testExpressionLists.

@Test
@Ignore("Work in progress")
public void testExpressionLists() throws Exception {
    TargetWithNestedMapOfListOfString target = new TargetWithNestedMapOfListOfString();
    LinkedHashMap<String, List<String>> map = new LinkedHashMap<>();
    // map.put("foo", Arrays.asList("bar"));
    target.setNested(map);
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext(target);
    context.addPropertyAccessor(new MapAccessor());
    Expression expression = parser.parseExpression("nested.foo");
    assertThat(expression.getValue(context)).isNotNull();
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) List(java.util.List) MapAccessor(org.springframework.context.expression.MapAccessor) LinkedHashMap(java.util.LinkedHashMap) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with MapAccessor

use of org.springframework.context.expression.MapAccessor in project spring-integration by spring-projects.

the class IntegrationSimpleEvaluationContextFactoryBean method getObject.

@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;
}
Also used : DataBindingPropertyAccessor(org.springframework.expression.spel.support.DataBindingPropertyAccessor) PropertyAccessor(org.springframework.expression.PropertyAccessor) SimpleEvaluationContext(org.springframework.expression.spel.support.SimpleEvaluationContext) MapAccessor(org.springframework.context.expression.MapAccessor) Method(java.lang.reflect.Method)

Example 3 with MapAccessor

use of org.springframework.context.expression.MapAccessor in project spring-integration by spring-projects.

the class ObjectToMapTransformerParserTests method testObjectToSpelMapTransformer.

@SuppressWarnings("unchecked")
@Test
public void testObjectToSpelMapTransformer() {
    Employee employee = this.buildEmployee();
    StandardEvaluationContext context = new StandardEvaluationContext(employee);
    context.addPropertyAccessor(new MapAccessor());
    ExpressionParser parser = new SpelExpressionParser();
    Message<Employee> message = MessageBuilder.withPayload(employee).build();
    directInput.send(message);
    Message<Map<String, Object>> outputMessage = (Message<Map<String, Object>>) output.receive();
    Map<String, Object> transformedMap = outputMessage.getPayload();
    assertNotNull(outputMessage.getPayload());
    for (String key : transformedMap.keySet()) {
        Expression expression = parser.parseExpression(key);
        Object valueFromTheMap = transformedMap.get(key);
        Object valueFromExpression = expression.getValue(context);
        assertEquals(valueFromTheMap, valueFromExpression);
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Message(org.springframework.messaging.Message) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) MapAccessor(org.springframework.context.expression.MapAccessor) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with MapAccessor

use of org.springframework.context.expression.MapAccessor in project spring-integration by spring-projects.

the class ObjectToMapTransformerTests method testObjectToSpelMapTransformer.

@SuppressWarnings("unchecked")
@Test
public void testObjectToSpelMapTransformer() throws IOException {
    Employee employee = this.buildEmployee();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.addPropertyAccessor(new MapAccessor());
    ExpressionParser parser = new SpelExpressionParser();
    ObjectToMapTransformer transformer = new ObjectToMapTransformer();
    Message<Employee> message = MessageBuilder.withPayload(employee).build();
    Message<?> transformedMessage = transformer.transform(message);
    Map<String, Object> transformedMap = (Map<String, Object>) transformedMessage.getPayload();
    assertNotNull(transformedMap);
    Object valueFromTheMap = null;
    Object valueFromExpression = null;
    Expression expression = null;
    expression = parser.parseExpression("departments[0]");
    valueFromTheMap = transformedMap.get("departments[0]");
    valueFromExpression = expression.getValue(context, employee, String.class);
    assertEquals(valueFromTheMap, valueFromExpression);
    expression = parser.parseExpression("person.address.coordinates");
    valueFromTheMap = transformedMap.get("person.address.coordinates");
    valueFromExpression = expression.getValue(context, employee, Map.class);
    assertEquals(valueFromTheMap, valueFromExpression);
    expression = parser.parseExpression("person.akaNames[0]");
    valueFromTheMap = transformedMap.get("person.akaNames[0]");
    valueFromExpression = expression.getValue(context, employee, String.class);
    assertEquals(valueFromTheMap, valueFromExpression);
    expression = parser.parseExpression("testMapInMapData.internalMapA.bar");
    valueFromTheMap = transformedMap.get("testMapInMapData.internalMapA.bar");
    valueFromExpression = expression.getValue(context, employee, String.class);
    assertEquals(valueFromTheMap, valueFromExpression);
    expression = parser.parseExpression("companyAddress.street");
    valueFromTheMap = transformedMap.get("companyAddress.street");
    valueFromExpression = expression.getValue(context, employee, String.class);
    assertEquals(valueFromTheMap, valueFromExpression);
    expression = parser.parseExpression("person.lname");
    valueFromTheMap = transformedMap.get("person.lname");
    valueFromExpression = expression.getValue(context, employee, String.class);
    assertEquals(valueFromTheMap, valueFromExpression);
    expression = parser.parseExpression("person.address.mapWithListData.mapWithListTestData[1]");
    valueFromTheMap = transformedMap.get("person.address.mapWithListData.mapWithListTestData[1]");
    valueFromExpression = expression.getValue(context, employee, String.class);
    assertEquals(valueFromTheMap, valueFromExpression);
    expression = parser.parseExpression("companyAddress.city");
    valueFromTheMap = transformedMap.get("companyAddress.city");
    valueFromExpression = expression.getValue(context, employee, String.class);
    assertEquals(valueFromTheMap, valueFromExpression);
    expression = parser.parseExpression("person.akaNames[2]");
    valueFromTheMap = transformedMap.get("person.akaNames[2]");
    valueFromExpression = expression.getValue(context, employee, String.class);
    assertEquals(valueFromTheMap, valueFromExpression);
    expression = parser.parseExpression("person.child");
    valueFromTheMap = transformedMap.get("person.child");
    valueFromExpression = expression.getValue(context, employee, String.class);
    assertNull(valueFromTheMap);
    assertNull(valueFromExpression);
    expression = parser.parseExpression("testMapInMapData.internalMapA.foo");
    valueFromTheMap = transformedMap.get("testMapInMapData.internalMapA.foo");
    valueFromExpression = expression.getValue(context, employee, String.class);
    assertEquals(valueFromTheMap, valueFromExpression);
    expression = parser.parseExpression("person.address.city");
    valueFromTheMap = transformedMap.get("person.address.city");
    valueFromExpression = expression.getValue(context, employee, String.class);
    assertEquals(valueFromTheMap, valueFromExpression);
    expression = parser.parseExpression("companyAddress.coordinates.latitude[0]");
    valueFromTheMap = transformedMap.get("companyAddress.coordinates.latitude[0]");
    valueFromExpression = expression.getValue(context, employee, Integer.class);
    assertEquals(valueFromTheMap, valueFromExpression);
    expression = parser.parseExpression("person.remarks[1].baz");
    valueFromTheMap = transformedMap.get("person.remarks[1].baz");
    valueFromExpression = expression.getValue(context, employee, String.class);
    assertEquals(valueFromTheMap, valueFromExpression);
    expression = parser.parseExpression("listOfDates[0][1]");
    valueFromTheMap = new Date((Long) transformedMap.get("listOfDates[0][1]"));
    valueFromExpression = expression.getValue(context, employee, Date.class);
    assertEquals(valueFromTheMap, valueFromExpression);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Date(java.util.Date) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) MapAccessor(org.springframework.context.expression.MapAccessor) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with MapAccessor

use of org.springframework.context.expression.MapAccessor in project spring-boot-admin by codecentric.

the class TelegramNotifier method getText.

@Nullable
protected String getText(InstanceEvent event, Instance instance) {
    Map<String, Object> root = new HashMap<>();
    root.put("event", event);
    root.put("instance", instance);
    root.put("lastStatus", getLastStatus(event.getInstance()));
    StandardEvaluationContext context = new StandardEvaluationContext(root);
    context.addPropertyAccessor(new MapAccessor());
    return message.getValue(context, String.class);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) HashMap(java.util.HashMap) MapAccessor(org.springframework.context.expression.MapAccessor) Nullable(javax.annotation.Nullable)

Aggregations

MapAccessor (org.springframework.context.expression.MapAccessor)20 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)17 HashMap (java.util.HashMap)11 Nullable (javax.annotation.Nullable)7 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)6 Test (org.junit.Test)4 Expression (org.springframework.expression.Expression)4 Map (java.util.Map)3 BeanFactoryResolver (org.springframework.context.expression.BeanFactoryResolver)3 EnvironmentAccessor (org.springframework.context.expression.EnvironmentAccessor)3 Method (java.lang.reflect.Method)2 SpringELExpressionParser (org.springframework.binding.expression.spel.SpringELExpressionParser)2 BeanExpressionContextAccessor (org.springframework.context.expression.BeanExpressionContextAccessor)2 ExpressionParser (org.springframework.expression.ExpressionParser)2 PropertyAccessor (org.springframework.expression.PropertyAccessor)2 SpelParserConfiguration (org.springframework.expression.spel.SpelParserConfiguration)2 ReflectivePropertyAccessor (org.springframework.expression.spel.support.ReflectivePropertyAccessor)2 StandardTypeConverter (org.springframework.expression.spel.support.StandardTypeConverter)2 ActionPropertyAccessor (org.springframework.webflow.expression.spel.ActionPropertyAccessor)2 BeanFactoryPropertyAccessor (org.springframework.webflow.expression.spel.BeanFactoryPropertyAccessor)2