⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 14_2.htm

📁 翁剀JAVA语言那门课程的教案 很多人都看多他的视频教程可惜没有ppt的教案
💻 HTM
字号:
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Weng Kai">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.01 [en] (Win95; I) [Netscape]">
   <TITLE>Extracting BeanInfo with the Introspector</TITLE>
</HEAD>
<BODY>

<H2>
14.2 Extracting BeanInfo with the Introspector</H2>

<HR WIDTH="100%">
<BR>One of the most critical parts of the Bean scheme occurs when you drag
a Bean off a palette and plop it down on a form. The application builder
tool must be able to create the Bean (which it can do if there's a default
constructor) and then, without access to the Bean's source code, extract
all the necessary information to create the property sheet and event handlers.

<P>Part of the solution is already evident: Java 1.1 reflection allows
all the methods of an anonymous class to be discovered. This is perfect
for solving the Bean problem without requiring you to use any extra language
keywords like those required in other visual programming languages. In
fact, one of the prime reasons that reflection was added to Java 1.1 was
to support Beans (although reflection also supports object serialization
and remote method invocation). So you might expect that the creator of
the application builder tool would have to reflect each Bean and hunt through
its methods to find the properties and events for that Bean.

<P>This is certainly possible, but the Java designers wanted to provide
a standard interface for everyone to use, not only to make Beans simpler
to use but also to provide a standard gateway to the creation of more complex
Beans. This interface is the Introspector class, and the most important
method in this class is the static getBeanInfo( ). You pass a Class handle
to this method and it fully interrogates that class and returns a BeanInfo
object that you can then dissect to find properties, methods, and events.

<P>Usually you won't care about any of this -- you'll probably get most
of your Beans off the shelf from vendors, and you don't need to know all
the magic that's going on underneath. You'll simply drag your Beans onto
your form, then configure their properties and write handlers for the events
you're interested in. However, it's an interesting and educational exercise
to use the Introspector to display information about a Bean, so here's
a tool that does it:&nbsp; <A HREF="case/BeanDumper.java">BeanDumper.java</A>

<P>BeanDumper.dump( ) is the method that does all the work. First it tries
to create a BeanInfo object, and if successful calls the methods of BeanInfo
that produce information about properties, methods, and events. In Introspector.getBeanInfo(
), you'll see there is a second argument. This tells the Introspector where
to stop in the inheritance hierarchy. Here, it stops before it parses all
the methods from Object, since we're not interested in seeing those.

<P>For properties, getPropertyDescriptors( ) returns an array of PropertyDescriptors.
For each PropertyDescriptor you can call getPropertyType( ) to find the
class of object that is passed in and out via the property methods. Then,
for each property you can get its pseudonym (extracted from the method
names) with getName( ), the method for reading with getReadMethod( ), and
the method for writing with getWriteMethod( ). These last two methods return
a Method object that can actually be used to invoke the corresponding method
on the object (this is part of reflection).

<P>For the public methods (including the property methods), getMethodDescriptors(
) returns an array of MethodDescriptors. For each one you can get the associated
Method object and print out its name.

<P>For the events, getEventSetDescriptors( ) returns an array of (what
else?) EventSetDescriptors. Each of these can be queried to find out the
class of the listener, the methods of that listener class, and the add-
and remove-listener methods. The BeanDumper program prints out all of this
information.

<P>This reveals most of what the Introspector sees as it produces a BeanInfo
object from your Bean. You can see that the type of the property and its
name are independent. Notice the lowercasing of the property name. (The
only time this doesn't occur is when the property name begins with more
than one capital letter in a row.) And remember that the method names you're
seeing here (such as the read and write methods) are actually produced
from a Method object that can be used to invoke the associated method on
the object.

<P>The public method list includes the methods that are not associated
with a property or event, such as croak( ), as well as those that are.
These are all the methods that you can call programmatically for a Bean,
and the application builder tool can choose to list all of these while
you're making method calls, to ease your task.

<P>Finally, you can see that the events are fully parsed out into the listener,
its methods, and the add- and remove-listener methods. Basically, once
you have the BeanInfo, you can find out everything of importance for the
Bean. You can also call the methods for that Bean, even though you don't
have any other information except the object (again, a feature of reflection).
<BR>
<HR WIDTH="100%">
<DIV ALIGN=right><A HREF="14_3.htm">Next Page</A></DIV>

</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -