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

Support for any generic type #375

Closed
ivangreene opened this issue Oct 30, 2019 · 4 comments
Closed

Support for any generic type #375

ivangreene opened this issue Oct 30, 2019 · 4 comments

Comments

@ivangreene
Copy link

EasyRandom version: easy-random-core-4.0.0

When inside a class, we can randomize a field such as List. However this only seems to work on a limited set of known types (List, etc) and not on simply any type with parameters.

Failing test:

    static class Element<T> {
        T value;
    }

    static class Container {
        Element<String> element;
    }

    @Test
    public void shouldWork() {
        EasyRandom easyRandom = new EasyRandom();
        Container container = easyRandom.nextObject(Container.class);
        assert container.element.value instanceof String;
    }

Here is a pretty minimal way to get the actual type:

        Field elementField = Container.class.getDeclaredField("element");
        Type actualParameter = null;
        Type genericType = elementField.getGenericType();
        if (genericType instanceof ParameterizedType) {
            actualParameter = ((ParameterizedType) genericType).getActualTypeArguments()[0];
        }
        assert actualParameter == String.class;
@fmbenhassine
Copy link
Member

Thanks for opening this issue, we already use this kind of tricks in various places of the code base.

However, we had the discussion before about supporting generic types but we decided to not support them (please read the details in #242 and #313).

@ivangreene
Copy link
Author

@benas would you be prepared to review a pull request that implements handling for these types of situations? It really limits our ability to generate random beans for many common uses of parameterized types

@fmbenhassine
Copy link
Member

Why is this limiting? You can register a custom randomizer for that generic type as suggested here.

There are a lot of cases to cover to correctly implement this feature:

  • what if T in your example resolves to Element<List<String>> element; or Element<Map<Integer, List<String>>> element;?
  • what if the generic type you are looking for is not defined in the root type (as explained here)
  • etc

I believe the clean way to deal with this is to have something like Field.isGenericType() and then if true do Field.getActualType(), but this is not possible until reified generics are added to Java. Any other solution will be fighting type erasure with some kind of hacks IMO. I'm of course open for a PR you are welcome! But I'm not ready to maintain any complex logic (like the one proposed in #354, btw I invite you to read the comments there) unless you really come up with something easy 😉 .

@beluchin
Copy link

beluchin commented Nov 7, 2022

I put together a random value generator that fully supports generics. Consider taking a look at:
https://github.com/beluchin/rvg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants