Skip to content

Commit 1ad5b4f

Browse files
committed
Support char primitive default values in BeanUtils.instantiateClass()
Closes gh-27390
1 parent 4d1cdf6 commit 1ad5b4f

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

spring-beans/src/main/java/org/springframework/beans/BeanUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ public abstract class BeanUtils {
8383
values.put(byte.class, (byte) 0);
8484
values.put(short.class, (short) 0);
8585
values.put(int.class, 0);
86-
values.put(long.class, (long) 0);
87-
values.put(float.class, (float) 0);
88-
values.put(double.class, (double) 0);
86+
values.put(long.class, 0L);
87+
values.put(float.class, 0F);
88+
values.put(double.class, 0D);
89+
values.put(char.class, '\0');
8990
DEFAULT_TYPE_VALUES = Collections.unmodifiableMap(values);
9091
}
9192

spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,30 +93,31 @@ void instantiateClassWithMoreArgsThanParameters() throws NoSuchMethodException {
9393
Constructor<BeanWithPrimitiveTypes> constructor = getBeanWithPrimitiveTypesConstructor();
9494

9595
assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() ->
96-
BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, "foo", null));
96+
BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, null, "foo", null));
9797
}
9898

9999
@Test // gh-22531, gh-27390
100100
void instantiateClassWithOptionalPrimitiveTypes() throws NoSuchMethodException {
101101
Constructor<BeanWithPrimitiveTypes> constructor = getBeanWithPrimitiveTypesConstructor();
102102

103-
BeanWithPrimitiveTypes bean = BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, "foo");
103+
BeanWithPrimitiveTypes bean = BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, null, "foo");
104104

105105
assertSoftly(softly -> {
106-
softly.assertThat(bean.isFlag()).isEqualTo(false);
106+
softly.assertThat(bean.isFlag()).isFalse();
107107
softly.assertThat(bean.getByteCount()).isEqualTo((byte) 0);
108108
softly.assertThat(bean.getShortCount()).isEqualTo((short) 0);
109109
softly.assertThat(bean.getIntCount()).isEqualTo(0);
110110
softly.assertThat(bean.getLongCount()).isEqualTo(0L);
111111
softly.assertThat(bean.getFloatCount()).isEqualTo(0F);
112112
softly.assertThat(bean.getDoubleCount()).isEqualTo(0D);
113+
softly.assertThat(bean.getCharacter()).isEqualTo('\0');
113114
softly.assertThat(bean.getText()).isEqualTo("foo");
114115
});
115116
}
116117

117118
private Constructor<BeanWithPrimitiveTypes> getBeanWithPrimitiveTypesConstructor() throws NoSuchMethodException {
118119
return BeanWithPrimitiveTypes.class.getConstructor(boolean.class, byte.class, short.class, int.class,
119-
long.class, float.class, double.class, String.class);
120+
long.class, float.class, double.class, char.class, String.class);
120121
}
121122

122123
@Test
@@ -566,19 +567,22 @@ private static class BeanWithPrimitiveTypes {
566567
private long longCount;
567568
private float floatCount;
568569
private double doubleCount;
570+
private char character;
569571
private String text;
570572

571573

572574
@SuppressWarnings("unused")
573575
public BeanWithPrimitiveTypes(boolean flag, byte byteCount, short shortCount, int intCount, long longCount,
574-
float floatCount, double doubleCount, String text) {
576+
float floatCount, double doubleCount, char character, String text) {
577+
575578
this.flag = flag;
576579
this.byteCount = byteCount;
577580
this.shortCount = shortCount;
578581
this.intCount = intCount;
579582
this.longCount = longCount;
580583
this.floatCount = floatCount;
581584
this.doubleCount = doubleCount;
585+
this.character = character;
582586
this.text = text;
583587
}
584588

@@ -610,6 +614,10 @@ public double getDoubleCount() {
610614
return doubleCount;
611615
}
612616

617+
public char getCharacter() {
618+
return character;
619+
}
620+
613621
public String getText() {
614622
return text;
615623
}

0 commit comments

Comments
 (0)