mercredi 6 septembre 2017

How to write in source code a class with dynamic getters and setters (e.g. with BeanUtils' DynaProperties)?

In a given code base I want to replace some traditional beans with "dynamic beans", i.e. instantiation of the newly written classes should behave like with normal beans, just instead of fixed getters and setters I use parameterised get and set methods.

As an example, let's keep it simple and assume I want to define a bean DynaMyBean with two members "name" and "job". Then the code using the dynamic bean and creating an instance should look similar to normal beans, ie. like this:

DynaMyBean bean = new DynaMyBean(); bean.set("name", "Peter"); bean.set("job", "juggler"

I came across Apache's BeanUtils and thought that the (Basic)DynaClass could do the job. So I tried (probably a bit naively)

public class DynaMyBean extends BasicDynaClass { public DynaMyBean() { super ("MyBean", null, new DynaProperty[] { new DynaProperty("name", String.class), new DynaProperty("job", String.class) }); } }

But with this definition new DynaMyBean() does not create a class with a set or get method. Somehow it is a bean factory, not the bean itself.

To be clear: I do not want to usea bean factory class (Examples for that I found plenty in the BeanUtils tutorials); I want to write a class which itself is a bean-like class, just with dynamic getters and setters.

How can I achieve this?





Aucun commentaire:

Enregistrer un commentaire