Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
doesNotUnderstand message, which enables an object to trap a request it doesn't know about. This can't happen in Java directly, because all
messages are statically type-checked at compile time -- so it's never possible to send a message directly to an object that
Java doesn't understand.However, with the introduction of Java 1.1, Sun released the java.lang.reflect API. It allows an object to inspect itself to find out what methods it supports, and to dynamically invoke them. The API
elevates Java from being a dynamic runtime environment to being a truly dynamic language.
This Java Tip shows you how to use the Reflection API, and specifically how to use it to implement a simple system for sending
dynamic messages. In our particular case, this simple system is the doesNotUnderstand message.
In order to understand how to implement dynamic messaging, it is necessary to first examine the java.lang.Reflection API.
In the world of programming, reflection is a process that allows an object to look at itself (as in a mirror, ergo the term) to see what it can do. Reflection identifies all of the members that are associated with an object (field members and method members) and makes it possible for that object to interact with them.
One question that is asked often is, What's the difference between introspection and reflection? The answer: Reflection allows
you to find out which members an object has; and introspection allows you to identify which (JavaBean) properties an object
has. That's why the java.lang.reflect lives in a separate package, and the Introspector object is in the java.beans package. Links to these packages on Sun's Java site, as well as further information on JavaBeans, are available in the Resources section below.
The java.lang.reflect package defines wrapper objects for manipulating arrays, methods, and fields. However, you can't create objects like Method, Field, or Constructor directly; you have to invoke a factory method in the Class object (getMethod, getConstructor, and so on).
To give you an example of reflection at work, this sample piece of code, DisplayMembers.java, takes a Java classname as its argument, and then prints out all the members it contains.
The next piece of code, listed below, takes a fully qualified classname as its argument(s), and for each class listed prints out all of the methods that are declared in that class. For instance, running:
java DisplayMembers DisplayMembers
lists the main and displayClassInfo methods along with their types and exceptions. (I have been purposefully lazy here by declaring that the main method generates an exception; this saves having to type in a try/catch block, but results in ugly code. Don't use this style unless you want to write compact demo code.)
Once you've got a list of the methods an object has, you can invoke them dynamically. This allows you to select a method dynamically (based on its name and argument types) and invoke it against a given object. The next example demonstrates how an object can be instantiated and given a method name, and how it can take optional arguments.
java.lang.reflect package http://www.javasoft.com/products/jdk/1.1/docs/api/Package-java.lang.reflect.html
java.lang.beans package http://www.javasoft.com/products/jdk/1.1/docs/api/Package-java.beans.html