Skip to content

Commit f07e6a1

Browse files
committed
Support char primitive default values in BeanUtils.instantiateClass()
Closes gh-27390
1 parent 5cc0984 commit f07e6a1

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
@@ -87,9 +87,10 @@ public abstract class BeanUtils {
8787
values.put(byte.class, (byte) 0);
8888
values.put(short.class, (short) 0);
8989
values.put(int.class, 0);
90-
values.put(long.class, (long) 0);
91-
values.put(float.class, (float) 0);
92-
values.put(double.class, (double) 0);
90+
values.put(long.class, 0L);
91+
values.put(float.class, 0F);
92+
values.put(double.class, 0D);
93+
values.put(char.class, '\0');
9394
DEFAULT_TYPE_VALUES = Collections.unmodifiableMap(values);
9495
}
9596

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

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

100100
assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() ->
101-
BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, "foo", null));
101+
BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, null, "foo", null));
102102
}
103103

104104
@Test // gh-22531, gh-27390
105105
void instantiateClassWithOptionalPrimitiveTypes() throws NoSuchMethodException {
106106
Constructor<BeanWithPrimitiveTypes> constructor = getBeanWithPrimitiveTypesConstructor();
107107

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

110110
assertSoftly(softly -> {
111-
softly.assertThat(bean.isFlag()).isEqualTo(false);
111+
softly.assertThat(bean.isFlag()).isFalse();
112112
softly.assertThat(bean.getByteCount()).isEqualTo((byte) 0);
113113
softly.assertThat(bean.getShortCount()).isEqualTo((short) 0);
114114
softly.assertThat(bean.getIntCount()).isEqualTo(0);
115115
softly.assertThat(bean.getLongCount()).isEqualTo(0L);
116116
softly.assertThat(bean.getFloatCount()).isEqualTo(0F);
117117
softly.assertThat(bean.getDoubleCount()).isEqualTo(0D);
118+
softly.assertThat(bean.getCharacter()).isEqualTo('\0');
118119
softly.assertThat(bean.getText()).isEqualTo("foo");
119120
});
120121
}
121122

122123
private Constructor<BeanWithPrimitiveTypes> getBeanWithPrimitiveTypesConstructor() throws NoSuchMethodException {
123124
return BeanWithPrimitiveTypes.class.getConstructor(boolean.class, byte.class, short.class, int.class,
124-
long.class, float.class, double.class, String.class);
125+
long.class, float.class, double.class, char.class, String.class);
125126
}
126127

127128
@Test
@@ -659,19 +660,22 @@ private static class BeanWithPrimitiveTypes {
659660
private long longCount;
660661
private float floatCount;
661662
private double doubleCount;
663+
private char character;
662664
private String text;
663665

664666

665667
@SuppressWarnings("unused")
666668
public BeanWithPrimitiveTypes(boolean flag, byte byteCount, short shortCount, int intCount, long longCount,
667-
float floatCount, double doubleCount, String text) {
669+
float floatCount, double doubleCount, char character, String text) {
670+
668671
this.flag = flag;
669672
this.byteCount = byteCount;
670673
this.shortCount = shortCount;
671674
this.intCount = intCount;
672675
this.longCount = longCount;
673676
this.floatCount = floatCount;
674677
this.doubleCount = doubleCount;
678+
this.character = character;
675679
this.text = text;
676680
}
677681

@@ -703,6 +707,10 @@ public double getDoubleCount() {
703707
return doubleCount;
704708
}
705709

710+
public char getCharacter() {
711+
return character;
712+
}
713+
706714
public String getText() {
707715
return text;
708716
}

0 commit comments

Comments
 (0)