1

I am wondering how can I define protected, public and private properties in my class GenericBean, which will result in a JavaBean. So far I've declared a class, that will enable use to access the value of the Bean, however, I have no idea how I can handle different accesses for those properties. Any idea? Here is m y class:

abstract class GenericBean {

    protected PropertyChangeSupport chg = new PropertyChangeSupport(this);
    protected VetoableChangeSupport veto = new VetoableChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener pcl) {
        chg.addPropertyChangeListener(pcl);
    }

    public void removePropertyChangeListener(PropertyChangeListener pcl) {
        chg.addPropertyChangeListener(pcl);
    }

    class BoundedProperty<T>  implements PropertyChangeListener {
        private String name;
        private T value;
        private Object chgHandlerObject;
        private Method changeHandler;

        public BoundedProperty(String name) {
            this.name = name;
        }

        public T getValue() { return value; }

        public void setValue(T newValue) {
            T old = value;
            value = newValue;
            chg.firePropertyChange(name, old, value);
        }

        public void propertyChange(PropertyChangeEvent e) {
            if (!e.getPropertyName().equals(name)) return;
            if (changeHandler == null) return;
            try {
                changeHandler.invoke(chgHandlerObject);
            } catch(Exception exc) {
                exc.printStackTrace();
            }
        }

        public void setChangeHandler(Object handl, String mname) {
            try {
                Method m = handl.getClass().getDeclaredMethod(mname);
                chgHandlerObject = handl;
                changeHandler = m;
                chg.addPropertyChangeListener(this);
            } catch(Exception exc) {
                exc.printStackTrace();
                return;
            }
        }


        public void setChangeHandler(Object ohandler) {
            try {
                Method m = ohandler.getClass().getDeclaredMethod(name+"Change");
                chgHandlerObject = ohandler;
                changeHandler = m;
            } catch(Exception exc) {
                exc.printStackTrace();
                return;
            }
            chg.addPropertyChangeListener(this);
        }

        public void removeChangeHandler() {
            changeHandler = null;
            chgHandlerObject = null;
            chg.removePropertyChangeListener(this);
        }
    }


}

So that I can decide which methods are available for certain fields?

alegrowski
  • 261
  • 2
  • 3
  • 8
  • This depends of course on what you want to allow the derived classes and the users of these classes. Or possibly I don't understand your problem correctly? – Uwe Allner Apr 07 '16 at 07:19
  • @UweAllner, I want to enable/disable setValue and getValue depending on whether property is public, private or protected – alegrowski Apr 07 '16 at 07:24
  • @UweAllner is there a way to find out if property is public private or protected within the class BoundedProperty, so that I can add VetoHandler for this? – alegrowski Apr 07 '16 at 07:25
  • By retrieving the Field from the class of T and evaluating the modifiers? – Uwe Allner Apr 07 '16 at 08:17

0 Answers0