-
I was looking through the docs and was wondering if there was a way to utilize instancio with JavaFX Beans. JavaFX Beans can be different in that their fields are normally property objects which can be viewed as a container for a value as seen below.
From reading the docs I had been hoping that I would be able to provide a custom generatorSpecProvider that would map the *Property types to proper generators that could then be used in combination with the Setter Assignment type to utilize the setters of the property with the base type (in this case String). However when attempting to do this I noticed the methodAssigner first checks if the field is final and if so assumes there is no setter for it then utilizes the field reflection assignment. For the case of classes like this where the actual field is more of a container for a value that assumption breaks down. The reason I would like to use the setterAssignment rather than using field assignment and creating a custom generator for the StringProperty class is that there are instances where you can have derived properties like below.
So in this case using the reflection assignment would break the connection between size and description since size even if ignored would be referencing a separate field. I recognize this is a bit of an edge case but was curious if there was anyway to use instancio in this case. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
@Sheikah45 this is an interesting use case. Thanks for bring it up! I don't have much experience with JavaFX. Any chance you could create a minimal project with some sample beans that I could clone? I would need to play around to see what might work. |
Beta Was this translation helpful? Give feedback.
-
@Sheikah45 thanks for providing the sample project! Looking at the bean classes I noticed that setter argument types don't match field types, e.g. a public class PlainJavaFxBean {
private final StringProperty stringValue = new SimpleStringProperty();
public void setStringValue(String stringValue) {
this.stringValue.set(stringValue);
} With method assignment field type and setter argument type must be the same, so unfortunately using method assignment will not work here. One option is to use assign via fields and use custom generators for the Generator<LongProperty> longPropertyGenerator = random -> new SimpleLongProperty(random.longRange(1, 10000)); For fields like ObservableList<String> listObservable = FXCollections.observableArrayList(); it's a bit tricky. It can be done using an internal SPI. It's not ideal, but can get the job done. Finally, fields like
probably won't be possible to set up using current version of Instancio. The next major version will include a new In general, populating these types of classes is somewhat outside the scope of the library because they are not regular POJOs. For this reason it takes some work to set things up. I included an example of each of the above in a fork of your sample repository. It includes the new https://github.com/armandino/instancio-fx-sample Hope this helps :) Edit: forgot to mention, I removed the module-info because the SPI implementation wasn't being picked up. |
Beta Was this translation helpful? Give feedback.
-
@armandino This is a bit of a necro but I noticed the addition of on_set_method_unmatched and thought it could be a way to work with the JavaFX beans. So I updated the sample and it did actually work as expected without using the SPI or a custom generator at all which you can see here https://github.com/Sheikah45/instancio-fx-sample. So I wanted to ask if this was intentional or if this should be viewed as an out of band usage that could break at any time in the future. |
Beta Was this translation helpful? Give feedback.
@Sheikah45 yes, this is the intended usage. Previously, Instancio only supported setters that have a matching field. Since v4 it can invoke setters that have no matching field using the
Keys.ON_SET_METHOD_UNMATCHED
setting, as you've done in the sample project. This makes it possible to populate classes like:The
Keys.ON_SET_METHOD_UNMATCHED
is marked as an experimental API since it's new behaviour, but it …