Extraction of BeanInfo with Introspector

Source: Internet
Author: User
Tags getcolor tostring uppercase letter

When we drag and drop a bean's palette and put it into a form, the rules for the most critical part of a bean occur. The application builder must be able to create a bean (which it can do if it is the default builder) and then access the bean's source code outside this scope, extracting all the necessary information to create the property sheet and event handler. A part of the
solution is already visible at the end of Chapter 11: The image of the Java 1.1 version allows all methods of an anonymous class to be discovered. This solves the bean puzzle perfectly without having to use some special language keywords as needed in other visual programming languages. In fact, one of the main reasons is that the image is added to Java version 1.1 to support beans (although the image also supports object concatenation and remote method invocation). Because we might want the developers of the application builder to have to image each bean and search through their methods to find the bean's properties and events.
This is certainly possible, but Java researchers want to provide a standard interface for each user who uses it, not just to make the beans easier to use, but they also provide a standard way to create more complex beans. This interface is the Introspector class, the most important method in this class, the static Getbeaninfo (). We deal with this method through a class and getbeaninfo the () method to query the class comprehensively, returning a BeanInfo object that we can conduct a detailed study to discover its properties, methods, and events.
Usually we don't pay attention to things like this--we might use most of our ready-made beans, and we don't need to know all the technical details that run at the bottom. We simply drag and drop our beans into our form, then configure their properties and write the processor for the event. Anyway, it's an interesting and instructive exercise to use Introspector to display information about beans, okay, gossip less, here's a tool please run it (we can find it in the Forgbean subdirectory):
 

: Beandumper.java//A method to introspect a Bean import java.beans.*;

Import java.lang.reflect.*;
    public class Beandumper {public static void Dump (class bean) {BeanInfo bi = null;
    try {bi = introspector.getbeaninfo (bean, java.lang.Object.class);
      catch (Introspectionexception ex) {System.out.println ("couldn ' t introspect" + bean.getname ());
    System.exit (1);
    } propertydescriptor[] Properties = bi.getpropertydescriptors ();
      for (int i = 0; i < properties.length i++) {Class p = properties[i].getpropertytype ();
      System.out.println ("Property type:\n" + P.getname ());
      System.out.println ("Property name:\n" + Properties[i].getname ());
      Method Readmethod = Properties[i].getreadmethod ();
      if (Readmethod!= null) System.out.println ("Read method:\n" + readmethod.tostring ()); Method Writemethod = Properties[i].Getwritemethod ();
      if (Writemethod!= null) System.out.println ("Write method:\n" + writemethod.tostring ());
    System.out.println ("====================");
    } System.out.println ("Public Methods:");
    Methoddescriptor[] methods = Bi.getmethoddescriptors ();
    for (int i = 0; i < methods.length i++) System.out.println (Methods[i].getmethod (). toString ());
    System.out.println ("======================");
    SYSTEM.OUT.PRINTLN ("Event support:");
    Eventsetdescriptor[] Events = Bi.geteventsetdescriptors (); for (int i = 0; i < events.length i++) {System.out.println ("Listener type:\n" + Events[i].getlistenert
      Ype (). GetName ());
      method[] lm = Events[i].getlistenermethods (); for (int j = 0; J < Lm.length J + +) System.out.println ("Listener method:\n" + Lm[j].getna
      Me ()); methoddescriptor[] LMD = EVENTS[I].GETLISTENERMETHODDESCRIPtors (); for (int j = 0; J < Lmd.length J + +) System.out.println ("Method descriptor:\n" + LMD[J].G
      Etmethod (). toString ());
      Method AddListener = Events[i].getaddlistenermethod ();
      System.out.println ("Add Listener method:\n" + addlistener.tostring ());
      Method RemoveListener = Events[i].getremovelistenermethod ();
      System.out.println ("Remove Listener method:\n" + removelistener.tostring ());
    System.out.println ("===================="); }//Dump the class of your choice:public static void main (string[] args) {if (Args.length < 1) {Sy
      Stem.err.println ("Usage: \ n" + "Beandumper fully.qualified.class");
    System.exit (0);
    Class c = null;
    try {c = class.forname (Args[0]);
      catch (ClassNotFoundException ex) {System.err.println ("couldn ' t find" + args[0]);
    System.exit (0);
  Dump (c); }
} ///:~ 

The


Beandumper.dump () is a way to do any work. First it attempts to create a BeanInfo object that, if successfully invoked, generates information about the properties, methods, and events of the BeanInfo. In Introspector.getbeaninfo (), we notice that there is an additional argument. It informs the introspector of the location of the inheritance system. In this case, it stops before parsing all the object methods, because we're not interested in seeing those.
Getpropertydescriptors () returns a set of property descriptors because of the property. For each descriptor we can call the Getpropertytype () method to discover the object of the class completely through the property method. At this point, we can use the GetName () method to get the kana (extracted from the method name) of each property, and the GetName () method completes the read and write operations with Getreadmethod () and Getwritemethod (). The last two methods return one that can be really invoked to invoke the corresponding method object on the object (which is part of the image). For public methods (including property methods), Getmethoddescriptors () returns a set of method descriptor characters. Every one of us can get a pretty good method object and can display their names.
for events, Geteventsetdescriptors () returns a set of event descriptor characters. Each of them can be queried to find the class of the receiver, the method of the receiver class, and the method of adding and removing the receiver. The Beandumper program prints out all of this information.
If we call Beandumper in the Frog class, like this:
Java beandumper Frogbean. Frog
Its output is as follows (removed additional details not required here):
 

Class Name:frog Property Type:color property Name:color Read method:public Color getcolor () Write method:pub LIC void SetColor (Color) ==================== Property Type:spots Property name:spots Read method:public spots ge 
Tspots () Write method:public void setspots (spots) ==================== Property Type:boolean Property Name:jumper Read Method:public boolean isjumper () Write method:public void Setjumper (Boolean) ==================== property Typ E:int Property name:jumps Read method:public int getjumps () Write method:public void setjumps (int) =========== ========= public methods:public void setjumps (int) public void croak () public void Removeactionlistener (ActionListener) p ublic void addActionListener (actionlistener) public int getjumps () public void SetColor (Color) public void setspots (spots 
Public-void Setjumper (Boolean) public boolean isjumper () public void Addkeylistener (keylistener) public Color GetColor () public void RemovekeylisTener (keylistener) public spots getspots () ====================== Event Support:listener Type:keylistener-Met hod:keytyped Listener method:keypressed Listener method:keyreleased method descriptor:public void keytyped (Key Event) method descriptor:public void Keypressed (keyevent) method descriptor:public void keyreleased (keyevent) Add Li Stener method:public void Addkeylistener (KeyListener) Remove Listener method:public void Removekeylistener (Keylisten ER) ==================== Listener type:actionlistener Listener method:actionperformed method Descriptor:public vo ID actionperformed (actionevent) Add Listener method:public void addActionListener (ActionListener) Remove Listener d:public void Removeactionlistener (ActionListener) ====================


This result reveals much of what Introspector sees when it produces a BeanInfo object from our bean. We can note that the types of attributes and their names are independent of each other. Note the Lowercase property name. (When the property name begins with more than one uppercase letter in a row, the program is not executed.) And remember that the name of the method we see here (for example, read and method) is really generated from a method object that can be used to invoke related methods in an object.
The list of common methods contains unrelated events or attributes, such as croak (). All the methods in the list are that we can have a planned call for the bean, and the Application Builder can choose to list all of the methods and mitigate our tasks when we call the methods.
Finally, we can see that the event completely analyzes its methods in the receiver, adding and reducing the receiver. Basically, once we have beaninfo, we can find anything important to the bean. We can also invoke methods for the bean, even if we don't have any other information except the object (in addition, this is the image feature).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.