List of usage examples for org.springframework.expression Expression getValue
@Nullable
Object getValue() throws EvaluationException;
From source file:prospring3.spel.SpelMain.java
public static void main(String[] args) { int sum = 0;//from w w w.j ava 2s. c om for (int a = 0; a < 10; a++) { sum += a; } System.out.println("sum = " + sum); ExpressionParser parser = new SpelExpressionParser(); Expression e2 = parser.parseExpression("'Hello World'.bytes.length"); // int v2 = e2.getValue(); // ALT+Enter and Idea suggest cast to int through Integer wrapper // System.out.println("v2 = " + v2); Expression e = parser.parseExpression("'Hello World'"); Object v = e.getValue(); System.out.println("v = " + v); e = parser.parseExpression("'Hello World'.concat('!')"); v = e.getValue(); System.out.println("v = " + v); }
From source file:hu.petabyte.redflags.engine.util.MappingUtils.java
/** * Sets the value of a bean's property, using the given wrapper. The * property can be a deeper one, e.g. "a.b.c.d". If any of a, b or c * properties is null, the method will call an empty constructor for its * static type. If any error occurs, setDeepProperty will write it to the * LOG and returns false.// ww w . java 2s . c o m * * @param wrapper * Wrapper of the root object. * @param property * The property needs to be set. * @param value * The new value of the property. * @return True if property setting was successful, false on error. */ public static synchronized boolean setDeepProperty(BeanWrapper wrapper, String property, Object value) { try { // this will help calling a constructor: ExpressionParser parser = new SpelExpressionParser(); // go thru property path elements: int offset = 0; while ((offset = property.indexOf(".", offset + 1)) > -1) { String currentProperty = property.substring(0, offset); // if current property is null: if (null == wrapper.getPropertyValue(currentProperty)) { String className = wrapper.getPropertyType(currentProperty).getName(); // build up a constructor call: Expression exp = parser.parseExpression(String.format("new %s()", className)); // LIMITATIONS: // 1) uses static type // 2) needs defined empty constructor // run it: Object newObject = exp.getValue(); // and set the property: wrapper.setPropertyValue(currentProperty, newObject); } } // finally, set the destination property: wrapper.setPropertyValue(property, value); return true; } catch (Exception ex) { LOG.error("Could not set property '{}'", property); LOG.debug("Exception: ", ex); return false; } }
From source file:com.arondor.common.reflection.parser.spring.BeanPropertyParser.java
public ElementConfiguration parseProperty(Object value) { LOGGER.debug("value : " + value); if (value instanceof TypedStringValue) { TypedStringValue stringValue = (TypedStringValue) value; if (stringValue.getTargetTypeName() != null) { return getEnumObjectConfiguration(stringValue); } else {//ww w . ja va 2 s. co m PrimitiveConfiguration primitiveConfiguration = objectConfigurationFactory .createPrimitiveConfiguration(); if (useSPEL(stringValue)) { ExpressionParser parser = new SpelExpressionParser(); String expAsStringWithToken = stringValue.getValue().trim(); String expAsString = expAsStringWithToken.substring(2, expAsStringWithToken.length() - 1) .trim(); LOGGER.trace("This property is a SPEL expression: " + expAsString); // String regex = "systemProperties\\['([^\\s]+)'\\]"; Expression exp = parser.parseExpression(expAsString); StandardEvaluationContext sec = null; if (sec == null) { sec = new StandardEvaluationContext(); sec.addPropertyAccessor(new EnvironmentAccessor()); sec.addPropertyAccessor(new BeanExpressionContextAccessor()); sec.addPropertyAccessor(new BeanFactoryAccessor()); sec.addPropertyAccessor(new MapAccessor()); } primitiveConfiguration.setValue(String.valueOf(exp.getValue())); } else { LOGGER.trace("This property is NOT a SPEL expression: " + stringValue.getValue()); primitiveConfiguration.setValue(stringValue.getValue()); } return primitiveConfiguration; } } else if (value instanceof RuntimeBeanReference) { RuntimeBeanReference beanReference = (RuntimeBeanReference) value; ReferenceConfiguration referenceConfiguration = objectConfigurationFactory .createReferenceConfiguration(); referenceConfiguration.setReferenceName(beanReference.getBeanName()); return referenceConfiguration; } else if (value instanceof ManagedList<?>) { return parseValueList((ManagedList<?>) value); } else if (value instanceof ManagedMap<?, ?>) { return parseValueMap((ManagedMap<?, ?>) value); } else if (value instanceof BeanDefinitionHolder) { BeanDefinitionHolder beanDefinitionHolder = (BeanDefinitionHolder) value; return parseBeanDefinition(beanDefinitionHolder.getBeanDefinition()); } else { throw new UnsupportedOperationException("The type of property value is not suppported : " + value + " (class : " + value.getClass().getName() + ")"); } }