So for a new EJB, you need three Java classes, each of which must adhere to the EJB specification. For just one new EJB, creating three classes -- all EJB compliant -- might not be so bad. But if you're creating lots of EJBs -- perhaps from functionality you've already coded into existing classes -- a more automated solution would be beneficial. |
Anonymous classes Supporting anonymous classes is hard to explain and even harder to design for in a program. The challenge of supporting an anonymous class can be stated like this: "Write a program that, when given a Java object, can incorporate that object into its continuing operation." The general solution is rather difficult, but by constraining the problem, some specialized solutions can be created. There are two examples of specialized solutions to this class of problem in the 1.0 version of Java: Java applets and the command-line version of the Java interpreter. |
Together with listing the contents of an arbitrary class, this article will illustrate how reflection can be used to leverage programming and push it to a higher level of abstraction. We will start from very basic examples and move forth by applying reflection within a simple application. |
Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them. |
Performance hit from reflection doesn't matter here because the network latency is much bigger. But I would have gone for an interface or class name that has a method in it that has defined semantics. |
Thanks to the Reflection API, you can. Using the Reflection API, you can run methods dynamically. Java's support of reflection enables you to create an interpreter that executes commands interactively. This article describes a system you can use to build an interpreter into your application. It demonstrates how to build a reflection-based system that allows simple scripting of Java programs without having to install a special-purpose scripting language. It also provides a downloadable sample program. This solution can allow you to create simple control scripts for your Java programs. |
Java Version 1.1 or later allows a Java program to discover information about the fields, methods, and constructors of loaded classes. Also, a Java program can instantiate a class and operate on the underlying objects, within security restrictions. |
|
In "Java programming dynamics, Part 1," I gave you an introduction to Java programming classes and class loading. That article described some of the extensive information present in the Java binary class format. This month I cover the basics of using the Java Reflection API to access and use some of that same information at run time. To help keep things interesting even for developers who already know the basics of reflection, I'm including a look at how reflection performance compares with direct access. |
Figure 1 shows the results of this timing test when called with loop counts ranging from 2K to 512K (tests run on an Athlon 2200+ XP system running Mandrake Linux 9.1, using Sun's 1.4.2 JVM). Here I've included both the reflection and generated code times for the second property within each test run (so the pair of times when using the Javassist code generation are first, followed by the same pair of times when using the BCEL code generation). The execution times are about the same regardless of whether Javassist or BCEL is used to generate the glue classes, which is what I'd expect to see -- but it's always good to have confirmation! |
Now that I've covered the basics of Java classes in Part 1, and the principles of the Java Reflection API in Part 2 and Part 3, the rest of this series is going to head off along the less-traveled path of bytecode manipulation. I'll start off easy in Part 4 with a look at the user-friendly Javassist library for working with binary classes. Do you want to try transforming methods, but are reluctant to start programming in bytecode? Javassist may be just the tool to suit your needs. Find out how next month. |
Consider listing 2, which shows a fragment of java.util.Vector. One method has an interface type as a parameter, another an array, and the third a primitive. To program effectively with reflection, you must know how to introspect on classes such as Vector that have methods with such parameters. |
The getClass method is used to access an object's class at runtime. The getClass method is often used to begin reflective programming because many reflective tasks require objects representing classes. The getClass method is introduced by java.lang.Object, so any object in Java can be queried for its class1. |
Java classes are the main building blocks of a Java application. As developers, we write some classes, but we also use classes developed by others. We rely on the API listings and documentation to learn how to use these "third-party" classes effectively. That works well (at least as well as the quality of the documentation!) in development stages. Java also provides a framework for learning about classes during runtime. That framework is called Reflection and is wrapped as the java.lang.reflect package. |
The JavaBeans framework makes use of this reflection mechanism to actually plug in a bean to a container of beans or a beanbox. |