![]() |
|||
![]() ![]() |
![]() |
|
![]() |
Decompilers and Other ToolsWith 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 youre using third-party software for which you do not have source code. If youre uncertain whether a bug is in your code, in the vendors code, or in your understanding of what the vendors library does, you can disassemble the relevant files and find out. However, some readers may wonder if its 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, its often possible to fill in the blanks and recognize certain common patterns. Throughout this chapter, youve seen how different constructs tend to compile. For example, when you see a tableswitch or lookupswitch statement in disassembled byte code, you know that theres a switch statement in the source code. With experience and practice, its not very hard to manually rewrite disassembled byte code as .java source code. If you dont understand a particular method, its 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 Vliets 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 Im 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 Heres 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:
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. SummaryThis 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:
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.
|
![]() |
|