Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are decompiled java programs not always directly compilable and what are the parts that are not?

So I am trying to make slight valid legal changes to a compiled java program. I am decompiling it using JD-GUI for Mac. For the most part the decompiled code is error free but there are some strange things like undeclared variables, multiple identical variable declarations and just some strange statements which are not readily compilable. Some of the strange statements in the decompiled code are really puzzling. I have been having trouble with one switch statement in particular:

    switch ($SWITCH_TABLE$PackageName$ClassName$InnerEnumName()[getPlatform().ordinal()])

Where PackageName.ClassName is the class this statement is in, and InnerEnumName is an inner enum within ClassName. Also note that getPlatform() is a method in ClassName which returns an enum of type InnerEnumName

The weird part is when I just stripped this class of the problematic statements, compiled it, and inserted it back into the program, it started to work but had a few strange bugs. For example when I changed the switch statement to

    switch (getPlatform().ordinal())

it started hitting case 3 (the third case and the case for value 3) when it is supposed to hit case 4 (once again the fourth case as well as the case for value 4)

like image 318
Sam Bryant Avatar asked Aug 03 '11 19:08

Sam Bryant


People also ask

How accurate is decompiled code?

Show activity on this post. Short answer: 99% of the time, decompiled code will not look like the original code, but it should behave the same way. So, assuming the decompiler doesn't make any mistakes, it should be accurate in function, but not in implementation.

Can Java code be decompiled?

If you are writing Java classes and distributing them over the Internet, you should know that people can reverse-engineer, disassemble, or decompile your classes into Java source code. The most widely used decompiler (at least publicly) is Mocha.

What is decompiled code How is it different from the actual source code?

A decompiler is a computer program that receives input in the form of an executable file. If the file's source code is lost or corrupted for some reason, the decompilation process attempts to recover the code, or at least most of it.

How does Java decompiler work?

A Java Decompiler is a special type of decompiler which takes a class file as input and produces Java source code as output. The decompilation is exactly the reverse process of compilation. Thus, decompiler does not produce a replica of the source code.


1 Answers

Decompiling is always going to be imperfect. The decompiler must take the bytecodes and reverse-engineer the original source, figuring out where loops are, what the loop controls are, etc. I would never expect it to be flawless for non-trivial programs.

In the case of the $ names, these are names generated internally in the process of "faking" inner classes (since the JVM doesn't actually support inner classes). The decompiler is apparently doing an imperfect job of figuring out what the inner classes are and appropriately naming them and the objects the compiler created to fake things out. Someone familiar with the bytecode format could probably sort things out fairly quickly, but, like the rest, it's non-trivial.

(In this particular case it appears that the compiler, for some reason, created a mapping table from inner enum values to some other values, and when you "stripped" the statement you lost that mapping.)

[I'll add that one big problem that decompilers have is that javac is such a moving target. In particular things like inner class implementations are being constantly tweaked, so what worked one week may fail the next, with the next +.001 version of the compiler.]

like image 82
Hot Licks Avatar answered Sep 24 '22 18:09

Hot Licks