Java ExpressionParser parser new Expression exp

Java expressionparser parser new expression exp

This preview shows page 287 - 290 out of 481 pages.

Java ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression("'Hello World'"); String message = (String) exp.getValue(); The value of the message variable is 'Hello World' . Kotlin val parser = SpelExpressionParser() val exp = parser.parseExpression("'Hello World'") val message = exp.value as String The value of the message variable is 'Hello World' . The SpEL classes and interfaces you are most likely to use are located in the org.springframework.expression package and its sub-packages, such as spel.support . The ExpressionParser interface is responsible for parsing an expression string. In the preceding example, the expression string is a string literal denoted by the surrounding single quotation marks. The Expression interface is responsible for evaluating the previously defined expression string. Two exceptions that can be thrown, ParseException and EvaluationException , when calling parser.parseExpression and exp.getValue , respectively. SpEL supports a wide range of features, such as calling methods, accessing properties, and calling constructors. In the following example of method invocation, we call the concat method on the string literal: 276
Image of page 287
Java ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression("'Hello World'.concat('!')"); String message = (String) exp.getValue(); The value of message is now 'Hello World!'. Kotlin val parser = SpelExpressionParser() val exp = parser.parseExpression("'Hello World'.concat('!')") val message = exp.value as String The value of message is now 'Hello World!'. The following example of calling a JavaBean property calls the String property Bytes : Java ExpressionParser parser = new SpelExpressionParser(); // invokes 'getBytes()' Expression exp = parser.parseExpression("'Hello World'.bytes"); byte[] bytes = (byte[]) exp.getValue(); This line converts the literal to a byte array. Kotlin val parser = SpelExpressionParser() // invokes 'getBytes()' val exp = parser.parseExpression("'Hello World'.bytes") val bytes = exp.value as ByteArray This line converts the literal to a byte array. SpEL also supports nested properties by using the standard dot notation (such as prop1.prop2.prop3 ) and also the corresponding setting of property values. Public fields may also be accessed. The following example shows how to use dot notation to get the length of a literal: Java ExpressionParser parser = new SpelExpressionParser(); // invokes 'getBytes().length' Expression exp = parser.parseExpression("'Hello World'.bytes.length"); int length = (Integer) exp.getValue(); 277
Image of page 288
'Hello World'.bytes.length gives the length of the literal. Kotlin val parser = SpelExpressionParser() // invokes 'getBytes().length' val exp = parser.parseExpression("'Hello World'.bytes.length") val length = exp.value as Int 'Hello World'.bytes.length gives the length of the literal. The String’s constructor can be called instead of using a string literal, as the following example shows: Java ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression("new String('hello world').toUpperCase()"); String message = exp.getValue(String.class); Construct a new String from the literal and make it be upper case.
Image of page 289
Image of page 290

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture