This sample is located in \Samples\Wfc\ClassDecoder.
Description
Using the Sample
Key Project Files
Technologies Demonstrated
The class file decoder reads Java class files and decodes the bytes into the attributes, structures, and properties that make up the class. This application consists of the sample.decoder package that contains all the objects required to read class files from the class path and decode them into their appropriate structures.
Decoding of the class files starts with the ClassObject class. This class has one constructor that takes the file name of the class to load and decode, and one that takes a byte array of the byte codes that make up a particular class.
The ClassObject decodes all the top-level properties of a class file, such as the magic number, the number of methods, the number of fields, the superclass index, the number of class attributes, and so on. For decoding subobjects, such as attributes, methods, fields, or constants, the appropriate subobject decoder classes are used. These classes are FieldObject, MethodObject, ConstantObject, or AttributeObject. Decoding methods and fields is fairly straightforward; however, there are many possibilities for the ConstantObject and AttributeObjects. All specific ConstantObjects, (such as Utf8, Value, and Integer) subclass ConstantObject, and all specific instances of attributes (such as Code, ConstantValue, and InnerClasses) subclass AttributeObject. ClassObject calls a generic method in these subobject decoder classes that returns a java.util.Vector of all decoded objects of that type.
The Java Virtual Machine Specification by Addison Wesley allows for new attributes to be added by any developer, and specifies that the virtual machine simply ignore any that it doesn't understand. There will be many new attributes found while decoding classes. To allow application decoding to function with unknown attributes, a mechanism using reflection loads the appropriate decoding class for a specific attribute. If the specific attribute decoding class cannot be located in the class path, a generic AttributeObject decoding will take place.
To add support for a new attribute, simply add a new class to the sample.decoder package called <new attribute name>Attribute.java that performs the specific decoding operations.
All objects used in the decoding process subclass DecodeObject, which contains common methods for storing the properties of the decoded structure. Each of the subclasses of DecodeObject implements the DecodeDisplayInterface, which defines some standard methods for retrieving information about the decoded structure for display purposes. The class sample.decoder.Decode contains some static methods that can help in conversions and in decoding.
To compile the sample
Use Nmake.exe to compile the makefile in the \Samples\Wfc\ClassDecoder directory. This compiles the class files and creates the signed cabinet for the applet version.
To install the sample
Add the Classviewer.cab and the Decode.htm files to a Web site.
To run the sample
sample.viewer Package
This package contains a WFC-based application that uses the Sample.decoder packages to decode class files from the class path or from a file. The class file subcomponents are displayed in a tree control, with detailed information (Description, Offset, Length, Value, and Bytes) related to each tree node displayed in the List View Control. A RichEdit20 control displays the source code for the class, fields, and methods (method code is node decoded at this time). Selecting the blue hyperlink class names in the source view with the right mouse button causes that class to be decoded (if it is available in the class path). The buttons in the toolbar can be used to filter out subcomponents from the display, or the filter menu can be used for this purpose.
sample.appletViewer Package
This package contains an Application Foundation Class (AFC)-based viewer that loads a specified class file, decodes it into its subcomponents, and displays them in a tree control. Detailed information on the properties of each component are displayed in the table when the appropriate tree item is selected. The class can be used either as an application from the command line, or from a browser using the Decode.htm file. Because the application is packaged in a signed cabinet file, the applet can read local Java class files when running from a browser.
sample.decoder.*
Contains all the worker classes that decode byte codes into a displayable form.
This sample shows how to: