Java Bean properties
R.V.S.Lalitha.,M.Tech(Ph.D) 2
Property
• Property is a public attribute of a bean that you can manipulate via getter and setter methods
• Properties typically correspond to fields that define the internal state of an object.
R.V.S.Lalitha.,M.Tech(Ph.D) 3
Properties
Active properties:Changing of a property can result in the firing of an event, these are called active properties. Active properties can be Bound or Constrained.
Bound and Constrained properties:Events used to notify, when a property is changed for a bean.
Standalone properties: It is standalone, which in this case means that it is not
connected to via events to listeners.
R.V.S.Lalitha.,M.Tech(Ph.D) 4
Simple Properties
• getXXX() – accessor method to access internal variable
• isXXX() – accessor method to flag to the introspector a single valued boolean property
R.V.S.Lalitha.,M.Tech(Ph.D) 5
Simple Propertiespublic class person{private int age;private boolean single;person(int newage){age=newage;}public int getAge(){return age;}public void setSingle(boolean status){single=status;}public boolean isSingle(){return single;}}
R.V.S.Lalitha.,M.Tech(Ph.D) 6
Multi-valued indexed properties
An indexed property contains a collection of values that are of same type.Ex:public class myfriends{private String friends[];myfriends(String[] list){friends=list;}public String getfriends(int index){return friends[index];}public String[] getfriends(){return friends;}
R.V.S.Lalitha.,M.Tech(Ph.D) 7
Bound Properties classes and interfaces
PropertyChangeSupport
addPropertyChangeListener
removePropertyChangeListener
firePropertyChange
Object class
PropertyChangeSupport class
R.V.S.Lalitha.,M.Tech(Ph.D) 8
Bound Properties classes and interfaces
PropertyChange
8
EventListenerinterface
PropertyChangeListener interface
R.V.S.Lalitha.,M.Tech(Ph.D) 9
Bound Properties classes and interfaces
PropertyChangeEvent
getPropertyName
getNewValue
EventObject class
PropertyChangeEvent class
R.V.S.Lalitha.,M.Tech(Ph.D) 10
R.V.S.Lalitha.,M.Tech(Ph.D) 11
Constrained Properties classes and interfaces
VetoableChangeSupport
addVetoableChangeListener
removeVetoableChangeListener
fireVetoableChange
11
Object class
VetoableChangeSupport
class
PropertyVetoException
getPropertyChangeEvent
Exception class EventListener interface
PropertyVetoException class
VetoableChangeVetoableChangeListener
interface
R.V.S.Lalitha.,M.Tech(Ph.D) 12
The Persistent bean streaming classes and interfaces
public class MyBean { String prop1; public String getProp1() { return prop1; } public void setProp1(String s) { prop1 = s; } }
Bean Instantiation
try {MyBean bean =
(MyBean)Beans.instantiate(ClassLoader.getSystemClassLoader(), "MyBean");
} catch (ClassNotFoundException e) {} catch (IOException e) {}
Getting and setting properties of a bean
Object o = new MyBean();try { // Get the value of prop1 Expression expr = new Expression(o, "getProp1", new Object[0]);
expr.execute(); String s = (String)expr.getValue(); // Set the value of prop1 Statement stmt = new Statement(o, "setProp1", new Object[]{"new
string"}); stmt.execute();}catch (Exception e) {}
//implementing a bound property
• // Create the listener list.• PropertyChangeSupport pcs = new PropertyChangeSupport(this);• // The listener list wrapper methods.• public synchronized void
addPropertyChangeListener(PropertyChangeListener listener) { pcs.addPropertyChangeListener(listener);}
• public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { pcs.removePropertyChangeListener(listener);}
//implementing a bound property
int myProperty;public int getMyProperty() { return myProperty;}public void setMyProperty(int newValue) { int oldValue = myProperty; myProperty = newValue;
pcs.firePropertyChange("myProperty", new Integer(oldValue), new Integer(newValue));
}
//implementing constrained property
// Create the listener list.VetoableChangeSupport vcs = new VetoableChangeSupport (this); // The listener list wrapper methods.public synchronized void addVetoableChangeListener
(VetoableChangeListener listener) { vcs.addVetoableChangeListener(listener);}
public synchronized void removeVetoableChangeListener (VetoableChangeListener listener) { vcs.removeVetoableChangeListener(listener);}
//implementing constrained property
int myProperty;public int getMyProperty() { return myProperty;}public void setMyProperty(int newValue) throws
PropertyVetoException { try { vceListeners.fireVetoableChange( "myProperty", new
Integer(myProperty), new Integer(newValue)); myProperty = newValue; } catch (PropertyVetoException e) { throw e; }}
R.V.S.Lalitha.,M.Tech(Ph.D) 20
Reflection classes
Object class
Classs classBeans
introspection classes
Method class Array class Constructor class Field class Modifier class
R.V.S.Lalitha.,M.Tech(Ph.D) 21
The enhanced Class classtoString Class class getClasses
forName getFields
newInstance getMethods
isInstance getConstructors
isAssignableForm getField
isInterface getMethod
isArray getConstructor
isPrimitive getDeclaredClass
getName getDeclaredFields
getClassLoader getDeclaredMethods
getSuperClass getDeclaredConstructors
getInterface getDeclaredField
getComponentType getDeclaredMethod
getModifiers getDeclaredConstructor
getSigners getResourceAsStream
R.V.S.Lalitha.,M.Tech(Ph.D) 22
The Reflective Method ClassgetDeclaringClass Method Class getExceptionTypes
getName equals
getModifiers hashCode
getReturnType toString
getParameterTypes invoke
R.V.S.Lalitha.,M.Tech(Ph.D) 23
Java beans Introspection: An Overview
Object class
SimpleBeanInfo class Introspector class
MethodDescriptor class
EventSetDescriptor class
PropertyDescriptor class
ParameterDescriptor class
BeanDescriptor class
FeatureDescriptor class
BeanInfo classIndexedProper
tyDescriptor class
R.V.S.Lalitha.,M.Tech(Ph.D) 24
JavaBeans Introspection:The descriptor classesFeatureDescriptor FeatureDescriptor
ClassisHidden
getName setHiddensetName getShortDescriptiongetDisplayName setShortDescriptionsetDisplayName setValueisExpert getValuesetExpert attributeName
BeanDescriptor BeanDescriptor ClassgetBeanClass
getCustomizerClass
MethodDescriptor MethodDescriptor ClassgetMethod
getParameterDescriptors
R.V.S.Lalitha.,M.Tech(Ph.D) 25
EventSetDescriptor EventSetDescriptor class
getRemoveListenerMethod
getListenerType setUnicast
getListernerMethods isUnicast
getListernerMethodDescriptors
setinDefaultEventSet
getAddListenerMethod isInDefaultEventSet
PropertyDescriptor PropertyDescriptoer Class
setBound
getPropertyType isConstrained
getReadMethod setConstrained
getWriteMethod setPropertyEditorClass
isBound getPropertyEditorClass
ParameterDescriptor ParameterDescriptor Class
IndexedPropertyDescriptor
IndexedPropertyDescriptor Class
getIndexedReadMethodgetIndexedWriteMethodgetIndexedPropertyType
//listing the properties of a bean
try { BeanInfo bi = Introspector.getBeanInfo(MyBean.class);
PropertyDescriptor[] pds = bi.getPropertyDescriptors(); for (int i=0; i<pds.length; i++) { // Get property name String propName = pds[i].getName(); } catch (java.beans.IntrospectionException e) {}
Recommended