Search code examples
javadecompiler

Do Java class files know about white space?


I wrote a Java program then compiled it and then decompiled it and got a Java file back. The decompiler gave back the exact Java file with exact indentations as my original Java file, but that is what decompilers do, this is not a problem the thing that burned the question in my mind is indentation.

As decompiled Java file have the exact same indentation as the original I conclude that class files store indentation data too.

Now the questions are as follows:

  1. Is my conclusion correct?

If yes:

  1. Why does a class file need to know about the indentation?

  2. Indentation is meant for readability and no human reads class files, so why can't class file just not store it and save some space?

  3. Are class files just an encrypted Java source file in which decompilers can decrypt to give the original back?


Solution

  • Compiled class files do not contain exact whitespace information of the source file.

    There are two possible reasons that I can think of that your decompiled class files look exactly like your input:

    • you happen to have written your original input with a style matching what the decompiler uses
    • you didn't actually look at the decompiled source, but at the original file somehow (for example because the decompiler didn't write the output where you thought it did)

    They do contain line numbers associated with the code that's executed to help with debugging.

    But try adding a few random spaces into some of your expressions and you'll see that the decompiler can't reconstruct those.

    And no, class files are not a simple "encryption" of the source files, plenty of information is lost when compiling (and some information taking from referenced classes might actually be added, such as the specific signatures of methods that are called).

    You can even turn off debugging information during compilation to strip even more information (like local variable names, for example).