|
Peeking Inside the Box: Attribute-Oriented Programming with Java 1.5, Part 2 作者:Don Schwarz(1)
Peeking Inside the Box: Attribute-Oriented Programming with Java 1.5 In the previous article in this series, "Java.com/pub/a/onJava/2004/06/30/insidebox1.html" lid="Peeking Inside the Box, Part 1">Peeking Inside the Box, Part 1," I introdUCed the concepts of Attribute-Oriented Programming, Java 1.5 annotations, and bytecode instrumentation. I used these concepts to provide a JStatusBar GUI component that reports on the progress of an application without any eXPlicit code. In this article I will introdUCe a completely different implementation of the same JStatusBar that uses thread sampling rather than bytecode instrumentation. I will then combine the two practices to develop a solution that has the best features of each. In the previous article, I also defined a new annotation, @Status, which I used throughout my code to associate methods with user-readable status messages. For example: @Status("Connecting to database")public void connectToDB (String url) { ...}
Exception HandlingAs discussed in the previous article, I may want to write additional code which uses the @Status annotations for a different purpose. Let's consider the additional requirement that our application must catch all unhandled exceptions and display them to the user. Rather than providing a Java stack trace, however, it should only display methods that have a @Status annotation, and it should not show any code artifacts (class or method names, line numbers, etc.). For example, consider the following stack trace: Java.lang.RuntimeException: Could not load data for symbol IBM at boXPeeking.code.YourCode.loadData(Unknown Source) at boXPeeking.code.YourCode.go(Unknown Source) at boXPeeking.yourcode.ui.Main+2.run(Unknown Source) at Java.lang.Thread.run(Thread.Java:566)Caused by: Java.lang.RuntimeException: Timed out at boXPeeking.code.YourCode.connectToDB(Unknown Source) ... 4 more
This should result in the GUI pop-up box in Figure 1, assuming that YourCode.loadData(), YourCode.go(), and YourCode.connectToDB() all have @Status annotations. Note that the order of the exceptions is reversed so that the user is given the most detailed information first. Java.com/onJava/2004/07/21/graphiCS/exception.png" width=268>
|