Download presentation
Presentation is loading. Please wait.
Published byMarilyn Higgins Modified over 9 years ago
1
JavaBeans: Introspection and Customization Umer Farooq CS6704: Design Patterns & Component FrameworksJanuary 30, 2002
2
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?
3
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.
4
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
5
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.
6
BeanInfo interface BeanInfo defines methods that return descriptors for each property, method, or event that you want exposed: PropertyDescriptor[] getPropertyDescriptors(); MethodDescriptor[] getMethodDescriptors(); EventSetDescriptor[] getEventSetDescriptors();
7
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
8
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 (2)
9
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
10
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 Bean Customization
11
Property Editors Explicit association via a BeanInfo object using the method setPropertyEditorClass(…) Explicit registration via java.Beans.PropertyEditorManager.registerE ditor Naming convention
12
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.
13
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( )
14
References http://java.sun.com/docs/books/tutorial/javabeans/ 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
15
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? Discussion
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.