Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
JavaBeans: Introspection and Customization Umer Farooq January 30, 2002 CS6704: Design Patterns & Component Frameworks Deploying Beans to the Enterprise How do we treat beans as independent software components? How do you examine a bean and expose its features: properties, events, and methods? How do you create global international beans? Reflection and Introspection A Bean has to reveal it’s properties, methods and events to the builder tool. Reflection is the ability to obtain information about the fields,constructors and methods of any class. Introspection is the ability to obtain information about the properties, events and methods of a Bean. Reflection Is Java a true Object-Oriented language? For reflection, all data types must be objects Classes and interfaces exist for reflection: class Array, Class, Constructor, Field, Method, Modifier interface Member Introspection class Introspector uses core reflection API to discover a Bean’s methods All java.beans.Introspector class methods are static Call Introspector.getBeanInfo(…) method to get the BeanInfo object Explicitly expose features Rely on BeanInfo to expose some Bean features while relying on low-level reflection to expose others. BeanInfo interface BeanInfo defines methods that return descriptors for each property, method, or event that you want exposed: PropertyDescriptor[] getPropertyDescriptors(); MethodDescriptor[] getMethodDescriptors(); EventSetDescriptor[] getEventSetDescriptors(); Creating a BeanInfo class (1) 1. Name your BeanInfo class Target class: ExplicitButton Bean information class: ExplicitButtonBeanInfo 2. Subclass SimpleBeanInfo public class ExplicitButtonBeanInfo extends SimpleBeanInfo Creating a BeanInfo class (2) 3. Override methods public PropertyDescriptor[] getPropertyDescriptors() { try { PropertyDescriptor background = new PropertyDescriptor("background", beanClass); PropertyDescriptor foreground = new PropertyDescriptor("foreground", beanClass); PropertyDescriptor font = new PropertyDescriptor("font", beanClass); PropertyDescriptor label = new PropertyDescriptor("label", beanClass); } background.setBound(true); foreground.setBound(true); font.setBound(true); label.setBound(true); PropertyDescriptor rv[] = {background, foreground, font, label}; return rv; } catch (IntrospectionException e) { throw new Error(e.toString()); } Creating a BeanInfo class (3) 4. Specify the target Bean class public BeanDescriptor getBeanDescriptor() { return new BeanDescriptor(beanClass); } ... private final static Class beanClass = ExplicitButton.class Bean Customization A Bean's appearance and behavior can be customized at design time: 1. By using a property editor: Property sheet Associated with a property 2. By using customizers: Complete GUI control Associated with a bean Property Editors Explicit association via a BeanInfo object using the method setPropertyEditorClass(…) Explicit registration via java.Beans.PropertyEditorManager.registerE ditor Naming convention Customizers Extend java.awt.Component or one of its subclasses. Implement the java.beans.Customizer interface This means implementing methods to register PropertyChangeListener objects, and firing property change events at those listeners when a change to the target Bean has occurred. Implement a default constructor. Associate the customizer with its target class via BeanInfo.getBeanDescriptor. International Beans Programs are written with one language and culture What about localization? Locale is a geographic, political, or culturally cohesive region sharing the same language and customs Localizing objects: Use Java’s locale class Encapsulate locale-specific data in resource bundles Locale localeUS = new Locale(“en”, “US”); public Locale getLocale( ) References http://java.sun.com/docs/books/tutorial/javabeans/ JavaBeans Unleased, Dr. Don Doherty, Rick Leinecker ftp://ftp.javasoft.com/docs/beans/beans.101.pdf JavaBeans 1.01 Specification Beginning Java 2, Ivor Horton Discussion When should the reflection API not be used? When to use BeanInfo and when to use low-level Reflection API? What kind of applications might want to use the Reflection API?