You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
... I want EasyRandom to only fill the name when creating objects with easyRandom.nextObject(User.class).
I tried two different ways to implement this, but I'm not happy with either one of them:
// works but is not really futureproof. New superclasses must be added to the listIGNORED_CLASSES = Arrays.asList(BaseEntity.class);
...
.excludeField(f -> !IGNORED_CLASSES.contains(field.getDeclaringClass()))
// works but it's a bit hacky imho.// delegating to the original ExclusionPolicy instead of simply extending it, because// a) the default policy might change and// b) the original implementation is package-private: https://github.com/j-easy/easy-random/blob/master/easy-random-core/src/main/java/org/jeasy/random/ExclusionChecker.javaEasyRandomParametersparameters = newEasyRandomParameters();
ExclusionPolicyoriginalPolicy = parameters.getExclusionPolicy();
parameters.setExclusionPolicy(newExclusionPolicy() {
@OverridepublicbooleanshouldBeExcluded(Fieldfield, RandomizerContextcontext) {
returnoriginalPolicy.shouldBeExcluded(field, context) || field.getDeclaringClass() == context.getCurrentObject().getClass();
}
@OverridepublicbooleanshouldBeExcluded(Class<?> type, RandomizerContextcontext) {
returnoriginalPolicy.shouldBeExcluded(type, context);
}
});
Unfortunately using org.jeasy.random.annotation.Exclude is also not an option, because the superclasses actually live inside another library I have no control over.
The text was updated successfully, but these errors were encountered:
delegating to the original ExclusionPolicy instead of simply extending it
I made ExclusionChecker public and renamed it to DefaultExclusionPolicy. You can now extend it with additional exclusion logic and register it in your config. Here is a quick test that shows how to exclude inherited fields based on your example:
I need EasyRandom to ignore fields in superclasses.
Given the following classes (getter/setter for better readability omitted) ...
... I want EasyRandom to only fill the
name
when creating objects witheasyRandom.nextObject(User.class)
.I tried two different ways to implement this, but I'm not happy with either one of them:
Unfortunately using
org.jeasy.random.annotation.Exclude
is also not an option, because the superclasses actually live inside another library I have no control over.The text was updated successfully, but these errors were encountered: