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

Add support for field definition with a predicate #342

Closed
fmbenhassine opened this issue Feb 22, 2019 · 2 comments
Closed

Add support for field definition with a predicate #342

fmbenhassine opened this issue Feb 22, 2019 · 2 comments
Labels
Milestone

Comments

@fmbenhassine
Copy link
Member

As of v3.8, in order to define a field, one should use the FieldDefinition API. This API provides 5 methods to define field properties (named, ofType, inClass, isAnnotatedWith and hasModifiers).

As a library user, this limited set of properties makes it impossible to use a custom condition on a field, for example field.isSynthetic().

As a library developer, I will need to update the API each time a user asks to add a custom field property.

A better approach is to use a Predicate<Field> as a replacement for FieldDefinition. Thanks to java.util.function.Predicate#and and java.util.function.Predicate#or, multiple predicates can be combined to define a field just like FieldDefinition, adding the flexibility to define custom conditions without having to change the API each time. Here is an example:

Predicate<Field> predicate = named("name").and(ofType(String.class)).and(inClass(Person.class));

Random Beans would provide common predicates and let the user define additional predicates as needed. A first work on this feature can be reviewed in branch predicates.

@PascalSchumacher WDYT?

BTW, this idea came from a situation where I wanted to exclude all types defined in a given package. I though about adding a TypeDefinition API allowing one to write something like:

EnhancedRandom enhancedRandom = new EnhancedRandomBuilder()
	        .exclude(TypeDefinition.type().inPackage("io.github.benas.randombeans.beans").get())
	        .build();

But then I found myself exposing all properties of a Class. I quickly realized that this was not the best approach.. Hence the idea of using predicates: No need to expose properties + ability to provide custom conditions.

@fmbenhassine fmbenhassine changed the title Add ability to use predicates to define field definition Add ability to use predicates to define fields Feb 22, 2019
@fmbenhassine fmbenhassine changed the title Add ability to use predicates to define fields Add support to define fields using predicates Feb 22, 2019
@fmbenhassine fmbenhassine changed the title Add support to define fields using predicates Add support for field definition using predicates Feb 22, 2019
@fmbenhassine fmbenhassine changed the title Add support for field definition using predicates Add support for field definition with a predicate Feb 22, 2019
@fmbenhassine fmbenhassine added this to the 3.9.0 milestone Feb 22, 2019
@PascalSchumacher
Copy link
Collaborator

Good idea. 👍

@fmbenhassine
Copy link
Member Author

@PascalSchumacher Thank you for your quick feedback!

Rebased and merged as 4929f12.

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

No branches or pull requests

2 participants