- class Customer {
- public Property<String> name = new Property();
- }
- public class Property<T> {
- public final String name;
- T value;
- private final PropertyChangeSupport support;
- public static <T> Property<T> newInstance(String name, T value,
- PropertyChangeSupport support) {
- return new Property<T>(name, value, support);
- }
- public static <T> Property<T> newInstance(String name, T value) {
- return newInstance(name, value, null);
- }
- public Property(String name, T value, PropertyChangeSupport support) {
- this.name = name;
- this.value = value;
- this.support = support;
- }
- public T getValue() { return value; }
- public void setValue(T value) {
- T old = this.value;
- this.value = value;
- if(support != null)
- support.firePropertyChange(name, old, this.value);
- }
- public String toString() { return value.toString(); }
- }
- public class Customer {
- private final PropertyChangeSupport support = new PropertyChangeSupport();
- public final Property<String> name = Property.newInstance("name", "", support);
- public final Property<Integer> age = Property.newInstance("age", 0, support);
- ... declare add/remove listenener ...
- }
- Customer c = new Customer();
- c.name.setValue("Hyrum");
- c.age.setValue(49);
- System.out.println("%s : %s", c.name, c.age);
- @Bean(
- properties={
- @Property(name="name"),
- @Property(name="phone", bound=true),
- @Property(name="friend", type=Person.class, kind=PropertyKind.LIST)
- }
- )
- public class Person extends PersonGen {}
- interface IListenable {
- void addPropertyChangeListener( PropertyChangeListener listener );
- void removePropertyChangeListener( PropertyChangeListener listener );
- }
- abstract class MyBean extends IListenable {
- public abstract void setName(String name);
- public abstract String getName();
- // more things
- }
- public class JavaBeanFactory {
- public <T> Class<T> generate(Class<T> clazz) {
- // I used here CGLIB to generate dynamically a class that implements the methods:
- // getters
- // setters
- // addPropertyChangeListener
- // removePropertyChangeListener
- }
- }
- public class Foo {
- @Inject
- public Provider<MyBean> myBeanProvider;
- public MyBean createHook(MyBean a) {
- final MyBean b = myBeanProvider.get();
- a.addPropertyChangeListener(new PropertyChangeListener() {
- public void propertyChange(PropertyChangeEvent evt) {
- b.setName((String) evt.getNewValue());
- }
- });
- return b;
- }
- }