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) 2
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) 3
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) 4
5. Simple Properties
public 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) 5
}}
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) 6
7. Bound Properties
classes and interfaces
Object class
PropertyChangeSupport
addPropertyChangeListener
PropertyChangeSupport
removePropertyChangeListener class
firePropertyChange
R.V.S.Lalitha.,M.Tech(Ph.D) 7
9. Bound Properties
classes and interfaces
EventObject
class
PropertyChangeEvent
getPropertyName PropertyChangeEvent
class
getNewValue
R.V.S.Lalitha.,M.Tech(Ph.D) 9
11. Constrained Properties classes and interfaces
Object class Exception class EventListener
interface
VetoableChangeSupport
addVetoableChangeListener VetoableChange
removeVetoableChangeListener Support
class
fireVetoableChange
PropertyVetoException
PropertyVetoExce
getPropertyChangeEvent ption class
VetoableChange VetoableChangeListener
R.V.S.Lalitha.,M.Tech(Ph.D) interface 11
12. The Persistent bean streaming classes and interfaces
R.V.S.Lalitha.,M.Tech(Ph.D) 12
13. public class MyBean {
String prop1;
public String getProp1() {
return prop1; }
public void setProp1(String s) {
prop1 = s; }
}
15. 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) {}
16. //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);}
17. //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));
}
18. //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);}
19. //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; }}
20. Reflection classes
Object class
Beans
Classs class introspection
classes
Method class Constructor
Array class Field class Modifier class
class
R.V.S.Lalitha.,M.Tech(Ph.D) 20
21. The enhanced Class class
toString getClasses
Class class
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 R.V.S.Lalitha.,M.Tech(Ph.D) getResourceAsStream 21
22. The Reflective Method Class
getDeclaringClass getExceptionTypes
Method Class
getName equals
getModifiers hashCode
getReturnType toString
getParameterTypes invoke
R.V.S.Lalitha.,M.Tech(Ph.D) 22
23. Java beans Introspection: An Overview
Object class
SimpleBeanInfo FeatureDescriptor
class Introspector class
class
MethodDescriptor EventSetDescr PropertyDescri ParameterDe
class iptor class ptor class scriptor class
BeanDescripto IndexedProper
r class BeanInfo class tyDescriptor
class
R.V.S.Lalitha.,M.Tech(Ph.D) 23
24. JavaBeans Introspection:The descriptor classes
FeatureDescriptor isHidden
FeatureDescriptor
getName setHidden
Class
setName getShortDescription
getDisplayName setShortDescription
setDisplayName setValue
isExpert getValue
setExpert attributeName
BeanDescriptor
BeanDescripto MethodDescriptor
MethodDescr
getBeanClass getMethod
r Class iptor Class
getCustomizerClass getParameterDescrip
tors
R.V.S.Lalitha.,M.Tech(Ph.D) 24
25. EventSetDescriptor getRemoveListenerMethod
EventSetDescriptor
getListenerType setUnicast
class
getListernerMethods isUnicast
getListernerMethodDescriptor setinDefaultEventSet
s
getAddListenerMethod isInDefaultEventSet
PropertyDescriptor setBound
PropertyDescripto
getPropertyType isConstrained
er Class
getReadMethod setConstrained
getWriteMethod setPropertyEditorClass
isBound getPropertyEditorClass
ParameterDescriptor IndexedPropertyDesc IndexedPropertyDe
ParameterDes riptor scriptor Class
criptor Class
getIndexedReadMeth
od
getIndexedWriteMeth
R.V.S.Lalitha.,M.Tech(Ph.D) 25
od
26. //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) {}