Skip to content

Commit

Permalink
Support char primitive default values in BeanUtils.instantiateClass()
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Sep 14, 2021
1 parent 4d1cdf6 commit 1ad5b4f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Expand Up @@ -83,9 +83,10 @@ public abstract class BeanUtils {
values.put(byte.class, (byte) 0);
values.put(short.class, (short) 0);
values.put(int.class, 0);
values.put(long.class, (long) 0);
values.put(float.class, (float) 0);
values.put(double.class, (double) 0);
values.put(long.class, 0L);
values.put(float.class, 0F);
values.put(double.class, 0D);
values.put(char.class, '\0');
DEFAULT_TYPE_VALUES = Collections.unmodifiableMap(values);
}

Expand Down
Expand Up @@ -93,30 +93,31 @@ void instantiateClassWithMoreArgsThanParameters() throws NoSuchMethodException {
Constructor<BeanWithPrimitiveTypes> constructor = getBeanWithPrimitiveTypesConstructor();

assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() ->
BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, "foo", null));
BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, null, "foo", null));
}

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

BeanWithPrimitiveTypes bean = BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, "foo");
BeanWithPrimitiveTypes bean = BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, null, "foo");

assertSoftly(softly -> {
softly.assertThat(bean.isFlag()).isEqualTo(false);
softly.assertThat(bean.isFlag()).isFalse();
softly.assertThat(bean.getByteCount()).isEqualTo((byte) 0);
softly.assertThat(bean.getShortCount()).isEqualTo((short) 0);
softly.assertThat(bean.getIntCount()).isEqualTo(0);
softly.assertThat(bean.getLongCount()).isEqualTo(0L);
softly.assertThat(bean.getFloatCount()).isEqualTo(0F);
softly.assertThat(bean.getDoubleCount()).isEqualTo(0D);
softly.assertThat(bean.getCharacter()).isEqualTo('\0');
softly.assertThat(bean.getText()).isEqualTo("foo");
});
}

private Constructor<BeanWithPrimitiveTypes> getBeanWithPrimitiveTypesConstructor() throws NoSuchMethodException {
return BeanWithPrimitiveTypes.class.getConstructor(boolean.class, byte.class, short.class, int.class,
long.class, float.class, double.class, String.class);
long.class, float.class, double.class, char.class, String.class);
}

@Test
Expand Down Expand Up @@ -566,19 +567,22 @@ private static class BeanWithPrimitiveTypes {
private long longCount;
private float floatCount;
private double doubleCount;
private char character;
private String text;


@SuppressWarnings("unused")
public BeanWithPrimitiveTypes(boolean flag, byte byteCount, short shortCount, int intCount, long longCount,
float floatCount, double doubleCount, String text) {
float floatCount, double doubleCount, char character, String text) {

this.flag = flag;
this.byteCount = byteCount;
this.shortCount = shortCount;
this.intCount = intCount;
this.longCount = longCount;
this.floatCount = floatCount;
this.doubleCount = doubleCount;
this.character = character;
this.text = text;
}

Expand Down Expand Up @@ -610,6 +614,10 @@ public double getDoubleCount() {
return doubleCount;
}

public char getCharacter() {
return character;
}

public String getText() {
return text;
}
Expand Down

0 comments on commit 1ad5b4f

Please sign in to comment.