Skip to content

Commit

Permalink
Merge pull request #44 from synyx/fix-constructor-creation-with-colle…
Browse files Browse the repository at this point in the history
…ctions

Fix constructor creation with generic types and collections
  • Loading branch information
tknell authored Feb 21, 2024
2 parents 3b99f07 + 402a9af commit d131d4d
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.synyx.beanfiller.creator.Creator;
import org.synyx.beanfiller.domain.ObjectInformation;
import org.synyx.beanfiller.exceptions.FillingException;
import org.synyx.beanfiller.exceptions.WrongCreatorException;
import org.synyx.beanfiller.services.CreatorRegistry;
import org.synyx.beanfiller.util.GenericsUtils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

Expand Down Expand Up @@ -236,7 +236,12 @@ protected List<ObjectInformation> getTypeArgumentObjectInformation(ObjectInforma

List<ObjectInformation> typeArgumentObjectInformationList = new ArrayList<>();

List<Type> actualTypeArguments = GenericsUtils.getActualTypeArguments(objectInformation.getField());
List<Type> actualTypeArguments;
if(objectInformation.getType() instanceof ParameterizedType){
actualTypeArguments = Arrays.asList(((ParameterizedType) objectInformation.getType()).getActualTypeArguments());
}else{
actualTypeArguments = GenericsUtils.getActualTypeArguments(objectInformation.getField());
}

for (Type type : actualTypeArguments) {
typeArgumentObjectInformationList.add(createObjectInformationForType(type, objectInformation));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -82,10 +83,11 @@ public Object createObjectInternal(ObjectInformation parentInformation) throws F
instance = declaredConstructor.newInstance();
} else {
Class[] parameterTypes = declaredConstructor.getParameterTypes();
Type[] genericParameterTypes = declaredConstructor.getGenericParameterTypes();
Object[] parameters = new Object[declaredConstructor.getParameterCount()];

for (int i = 0; i < parameterTypes.length; i++) {
ObjectInformation information = new ObjectInformation(parameterTypes[i], null, null, null, null,
ObjectInformation information = new ObjectInformation(parameterTypes[i], null, genericParameterTypes[i], null, null,
null);
AbstractCreatorStrategy strategy = getStrategyManager().getStrategyFor(information);
parameters[i] = strategy.createObject(information);
Expand Down
51 changes: 32 additions & 19 deletions src/test/java/org/synyx/beanfiller/ConstructorCreationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import org.junit.Test;
import org.synyx.beanfiller.exceptions.FillingException;
import org.synyx.beanfiller.testobjects.ArrayConstructorObject;
import org.synyx.beanfiller.testobjects.ListConstructorObject;
import org.synyx.beanfiller.testobjects.MapConstructorObject;
import org.synyx.beanfiller.testobjects.TestEnum;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.*;

public class ConstructorCreationTest {

Expand All @@ -23,30 +25,45 @@ public void createsObjectViaConstructor() throws FillingException {
assertThat(object.innerObject.foo, notNullValue());
}

@Test
public void createsObjectWithListInConstructor() throws FillingException {

ListConstructorObject listConstructorObject = new BeanFiller().fillBean(ListConstructorObject.class);
assertThat(listConstructorObject,notNullValue());
assertThat(listConstructorObject.getValues(),notNullValue());
assertThat(listConstructorObject.getValues().size(), is(greaterThan(0)));
}

@Test
public void createsObjectWithArrayInConstructor() throws FillingException {

ArrayConstructorObject object = new BeanFiller().fillBean(ArrayConstructorObject.class);
assertThat(object,notNullValue());
assertThat(object.getValues(),notNullValue());
assertThat(object.getValues().length, is(greaterThan(0)));
}

@Test
public void createsObjectWithMapInConstructor() throws FillingException {

MapConstructorObject object = new BeanFiller().fillBean(MapConstructorObject.class);
assertThat(object,notNullValue());
assertThat(object.getValues(),notNullValue());
assertThat(object.getValues().size(), is(greaterThan(0)));
}

public static class TestObjectWithConstructor{

private final String[] array;
private final TestEnum testEnum;
private final InnerObject innerObject;

TestObjectWithConstructor(String[] array, TestEnum testEnum, InnerObject innerObject){
TestObjectWithConstructor(String[] array, TestEnum testEnum, InnerObject innerObject) {

this.array = array;
this.testEnum = testEnum;
this.innerObject = innerObject;
}

public String[] getArray() {
return array;
}

public TestEnum getTestEnum() {
return testEnum;
}

public InnerObject getInnerObject() {
return innerObject;
}
}

public static class InnerObject{
Expand All @@ -56,9 +73,5 @@ public static class InnerObject{
public InnerObject(String foo) {
this.foo = foo;
}

public String getFoo() {
return foo;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.synyx.beanfiller.testobjects;

public class ArrayConstructorObject {

private final String[] values;

public ArrayConstructorObject(String[] values){

this.values = values;
}

public String[] getValues() {
return values;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.synyx.beanfiller.testobjects;

import java.util.List;

public class ListConstructorObject {

private final List<String> values;

public ListConstructorObject(List<String> values){

this.values = values;
}

public List<String> getValues() {
return values;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.synyx.beanfiller.testobjects;

import java.util.Map;

public class MapConstructorObject {

private final Map<Integer, String> values;

public MapConstructorObject(Map<Integer, String> values){

this.values = values;
}

public Map<Integer, String> getValues() {
return values;
}
}

0 comments on commit d131d4d

Please sign in to comment.