OpenPojo / openpojo

POJO Testing & Identity Management Made Trivial
http://openpojo.com
Apache License 2.0
156 stars 40 forks source link

RandomFactory should support generics on collections #45

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Osman and I have talked about this but I wanted to add it as a formal request 
for enhancement.

Given an object like this:

class Container {
    List<Item> list;
}

Currently RandomFactory recognizes "list" as a List, but it doesn't pay 
attention to the type of objectcs the list stores, and generates a List with 
numbers in it instead of generating instances of Item.

Since RandomFactory already knows how to create instances of Item, it would be 
nice if it could detect the generic type and create the objects for the list.

There are definitely complications, for example what if we have 
List<Item<X<Y>>>, etc.  But in many simple cases RandomFactory could start to 
behave smarter while those complications are saved for a later release.

Alternative -

If we want OpenPojo to remain clean until these complications can be solved 
100%, there is another way to approach this which would allow users to 
implement a new kind of generator, buying some time until OpenPojo supports 
this fully.

For example in addition to RandomGenerator, we could add another kind of 
generator like this:

interface RandomFieldGenerator {
    // same as RandomGenerator
    public Collection<Class<?>> getTypes();

    // similar to RandomGenerator but with a Field parameter
    public Object doGenerate(Class<?> type, @Nullable java.lang.reflect.Field field);
}

The field would be null for a top-level class or if the field is not available 
for some reason.

With this type of approach I would be able to register a generator on 
List.class and populate it with the correct type gleaned from 
field.getGenericType() etc.

Original issue reported on code.google.com by jmccabe....@gmail.com on 26 Sep 2014 at 4:37

GoogleCodeExporter commented 9 years ago

Original comment by oshou...@gmail.com on 27 Sep 2014 at 5:16

GoogleCodeExporter commented 9 years ago

Original comment by oshou...@gmail.com on 27 Sep 2014 at 5:16

GoogleCodeExporter commented 9 years ago

Original comment by oshou...@gmail.com on 1 Nov 2014 at 5:31