• Like
Javabeanproperties
Upcoming SlideShare
Loading in...5
×

Javabeanproperties

  • 177 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
177
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Java Bean properties
  • 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. PropertiesActive 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 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) 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 classPropertyChangeSupportaddPropertyChangeListener PropertyChangeSupportremovePropertyChangeListener classfirePropertyChange R.V.S.Lalitha.,M.Tech(Ph.D) 7
  • 8. Bound Properties classes and interfaces EventListener interfacePropertyChange PropertyChangeListener interface R.V.S.Lalitha.,M.Tech(Ph.D) 8
  • 9. Bound Properties classes and interfaces EventObject classPropertyChangeEventgetPropertyName PropertyChangeEvent classgetNewValue R.V.S.Lalitha.,M.Tech(Ph.D) 9
  • 10. R.V.S.Lalitha.,M.Tech(Ph.D) 10
  • 11. Constrained Properties classes and interfaces Object class Exception class EventListener interfaceVetoableChangeSupportaddVetoableChangeListener VetoableChangeremoveVetoableChangeListener Support classfireVetoableChange 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; }}
  • 14. Bean Instantiationtry {MyBean bean = (MyBean)Beans.instantiate(ClassLoader.getSystemClassLoader(), "MyBean");} catch (ClassNotFoundException e) {} catch (IOException e) {}
  • 15. Getting and setting properties of a beanObject 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 propertyint 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 propertyint 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 classesMethod class Constructor Array class Field class Modifier class class R.V.S.Lalitha.,M.Tech(Ph.D) 20
  • 21. The enhanced Class classtoString getClasses Class classforName getFieldsnewInstance getMethodsisInstance getConstructorsisAssignableForm getFieldisInterface getMethodisArray getConstructorisPrimitive getDeclaredClassgetName getDeclaredFieldsgetClassLoader getDeclaredMethodsgetSuperClass getDeclaredConstructorsgetInterface getDeclaredFieldgetComponentType getDeclaredMethodgetModifiers getDeclaredConstructorgetSigners R.V.S.Lalitha.,M.Tech(Ph.D) getResourceAsStream 21
  • 22. The Reflective Method ClassgetDeclaringClass getExceptionTypes Method ClassgetName equalsgetModifiers hashCodegetReturnType toStringgetParameterTypes 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 classesFeatureDescriptor isHidden FeatureDescriptorgetName setHidden ClasssetName getShortDescriptiongetDisplayName setShortDescriptionsetDisplayName setValueisExpert getValuesetExpert attributeNameBeanDescriptor BeanDescripto MethodDescriptor MethodDescrgetBeanClass getMethod r Class iptor ClassgetCustomizerClass getParameterDescrip tors R.V.S.Lalitha.,M.Tech(Ph.D) 24
  • 25. EventSetDescriptor getRemoveListenerMethod EventSetDescriptorgetListenerType setUnicast classgetListernerMethods isUnicastgetListernerMethodDescriptor setinDefaultEventSetsgetAddListenerMethod isInDefaultEventSetPropertyDescriptor setBound PropertyDescriptogetPropertyType isConstrained er ClassgetReadMethod setConstrainedgetWriteMethod setPropertyEditorClassisBound getPropertyEditorClassParameterDescriptor 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 beantry {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) {}