FlexTradeUKLtd / jfixture

JFixture is an open source library based on the popular .NET library, AutoFixture

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Circular generic types cause StackOverflowError

phutchin opened this issue · comments

If you've got two generic classes which refer two each other, via type arguments then JFixture end up in a StackOverflowError when trying to instantiate it.

I'm not sure if it's possible to handle it correctly, but a better error message would be helpful.
This appears to have only been introduced in version 2.7.2 #36 #37

example:

public class MySuperClass<T extends MyOtherSuperClass> { }
public class MyOtherSuperClass<T extends MySuperClass> { }

public class MyClass extends MySuperClass<MyOtherClass> { }
public class MyOtherClass extends MyOtherSuperClass<MyClass> {}

new JFixture().create(MyClass.class);

results in

java.lang.StackOverflowError
	at com.flextrade.jfixture.utility.SpecimenType$1.<init>(SpecimenType.java:51)
	at com.flextrade.jfixture.utility.SpecimenType.of(SpecimenType.java:51)
	at com.flextrade.jfixture.utility.GenericType$GenericTypeCreatorImpl.createGenericType(GenericType.java:48)
	at com.flextrade.jfixture.utility.SpecimenType.getGenericTypeMappings(SpecimenType.java:156)
	at com.flextrade.jfixture.utility.SpecimenType.createGenericTypeNameMap(SpecimenType.java:194)
	at com.flextrade.jfixture.utility.SpecimenType.getParameterizedTypeFields(SpecimenType.java:213)
	at com.flextrade.jfixture.utility.SpecimenType.getFields(SpecimenType.java:120)
	at com.flextrade.jfixture.utility.SpecimenType.getFieldsForClassType(SpecimenType.java:142)
	at com.flextrade.jfixture.utility.SpecimenType.getFields(SpecimenType.java:119)
	at com.flextrade.jfixture.utility.SpecimenType.<init>(SpecimenType.java:34)
	at com.flextrade.jfixture.utility.SpecimenType.<init>(SpecimenType.java:16)
	at com.flextrade.jfixture.utility.SpecimenType$1.<init>(SpecimenType.java:51)
	at com.flextrade.jfixture.utility.SpecimenType.of(SpecimenType.java:51)

It looks like it's not possible to be able to fixture something like MyClass correctly due to the circular reference on the Parameterized types. Like you've suggested the next best thing we can do is to try and detect if a Type will have a circular reference back to itself and emit a better error message.
I'm currently actively working on this so keep an eye out for upcoming pull requests

Sorry it's been a while - I've not had much time to get back onto this but I promised myself that I would try and resolve this! It looks like we do actually have to handle this properly in some way. Self referential generic types are pretty common and they also fall foul of this StackOverflowError when JFixture tries to fixture them. Example:

public class SelfReferentialType<T extends SelfReferentialType<T>> {}

public class MyType extends SelfReferentialType<MyType> {}

abstract class Enum<E extends Enum<E>> in the java.lang package of the JDK is another example of a self-referential generic type (though in JFixture we have specifically deal with Enums separately).

I'm currently trying to figure out a solution to handle this for most use cases.

I see this was merged, but there haven't been any new releases since 2.7.2 in September 2017. Any chance of a new release being created?