Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Decompilers and Other Tools

With the tools developed in the last few chapters, you should be able to make sense out of almost any Java .class file you come across. This is an immensely useful debugging skill, especially when you’re using third-party software for which you do not have source code. If you’re uncertain whether a bug is in your code, in the vendor’s code, or in your understanding of what the vendor’s library does, you can disassemble the relevant files and find out. However, some readers may wonder if it’s possible to go one step further. Can you move from a .class file to actual .java source code?

In general the answer is no, you cannot. Some information that is contained in .java source files, notably the names of local variables and comments, is simply not present anywhere in a .class file. Nonetheless, it’s often possible to fill in the blanks and recognize certain common patterns. Throughout this chapter, you’ve seen how different constructs tend to compile. For example, when you see a tableswitch or lookupswitch statement in disassembled byte code, you know that there’s a switch statement in the source code. With experience and practice, it’s not very hard to manually rewrite disassembled byte code as .java source code. If you don’t understand a particular method, it’s often helpful to start rewriting it as .java source code. What the method is doing will often become apparent as you translate it.

Algorithmically converting byte codes to source code is more difficult. There is at least one free tool to help you though. HanPeter van Vliet’s Mocha is a free decompiler for Java. It can normally back out .java source code from most .class files. At least in the version that was available at the time I was writing this, Mocha does choke on some files. In those cases, you have to drop back to disassembly.

Unfortunately, as this book was going to press, the Mocha Web page disappeared from the Net. Unconfirmed Usenet gossip has it that HanPeter van Vliet died of cancer in late 1996. At this point, the future of Mocha is uncertain, but more unconfirmed Usenet gossip says that it may reappear as part of an unreleased Borland product. Several other people have placed Mocha on their Web sites, but I’m hesitant to include URLs that seem to change monthly in a book that takes longer than that just to get from warehouse to bookstore. I suggest doing a search on “Mocha” and “HanPeter van Vliet” at your favorite Web search engine to try to track it down.

Mocha is quite simple to use. Just type java Mocha.decompiler filename.class on the command line. Mocha then analyzes the file and puts its best guess as to the source code in a file called filename.mocha. For example:

     % java mocha.Decompiler HelloWorld.class

Here’s the result when Mocha was used to decompile the HelloWorld program:

     /* Decompiled by Mocha from HelloWorld.class */
     /* Originally compiled from HelloWorld.java */

     import java.io.PrintStream;

     class HelloWorld
     {
       public static void main(String astring[])
       {
         System.out.println(“Hello World!”);
       }

       public HelloWorld()
       {
       }
     }
     %

There are several differences between the original .java source code and the decompiled version:

!  The HelloWorld() constructor is included in the decompiled program because it was in the byte code. It wasn’t in the original source code.
!  Mocha did not find the name of the String array that was passed into the main method. It was args[] in the source code but simply astring[] here.
!  The white space is different. In fact, Mocha often does a better job of uniformly and consistently inserting white space than manual placement or many fancy source code editors. More than one programmer has been surprised to see that Mocha outputs cleaner code than he or she originally wrote!

Although Mocha was the first Java decompiler and is still the best known, several other companies have developed Java decompilers of varying degrees of usefulness. A research scientist at IBM published a technical report on decompiling Java, and he may have developed a working decompiler. However, IBM reclassified the report before I was able to get a copy of it.

Wingsoft, http://www.wingsoft.com/, is a startup company that released a payware Java decompiler called WingDis as this book was going to press. Initial testing indicates that WingDis is about as reliable as Mocha. However, both Mocha and WingDis have problems with switch statements, complicated loops, and try-catch-finally blocks. WingDis is included on the CD.

Summary

This rather long chapter explore the meaning of more than 200 different opcodes in Java. It also completed the disassembly project that began in Chapter 4. Among other things you learn:

!  Opcodes, also known as byte codes, each have exactly one mnemonic constant to represent them. However, these mnemonic constants are conveniences for disassemblers. Java uses only the raw numbers.
!  Most opcodes come in multiple flavors that handle different data types. Thus, although there are a little over 200 opcodes, many of them are simple variations of each other. Therefore, you really need to know only about 60 of them.
!  With a few exceptions, opcodes take values from the byte codes following the opcode in the file and/or from the stack. All values are returned to the stack. The byte codes never change while a program is running.
!  The load opcodes move values from the local variable array to the stack. The store opcodes move values from the stack to the local variable array.

In the next chapter, I back off from this extremely low-level approach and start to investigate threading and garbage collection. You learn about the different ways that these technologies can be implemented, learn how to determine in which fashion they are implemented on a particular platform, and learn how to test new platforms.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.