Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use copy constructors, as they will cause a stackoverflow #43

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,20 @@ public Object createObjectInternal(ObjectInformation parentInformation) throws F

// use constructor with most parameters to potentially set the most final fields
Constructor declaredConstructor = declaredConstructors.stream()
.filter(constructor ->
// filter copy constructors as they will cause a stackoverflow
!Arrays.asList(constructor.getParameterTypes()).contains(parentClazz)
)
.max(Comparator.comparingInt(Constructor::getParameterCount))
.orElse(null);

if(declaredConstructor==null){
throw new FillingException("There was no Creator set for the class " + parentClazz.getName()
+ " So we tried to instantiate it via constructor, but couldn't find a usable constructor. "
+ "Please provide at least a private empty constructor for usage. "
+ "Copy constructors will not be used.");
}

try {
declaredConstructor.setAccessible(true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package org.synyx.beanfiller.strategies;

import org.hamcrest.MatcherAssert;
import org.junit.Test;

import org.synyx.beanfiller.BeanFiller;
import org.synyx.beanfiller.domain.ObjectInformation;
import org.synyx.beanfiller.exceptions.FillingException;
import org.synyx.beanfiller.services.CreatorRegistry;
import org.synyx.beanfiller.testobjects.MultipleConstructorsObject;
import org.synyx.beanfiller.testobjects.NoDefaultConstructorObject;
import org.synyx.beanfiller.testobjects.PrivateConstructorObject;
import org.synyx.beanfiller.testobjects.*;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

import static org.hamcrest.Matchers.*;
import static org.synyx.beanfiller.testobjects.CopyConstructorObjectWithPrivateConstructor.COPY_CONSTRUCTOR_USED_VALUE;


/**
* @author Tobias Knell - [email protected]
*/
public class JustAnotherBeanStrategyTest {


@Test
public void testPrivateConstructor() throws FillingException {

Expand Down Expand Up @@ -56,6 +52,29 @@ public void testTakesTheConstructorWithTheHighestNumberOfParameters() throws Fil
assertThat(object.getBar(), notNullValue());
}

@Test(expected = FillingException.class)
public void failsWhenOnlyCopyConstructorIsAvailable() throws FillingException {

JustAnotherBeanStrategy strategy = setupStrategy();

strategy.createObject(new ObjectInformation(
CopyConstructorObject.class, null, null, null, null, null));
}

@Test
public void usesPrivateConstructorOverCopyConstructor() throws FillingException {

assertThat(new CopyConstructorObjectWithPrivateConstructor(null).getValue(), is(COPY_CONSTRUCTOR_USED_VALUE));

JustAnotherBeanStrategy strategy = setupStrategy();

CopyConstructorObjectWithPrivateConstructor object = (CopyConstructorObjectWithPrivateConstructor)
strategy.createObject(new ObjectInformation(CopyConstructorObjectWithPrivateConstructor.class, null, null, null, null, null));
assertThat(object, notNullValue());

assertThat(object.getValue(), is(not(COPY_CONSTRUCTOR_USED_VALUE)));
}


private JustAnotherBeanStrategy setupStrategy() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.synyx.beanfiller.testobjects;

public class CopyConstructorObject {
public CopyConstructorObject(CopyConstructorObject copy){
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.synyx.beanfiller.testobjects;

public class CopyConstructorObjectWithPrivateConstructor {
public static final String COPY_CONSTRUCTOR_USED_VALUE = "copy constructor used";

private String value;

private CopyConstructorObjectWithPrivateConstructor(){
// private for filling
}

public CopyConstructorObjectWithPrivateConstructor(CopyConstructorObjectWithPrivateConstructor copy){
this.value = COPY_CONSTRUCTOR_USED_VALUE;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
Loading