Polish SpEL chapter

This commit is contained in:
Sam Brannen 2021-04-22 18:55:40 +02:00
parent 6c3a0a9026
commit 01e50fb60a
1 changed files with 37 additions and 35 deletions

View File

@ -517,7 +517,7 @@ kinds of expression cannot be compiled at the moment:
* Expressions using custom resolvers or accessors * Expressions using custom resolvers or accessors
* Expressions using selection or projection * Expressions using selection or projection
More types of expression will be compilable in the future. More types of expressions will be compilable in the future.
@ -589,7 +589,7 @@ You can also refer to other bean properties by name, as the following example sh
To specify a default value, you can place the `@Value` annotation on fields, methods, To specify a default value, you can place the `@Value` annotation on fields, methods,
and method or constructor parameters. and method or constructor parameters.
The following example sets the default value of a field variable: The following example sets the default value of a field:
[source,java,indent=0,subs="verbatim,quotes",role="primary"] [source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java .Java
@ -788,7 +788,7 @@ using a literal on one side of a logical comparison operator.
---- ----
Numbers support the use of the negative sign, exponential notation, and decimal points. Numbers support the use of the negative sign, exponential notation, and decimal points.
By default, real numbers are parsed by using Double.parseDouble(). By default, real numbers are parsed by using `Double.parseDouble()`.
@ -796,10 +796,10 @@ By default, real numbers are parsed by using Double.parseDouble().
=== Properties, Arrays, Lists, Maps, and Indexers === Properties, Arrays, Lists, Maps, and Indexers
Navigating with property references is easy. To do so, use a period to indicate a nested Navigating with property references is easy. To do so, use a period to indicate a nested
property value. The instances of the `Inventor` class, `pupin` and `tesla`, were populated with property value. The instances of the `Inventor` class, `pupin` and `tesla`, were
data listed in the <<expressions-example-classes, Classes used in the examples>> section. populated with data listed in the <<expressions-example-classes, Classes used in the
To navigate "`down`" and get Tesla's year of birth and Pupin's city of birth, we use the following examples>> section. To navigate "down" the object graph and get Tesla's year of birth and
expressions: Pupin's city of birth, we use the following expressions:
[source,java,indent=0,subs="verbatim,quotes",role="primary"] [source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java .Java
@ -939,7 +939,7 @@ You can directly express lists in an expression by using `{}` notation.
---- ----
`{}` by itself means an empty list. For performance reasons, if the list is itself `{}` by itself means an empty list. For performance reasons, if the list is itself
entirely composed of fixed literals, a constant list is created to represent the entirely composed of fixed literals, a constant list is created to represent the
expression (rather than building a new list on each evaluation). expression (rather than building a new list on each evaluation).
@ -967,10 +967,11 @@ following example shows how to do so:
val mapOfMaps = parser.parseExpression("{name:{first:'Nikola',last:'Tesla'},dob:{day:10,month:'July',year:1856}}").getValue(context) as Map<*, *> val mapOfMaps = parser.parseExpression("{name:{first:'Nikola',last:'Tesla'},dob:{day:10,month:'July',year:1856}}").getValue(context) as Map<*, *>
---- ----
`{:}` by itself means an empty map. For performance reasons, if the map is itself composed `{:}` by itself means an empty map. For performance reasons, if the map is itself
of fixed literals or other nested constant structures (lists or maps), a constant map is created composed of fixed literals or other nested constant structures (lists or maps), a
to represent the expression (rather than building a new map on each evaluation). Quoting of the map keys constant map is created to represent the expression (rather than building a new map on
is optional. The examples above do not use quoted keys. each evaluation). Quoting of the map keys is optional (unless the key contains a period
(`.`)). The examples above do not use quoted keys.
@ -1003,8 +1004,7 @@ to have the array populated at construction time. The following example shows ho
val numbers3 = parser.parseExpression("new int[4][5]").getValue(context) as Array<IntArray> val numbers3 = parser.parseExpression("new int[4][5]").getValue(context) as Array<IntArray>
---- ----
You cannot currently supply an initializer when you construct You cannot currently supply an initializer when you construct a multi-dimensional array.
multi-dimensional array.
@ -1105,7 +1105,7 @@ expression-based `matches` operator. The following listing shows examples of bot
boolean trueValue = parser.parseExpression( boolean trueValue = parser.parseExpression(
"'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class); "'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);
//evaluates to false // evaluates to false
boolean falseValue = parser.parseExpression( boolean falseValue = parser.parseExpression(
"'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class); "'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);
---- ----
@ -1120,14 +1120,14 @@ expression-based `matches` operator. The following listing shows examples of bot
val trueValue = parser.parseExpression( val trueValue = parser.parseExpression(
"'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean::class.java) "'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean::class.java)
//evaluates to false // evaluates to false
val falseValue = parser.parseExpression( val falseValue = parser.parseExpression(
"'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean::class.java) "'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean::class.java)
---- ----
CAUTION: Be careful with primitive types, as they are immediately boxed up to the wrapper type, CAUTION: Be careful with primitive types, as they are immediately boxed up to their
so `1 instanceof T(int)` evaluates to `false` while `1 instanceof T(Integer)` wrapper types. For example, `1 instanceof T(int)` evaluates to `false`, while
evaluates to `true`, as expected. `1 instanceof T(Integer)` evaluates to `true`, as expected.
Each symbolic operator can also be specified as a purely alphabetic equivalent. This Each symbolic operator can also be specified as a purely alphabetic equivalent. This
avoids problems where the symbols used have special meaning for the document type in avoids problems where the symbols used have special meaning for the document type in
@ -1155,7 +1155,7 @@ SpEL supports the following logical operators:
* `or` (`||`) * `or` (`||`)
* `not` (`!`) * `not` (`!`)
The following example shows how to use the logical operators The following example shows how to use the logical operators:
[source,java,indent=0,subs="verbatim,quotes",role="primary"] [source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java .Java
@ -1222,10 +1222,11 @@ The following example shows how to use the logical operators
[[expressions-operators-mathematical]] [[expressions-operators-mathematical]]
==== Mathematical Operators ==== Mathematical Operators
You can use the addition operator on both numbers and strings. You can use the subtraction, multiplication, You can use the addition operator (`+`) on both numbers and strings. You can use the
and division operators only on numbers. You can also use subtraction (`-`), multiplication (`*`), and division (`/`) operators only on numbers.
the modulus (%) and exponential power (^) operators. Standard operator precedence is enforced. The You can also use the modulus (`%`) and exponential power (`^`) operators on numbers.
following example shows the mathematical operators in use: Standard operator precedence is enforced. The following example shows the mathematical
operators in use:
[source,java,indent=0,subs="verbatim,quotes",role="primary"] [source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java .Java
@ -1296,9 +1297,9 @@ following example shows the mathematical operators in use:
[[expressions-assignment]] [[expressions-assignment]]
==== The Assignment Operator ==== The Assignment Operator
To setting a property, use the assignment operator (`=`). This is typically To set a property, use the assignment operator (`=`). This is typically done within a
done within a call to `setValue` but can also be done inside a call to `getValue`. The call to `setValue` but can also be done inside a call to `getValue`. The following
following listing shows both ways to use the assignment operator: listing shows both ways to use the assignment operator:
[source,java,indent=0,subs="verbatim,quotes",role="primary"] [source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java .Java
@ -1333,9 +1334,9 @@ You can use the special `T` operator to specify an instance of `java.lang.Class`
type). Static methods are invoked by using this operator as well. The type). Static methods are invoked by using this operator as well. The
`StandardEvaluationContext` uses a `TypeLocator` to find types, and the `StandardEvaluationContext` uses a `TypeLocator` to find types, and the
`StandardTypeLocator` (which can be replaced) is built with an understanding of the `StandardTypeLocator` (which can be replaced) is built with an understanding of the
`java.lang` package. This means that `T()` references to types within `java.lang` do not need to be `java.lang` package. This means that `T()` references to types within the `java.lang`
fully qualified, but all other type references must be. The following example shows how package do not need to be fully qualified, but all other type references must be. The
to use the `T` operator: following example shows how to use the `T` operator:
[source,java,indent=0,subs="verbatim,quotes",role="primary"] [source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java .Java
@ -1365,9 +1366,10 @@ to use the `T` operator:
[[expressions-constructors]] [[expressions-constructors]]
=== Constructors === Constructors
You can invoke constructors by using the `new` operator. You should use the fully qualified class name You can invoke constructors by using the `new` operator. You should use the fully
for all but the types located in the core package `java.lang`. The following qualified class name for all types except those located in the `java.lang` package
example shows how to use the `new` operator to invoke constructors: (`Integer`, `Float`, `String`, and so on). The following example shows how to use the
`new` operator to invoke constructors:
[source,java,indent=0,subs="verbatim,quotes",role="primary"] [source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java .Java
@ -1376,7 +1378,7 @@ example shows how to use the `new` operator to invoke constructors:
"new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')") "new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')")
.getValue(Inventor.class); .getValue(Inventor.class);
//create new inventor instance within add method of List // create new Inventor instance within the add() method of List
p.parseExpression( p.parseExpression(
"Members.add(new org.spring.samples.spel.inventor.Inventor( "Members.add(new org.spring.samples.spel.inventor.Inventor(
'Albert Einstein', 'German'))").getValue(societyContext); 'Albert Einstein', 'German'))").getValue(societyContext);
@ -1388,7 +1390,7 @@ example shows how to use the `new` operator to invoke constructors:
"new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')") "new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')")
.getValue(Inventor::class.java) .getValue(Inventor::class.java)
//create new inventor instance within add method of List // create new Inventor instance within the add() method of List
p.parseExpression( p.parseExpression(
"Members.add(new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German'))") "Members.add(new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German'))")
.getValue(societyContext) .getValue(societyContext)