Skip to content

Commit b0570cd

Browse files
committed
Polish contribution
See gh-25367
1 parent 35c0ae7 commit b0570cd

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -712,10 +712,11 @@ private void growCollectionIfNecessary() {
712712
}
713713
TypeDescriptor elementType = this.collectionEntryDescriptor.getElementTypeDescriptor();
714714
try {
715-
Constructor<?> ctor = getConstructor(elementType.getType());
715+
Constructor<?> ctor = getDefaultConstructor(elementType.getType());
716716
int newElements = this.index - this.collection.size();
717717
while (newElements >= 0) {
718-
this.collection.add(ctor == null ? null : ctor.newInstance());
718+
// Insert a null value if the element type does not have a default constructor.
719+
this.collection.add(ctor != null ? ctor.newInstance() : null);
719720
newElements--;
720721
}
721722
}
@@ -725,7 +726,7 @@ private void growCollectionIfNecessary() {
725726
}
726727
}
727728

728-
Constructor<?> getConstructor(Class<?> type) {
729+
private Constructor<?> getDefaultConstructor(Class<?> type) {
729730
try {
730731
return ReflectionUtils.accessibleConstructor(type);
731732
}

spring-expression/src/test/java/org/springframework/expression/spel/IndexingTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -203,22 +203,21 @@ public void setGenericPropertyContainingListAutogrow() {
203203
public List<BigDecimal> decimals;
204204

205205
@Test
206-
public void autoGrowWithoutDefaultConstructor() {
206+
public void autoGrowListOfElementsWithoutDefaultConstructor() {
207207
this.decimals = new ArrayList<>();
208208
SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
209209
parser.parseExpression("decimals[0]").setValue(this, "123.4");
210-
assertThat(decimals.get(0)).isEqualTo(BigDecimal.valueOf(123.4));
210+
assertThat(decimals).containsExactly(BigDecimal.valueOf(123.4));
211211
}
212212

213213
@Test
214-
public void indexIntoPropertyContainingNullList() {
214+
public void indexIntoPropertyContainingListContainingNullElement() {
215215
this.decimals = new ArrayList<>();
216216
this.decimals.add(null);
217217
this.decimals.add(BigDecimal.ONE);
218218
SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
219219
parser.parseExpression("decimals[0]").setValue(this, "9876.5");
220-
assertThat(decimals.get(0)).isEqualTo(BigDecimal.valueOf(9876.5));
221-
assertThat(decimals.get(1)).isEqualTo(BigDecimal.ONE);
220+
assertThat(decimals).containsExactly(BigDecimal.valueOf(9876.5), BigDecimal.ONE);
222221
}
223222

224223
@Test

src/docs/asciidoc/core/core-expressions.adoc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -332,19 +332,20 @@ being placed in it. The following example shows how to do so:
332332
[[expressions-parser-configuration]]
333333
=== Parser Configuration
334334

335-
It is possible to configure the SpEL expression parser by using a parser configuration object
336-
(`org.springframework.expression.spel.SpelParserConfiguration`). The configuration
335+
It is possible to configure the SpEL expression parser by using a parser configuration
336+
object (`org.springframework.expression.spel.SpelParserConfiguration`). The configuration
337337
object controls the behavior of some of the expression components. For example, if you
338-
index into an array or collection and the element at the specified index is `null`,
339-
you can automatically create the element. This is useful when using expressions made up of a
340-
chain of property references. If you index into an array or list
341-
and specifying an index that is beyond the end of the current size of the array or
342-
list, you can automatically grow the array or list to accommodate that index. In order to add
343-
an element at the specified index, SpEL will try to create the element using a default
344-
constructor before setting the specified value. If the element type does not have a default
345-
constructor, `null` will be added. Note if there is no built-in or custom converter, that knows
346-
how to set the value, `null` will remain in the array or list at the specified index.
347-
The following example demonstrates how to automatically grow the list:
338+
index into an array or collection and the element at the specified index is `null`, SpEL
339+
can automatically create the element. This is useful when using expressions made up of a
340+
chain of property references. If you index into an array or list and specify an index
341+
that is beyond the end of the current size of the array or list, SpEL can automatically
342+
grow the array or list to accommodate that index. In order to add an element at the
343+
specified index, SpEL will try to create the element using the element type's default
344+
constructor before setting the specified value. If the element type does not have a
345+
default constructor, `null` will be added to the array or list. If there is no built-in
346+
or custom converter that knows how to set the value, `null` will remain in the array or
347+
list at the specified index. The following example demonstrates how to automatically grow
348+
the list:
348349

349350
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
350351
.Java

0 commit comments

Comments
 (0)