|
Peeking Inside the Box: Attribute-Oriented Programming with Java 1.5, Part 1作者:Don Schwarz(1)
Peeking Inside the Box: Attribute-Oriented Programming with Java 1.5 I recently spent an afternoon at the London Science Museum watching steam engines, pumps, turbines, and other antique machines operate. Some were so simple that their behavior was obvious, but most had sections cut away and replaced with glass windows so their inner workings could be observed. As I walked through the exhibits, I couldn't help but wonder how the craftsmen and maintenance crews of those machines were able to do their jobs without the cut-away models and diagrams that accompanied the exhibits here. Software engineers have it easy! To a seasoned Java developer with Access to source code, any program can look like the transparent models at the museum. Tools sUCh as thread dumps, method call tracing, breakpoints, and profiling statistiCS can provide insight into what a program is doing right now, has done in the past, and may do in the future. But in a prodUCtion environment, things aren't always so obvious. These tools are not typically available, or at the very least, they are usable only by trained developers. Support teams and end users also need to be aware of what an application is doing at any given time. To fill this void, we've invented simpler alternatives sUCh as log files (typically for server processes) and status bars (for GUI applications). However, because these tools must capture and report only a very small subset of the information available to them and often must present this information in a way that is easy to understand, they tend to be programmed eXPlicitly into the application. This code is intertwined with the application's business logic in sUCh a way that developers must "work around it" when trying to debug or understand core functionality, yet remember to update this code whenever that functionality changes. What we really want to do is to centralize the status-reporting logic in a single place, and manage the individual status messages as metadata. In this article, I will consider the case of a status-bar component embedded in a GUI application. I will eXPlore a number of different ways to implement this status reporter, starting with the traditional hard-coded idiom. Along the way, I will introdUCe and discuss a number of new features in Java 1.5, including annotations and run-time bytecode instrumentation. The StatusManagerMy primary goal is to create a JStatusBar Swing component that can be embedded in our GUI application. Figure 1 shows what the finished status bar looks like in a simple JFrame. Java.com/onJava/2004/06/30/graphiCS/statusbar.png" width=258>
|