Java beans - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Java beans

Description:

An editor for a given property can be acquired by calling the manager's static method: ... Visual editing: Display a table of properties names and values ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 14
Provided by: csBg
Category:
Tags: beans | java

less

Transcript and Presenter's Notes

Title: Java beans


1
Java beans
  • Part 1 introspecting beans

2
What are Java beans?
  • Java beans are simply Java classes that exposes
    properties and events by a naming convention
  • The Java beans mechanism allows introspection and
    manipulation of such classes using reflection
  • Get a list of properties
  • Get or set a specific property
  • Get a list of events
  • Add and remove listeners

3
Why Java beans?
  • Reflection is too general to handle software
    components well. A naming convention standard
    mechanism encourages using comp.
  • The most commonly needed form of reflection is
    exposing properties and events
  • Properties determines the state of the object
  • Configuration by external tools
  • Displaying, serializing
  • Events allows activating it

4
Java beans properties
  • A property is determined by methods names
  • A property named foo of type Goo can have
  • Getter for reading Goo getFoo()
  • Setter for writing void setFoo(Goo goo)
  • So a class having such getter/setter method,
    defines its properties
  • The Java beans mechanism uses reflection to find
    these methods to describe properties

5
Class and properties example
  • class MyBean
  • public Color getForeground()
  • public void setForeground(Color c)
  • public int getSize()
  • public void setFont(Font f)
  • The class MyBean has the following properties
  • Read/Write foreground Color
  • Read-only size int
  • Write-only font Font

6
Other properties
  • Beside the simple properties, a bean can have
  • Indexed properties like simple but with an index
  • Getter Goo getFoo(int i)
  • Setter void setFoo(int i, Goo foo)
  • All getter Goo getFoo
  • All setter void setFoo(Goo foo)
  • Bounded properties when changed, a
    PropertyChangedEvent is fired
  • Constrained properties can be vetoed by listener

7
Introspecting a bean class
  • Java beans classes package java.beans
  • The class Introspector allows getting a BeanInfo
    on a certain bean class
  • The BeanInfo allows getting
  • Property descriptors (PropertyDescriptor)
  • Event descriptors (EventDescriptor)
  • Method descriptors (MethodDescriptor)
  • For the general use

8
Property descriptors
  • The PropertyDescriptor allow getting
  • The propertys name, type and other info
  • The getter method (if any)
  • The setter method (if any)
  • From this point, straight reflection can be used
    to effect the bean object itself
  • The descriptor can be obtained either
  • From a class introspection
  • Directly by constructor (if property name is
    known)

9
Other property descriptors
  • The general property descriptor also describes
    bounded and constrained properties
  • Has checks isBound() and isConstrained()
  • A sub-class IndexedPropertyDescriptor allows
    accessing the methods for indexed property access

10
Property editors (1)
  • One main problem with opaque beans is that the
    input/output is many times in text format.
  • To help, the beans mechanism includes a way to
    translate strings to objects and vice verse.
  • The PropertyEditor interface declares methods for
    translating to/from text
  • A static PropertyEditorManager handles getting
    and registering editors

11
Property editors (2)
  • An editor for a given property can be acquired by
    calling the managers static method
    findEditor(Class type)
  • Notice that the type is the type of the property
  • Returns a property editor for the given property
  • The editor has the translation methods
  • setAsText(text), String getAsText()
  • Object getValue(), setValue(Object)

12
Possible usage of Java beans
  • Visual editing
  • Display a table of properties names and values
  • When values change, it effects the displayed bean
  • Storage
  • Get properties and values and write them to a
    file
  • Load the file, create a new object and set
    properties
  • ? The new XML serializer for beans!
  • Display
  • Use a generic table to display content of any
    object

13
Problems with Java beans
  • The Java beans mechanism is more structured and
    organized then raw reflection, but it still has
    its drawbacks
  • It is based on a naming convention(And therefore
    the good will of other programmers)
  • It requires a lot of type checks
  • It still uses reflection!
  • ? Better then reflection, but still a last
    resort
Write a Comment
User Comments (0)
About PowerShow.com