easy-random icon indicating copy to clipboard operation
easy-random copied to clipboard

#471 Allow flexible concrete type resolution

Open npepinpe opened this issue 3 years ago • 2 comments

This PR introduces a new TypeResolver interface to the public API. This interface's default implementation is in ReflectionUtils, which is then delegated to the static helpers in ClassGraphFacade. This means the new feature remains backwards compatible with previous behavior.

Users can now specify a new parameter, EasyRandomParameters#typeResolver(TypeResolver), which lets them specify how concrete types will be resolved without coupling the library to ClassGraph in any way, though users can still use ClassGraph to implement it themselves.

An internal proxy class is used to wrap custom instances of TypeResolver set via the parameters. This is mostly because when creating the default ObjenesisObjectFactory, we need to pass it the current type resolver. However, it may get modified later on. So instead, we pass a proxy, which we can always modify thereafter. The proxy is not accessible to the user, so the only way to modify it is via the parameters. The downside here is that if a user modifies the parameters post-creation of EasyRandom, it will affect the internal ObjenesisObjectFactory and change the type resolver. I think that's a minor downside, as I don't expect this to be the common case, and I would hope users make use of the copy() factory method when reusing parameters. The copy() method will not copy the proxy but the actual TypeResolver, so it will really be a copy.

closes #471

npepinpe avatar Mar 02 '22 18:03 npepinpe

@npepinpe that's an interesting suggestion. I will try to implement your idea in Instancio. It's an alternative library that offers similar functionality to Easy Random. Since Easy Random is no longer maintained, I will link it here so others can find it.

https://github.com/instancio/instancio

armandino avatar Nov 16 '22 23:11 armandino

Thank you for your PR! I agree on the introduction of TypeResolver. However I am not a big fan a wrapping the default one in a proxy, I would keep it simple and just set the default where appropriate and make it configurable.

Please rebase the PR on the latest master and it should be good to merge. Thank you upfront.

fmbenhassine avatar Sep 17 '23 18:09 fmbenhassine