8

I know this question was asked gzillions of times already, but I am specifically after a decompiler that would handle enums corrently (because the jar I am decompiling uses enums heavily). I tested that both JAD and JD-GUI don't.

Could someone recommend a decompiler (ideally that works under Linux and can easily handle the whole jar without requiring me to write shell scrips)?

EDIT: Specifically I have issues with constructs like:

switch(myEnum) {
case A: ...
case B: ...
}

they get decompiled (for both JAD and JD-GUI) as something like:

switch ($SWITCH_TABLE$com$MyType()[myEnum.ordinal()]) {
case 1:
case 2:
}

where $SWITCH_TABLE$com$MyType() is either not declared at all or doesn't compile.

Grzenio
  • 35,875
  • 47
  • 158
  • 240
  • possible duplicate of [Where can I find a Java decompiler?](http://stackoverflow.com/questions/671394/where-can-i-find-a-java-decompiler) – Joachim Sauer Oct 24 '11 at 11:29
  • 1
    According to the question I linked to above http://java.decompiler.free.fr/ provides the best one for Java 5. If this doesn't work for you, then tell us *how* it doesn't help. – Joachim Sauer Oct 24 '11 at 11:29
  • I don't know about JD-GUI but we are using JD-Eclipse which uses an older version of JD-Core that JD-GUI does, and it handles enums as well. – Thomas Oct 24 '11 at 11:31
  • @JoachimSauer, java.decompiler.free.fr is JD-GUI, and it didn't work in my case. – Grzenio Oct 24 '11 at 11:33
  • 3
    Java 5 **[isn't exactly 'new' anymore](http://en.wikipedia.org/wiki/Java_version_history#J2SE_5.0_.28September_30.2C_2004.29)** by any stretch of imagination – sehe Oct 24 '11 at 11:34
  • 1
    @Grzenio: please define "didn't work". Can you define a sample of how it doesn't work? This question is no better than any of the other hunderets of duplicates out there, *unless* you can tell us **what** doesn't work for you. – Joachim Sauer Oct 24 '11 at 11:34
  • @sehe, I can't agree more with you – Grzenio Oct 24 '11 at 11:38
  • I've used JD-GUI before and had mixed results, particularly with obfuscated code -- obfuscation can rename variables to illegal names (such as a.1, a.2, etc.) and JD-GUI passes this through unfixed. JD-GUI can also produce output which uses locals before they are declared. Both of these are easily fixed of course, but for a large project, it leaves a lot of manual work after the decompilation. – mah Oct 24 '11 at 11:39
  • @Grzenio: ah, now I see. Yes, it seems that switches on enums are not completely decompiled with JD-GUI yet, even in unobfuscated code. – Joachim Sauer Oct 24 '11 at 11:42
  • According to [this bug entry](http://java.decompiler.free.fr/?q=node/335) this should have been fixed but I (and it seems others as well) can still reproduce the problem with the current JD-GUI. – Joachim Sauer Oct 24 '11 at 11:48
  • @JoachimSauer, this bug is closed and 2 years old :( I doubt they are actively working on it anymore. – Grzenio Oct 24 '11 at 12:28

2 Answers2

2

I have found that the product of a research project, CFR, handles Enums particularly well.

As at this time JD-GUI and JAD perform no better than when the OP's question was posted, while CFR version 0.43 produces correct, compilable source, even when fed obfuscated classes.

Dave
  • 764
  • 7
  • 17
2

It is not possible to decompile a enum-switch properly. The java-classfile doesn't know about the enum-Typ after compilation, thus your decompiler cant decompile it as you like it.

Enums-Types are all a subtype of java.lang.Enum, every enum constant got an ordinal-number which is used in a switch statement. The compiler do a switch on that int-value, a switch over an enum type is simply said syntactic-sugar. If you try to switch on an null-enum you will get a NPE.

Chriss
  • 5,157
  • 7
  • 41
  • 75
  • Depends how smart the decompiler is I guess. If it is able to find the declaration of the enum, then it should be able to map the ordinal numbers back to names. – Grzenio Oct 25 '11 at 17:59
  • If I decompile the $1 version a class file which switches on an ENUM, I get a list of all the ordinals along with the ENUM constants. It will be possible to decompile this - it just needs coding. Unfortunately it doesn't seem that JD-Java Decompiler is open source :-( and JAD is too old to get up to speed on everything else without major effort. – mjaggard Apr 25 '12 at 17:18