1 / 17

Introspection thro’ Reflection

Introspection thro’ Reflection. Reflection: Ability of the program to Analyze itself Java.lang.reflect package Can obtain information about the Fields Constructor Method Modifiers of the class With this information the class can be instantiated and the methods can be invoked dynamically.

shaun
Download Presentation

Introspection thro’ Reflection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introspection thro’ Reflection Reflection: • Ability of the program to Analyze itself • Java.lang.reflect package • Can obtain information about the • Fields • Constructor • Method • Modifiers of the class • With this information the class can be instantiated and the methods can be invoked dynamically

  2. Introspection • Why do we need it? • When beans are used in a IDE (Integrated Development Environment), its properties and methods are need to be exposed so that builder tool and the programmer can control the appearance and the behavior of the bean. • Design Pattern provides all the information • For accessing Read/write property we use getPropertyName() and setPropertyName() methods • Reflection mechanism in java will analyze and provide the information that a property “PropertName”.

  3. How introspection Works? • Provide information about the bean explicitly • Self-description provided by the bean. • Class implements the java.bean.BeanInfo interface • Which in turn specifies a set of function that are used to collect information about the bean. • Reflection – Analyze the class itself • Introspection – uses the meta-object(BeanInfo)

  4. BeanInfo • Follows a special design pattern • Always the Bean class which is having the BeanInfo object should have suffix ‘BeanInfo’ • BeanInfo can be accessed and modified using getBeanInfoSearchPath() and setBeanInfoSearchPath()

  5. java.beans.BeanInfo Methods Int Constant • getIcon(int) • Returns an image to be used to represent the bean in toolboxes, toolbars, etc. FeatureDescriptor • getBeanDescriptor() • Returns overall information about the bean, such as its displayName, • getMethodDescriptors() • Returns a list of methods supported by the bean with a description of each method • getPropertyDescriptors() • Returns a list of properties supported by the bean with a description of each property

  6. getEventSetDescriptors() • Returns a list of events fired by the bean with a description of each event • getAdditionalBeanInfo() • This method allows a BeanInfo object to return an arbitrary collection of other BeanInfo objects that provide additional information on the current bean • getDefaultEventIndex() • A bean may have a "default" event that is the event that will mostly commonly be used by human's when using the bean • getDefaultPropertyIndex() • A bean may have a "default" property that is the property that will mostly commonly be initially chosen for update by human's who are customizing the bean

  7. getIcon import java.beans.*; public class IntroBeanInfo extends SimpleBeanInfo { public java.awt.Image getIcon(int iconType) { if(iconType ==BeanInfo.ICON_COLOR_16x16) { System.err.println("True"); java.awt.Image img = loadImage("jugg.gif"); return img; } return null; } }

  8. There are four types of icon supported: • ICON_MONO_16x16 • ICON_COLOR_16x16 • ICON_MONO_32x32 • ICON_COLOR_32x32 • These constants are defined in java.beans.BeanInfo • The sizes refer to the pixel size of the imagesloadImage is defined in java.beans. SimpleBeanInfopublic Image loadImage(String resourceName) • resourceName - A pathname relative to the directory holding the class file of the current class. For example, "wombat.gif".

  9. java.beans.FeatureDescriptor • Most of the methods in java.beans.BeanInfo return descriptors • java.beans.FeatureDescriptor is the parent class for all bean descriptorssetDisplayName(String) getDisplayName() • The name displayed in builder tool for this item • setName(String) getName() • The programmatic name of the property/method/event • setShortDescription(String) getShortDescription() • You can associate a short descriptive string with a feature

  10. attributeNames() setValue(String, Object) getValue(String) • Supports name/value pairs with this feature • setExpert(boolean) isExpert() • Distinguishes between features for expert users from those for normal users • setHidden(boolean) isHidden() • Identifies features that are intended only for tool use, and which should not be exposed to humans

  11. getBeanDescriptor public BeanDescriptor getBeanDescriptor() { BeanDescriptor bd = new BeanDescriptor( Intro.class ); bd.setDisplayName( "Pink"); return bd; }

  12. PropertyDescriptor • public PropertyDescriptor(String propertyName, Method getter, Method setter) throws IntrospectionException • propertyName • The programmatic name of the property • getter • The method used for reading the property value. Set null if the property is write-only • setter • The method used for writing the property value. Set null if the property is read-only

  13. public PropertyDescriptor[] getPropertyDescriptors() { try { PropertyDescriptor nameProperty = new PropertyDescriptor( "color", Intro.class ); nameProperty.setDisplayName( "Color"); nameProperty.setShortDescription( "Color of the box"); PropertyDescriptor[] allProperties = { nameProperty }; return allProperties; } catch ( IntrospectionException error ) { return super.getPropertyDescriptors(); // use default if there is an error } }

  14. Event Descriptos • Expose the events that are fired by the bean • Implement the method ‘getEventSetDescriptors()’ Public EventSetDescriptor(Class sourceClass, String eventSetName, String listenerMethodName)throws IntrospectionException

  15. Customization • A bean can be customized for enhancing its funtionality • Through Property Editors • Through Customizer Classes

  16. Property Editors • Allows the user to read and change the value of the property • To create aProperty Editor we need the PropertyEditor interface and the PropertyEditorSupport class • PropertyEditor  provides support for GUIs that permit users to edit property value • setAsText and getAsText are overridden

  17. Cutomizer Class • Implementing the java.beans.Customizer interface • setObject of the class that implements customizer are overridden.

More Related