Java Reference
In-Depth Information
*/
protected
protected
void
void
startClass
(
Class
<?>
c
) {
println
(
"<class><classname>"
+
c
.
getName
() +
"</classname>"
);
}
protected
protected
void
void
putField
(
Field fld
,
Class
<?>
c
) {
println
(
"<field>"
+
fld
+
"</field>"
);
}
/** put a Method's information to the standard output.
* Marked protected so you can override it (hint, hint).
*/
protected
protected
void
void
putMethod
(
Method method
,
Class
<?>
c
) {
println
(
"<method>"
+
method
+
"</method>"
);
}
/** Print the end of a class.
*/
protected
protected
void
void
endClass
() {
println
(
"</class>"
);
}
}
By the way, if you publish a book using either of these and get rich, “Remember, remember
me!”
Program: AppletViewer
Though I don't say much about applets in this edition of this topic, another JDK tool that can
be replicated is the
AppletViewer
. This uses the reflection package to load a class that is sub-
classed from
Applet
, instantiate an instance of it, and
add()
this to a frame at a given size.
Writing a replacement version of such a tool is a good example of reflection in action: you
can use these techniques to dynamically load any subclass of a given class. Suppose we have
Example 23-23. HelloApplet.java
public
public class
class
HelloApplet
HelloApplet
extends
extends
JApplet
{
/** The flag which controls drawing the message. */
protected
protected
boolean
boolean
requested
;
/** init() is an Applet method called by the browser to initialize */