BeanLint, which analyzes class files and reports these potential problems. (To get the most out of what's being discussed here, you
should read last month's column first; it describes some potential problems JavaBeans might have, and begins to describe how
BeanLint works.)BeanLint is interesting to JavaBeans developers for two reasons. First, it's obviously preferable to find out about bugs in your software
during development or testing, rather than from customer service. The second reason this topic is interesting is that it uses
several new and/or advanced features of the Java platform, and, as such, provides good working examples of how to use those
features.
Last month's topics included examples and a discussion of how to use package java.util.zip to read the contents of a jar file, and it covered the creation of a custom ClassLoader class to convert the jar file contents into usable Java classes. This month, we'll see how BeanLint uses the package java.lang.reflection (a relatively new package introduced in Java 1.1) to analyze class files and look for potential problems.
We'll have an in-depth look at a potential pitfall in Java's object serialization, and go over the solution BeanLint offers. Finally, we'll show how to use the package java.beans.Introspector (part of the standard Java package java.beans) to produce a report of all of the properties, event sets, and other JavaBeans-related attributes of any class file that
passes all the tests. But before we start, I'd like to respond to some reader mail.
Several people have said they like the way I've been using scrollable text areas to show code examples in a compact way. Others say they don't like it because the articles, when printed, don't show all the code. Some have made both observations. One of my mottos is, Why choose when you can have both? So, I've continued to put the code in the scrolling text boxes, but any classes or programs I've written will also be accompanied by a link to a printable form of the source. Please write and let me know if this solution doesn't address your concerns.
BeanLint's basic approach to finding potential problems in a class file is to load the file as a class, and then perform a series
of checks on the class that look for these problems. Last month, we looked at how to get at the bytes that represent a class
file, and then how to convert that byte stream into a class. Now, we'll get into the nitty-gritty of detecting potential problems
in our beans. Several of these potential bean flaws have to do with properties of the class methods. The list below covers
potential bean problems that can be detected by analyzing a class's methods, and describes how to detect that problem.
java.io.Serializable can be found at http://java.sun.com/products/jdk/1.1/docs/api/java.io.Serializable.html.