Polish contribution

See gh-25367
This commit is contained in:
Sam Brannen
2020-07-16 15:39:36 +02:00
parent 35c0ae7b0c
commit b0570cd3a6
3 changed files with 23 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -712,10 +712,11 @@ public class Indexer extends SpelNodeImpl {
}
TypeDescriptor elementType = this.collectionEntryDescriptor.getElementTypeDescriptor();
try {
Constructor<?> ctor = getConstructor(elementType.getType());
Constructor<?> ctor = getDefaultConstructor(elementType.getType());
int newElements = this.index - this.collection.size();
while (newElements >= 0) {
this.collection.add(ctor == null ? null : ctor.newInstance());
// Insert a null value if the element type does not have a default constructor.
this.collection.add(ctor != null ? ctor.newInstance() : null);
newElements--;
}
}
@@ -725,7 +726,7 @@ public class Indexer extends SpelNodeImpl {
}
}
Constructor<?> getConstructor(Class<?> type) {
private Constructor<?> getDefaultConstructor(Class<?> type) {
try {
return ReflectionUtils.accessibleConstructor(type);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -203,22 +203,21 @@ public class IndexingTests {
public List<BigDecimal> decimals;
@Test
public void autoGrowWithoutDefaultConstructor() {
public void autoGrowListOfElementsWithoutDefaultConstructor() {
this.decimals = new ArrayList<>();
SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
parser.parseExpression("decimals[0]").setValue(this, "123.4");
assertThat(decimals.get(0)).isEqualTo(BigDecimal.valueOf(123.4));
assertThat(decimals).containsExactly(BigDecimal.valueOf(123.4));
}
@Test
public void indexIntoPropertyContainingNullList() {
public void indexIntoPropertyContainingListContainingNullElement() {
this.decimals = new ArrayList<>();
this.decimals.add(null);
this.decimals.add(BigDecimal.ONE);
SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
parser.parseExpression("decimals[0]").setValue(this, "9876.5");
assertThat(decimals.get(0)).isEqualTo(BigDecimal.valueOf(9876.5));
assertThat(decimals.get(1)).isEqualTo(BigDecimal.ONE);
assertThat(decimals).containsExactly(BigDecimal.valueOf(9876.5), BigDecimal.ONE);
}
@Test