• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Decompiler for Java 1.5+

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is there any decompiler for latest java version. i.e to decompile java 1.5 features like Generic Types, Enum etc.

Thanks
Kumaran
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bad idea dude.

From the java license file

3. RESTRICTIONS. Software is confidential and copyrighted.
Title to Software and all associated intellectual property
rights is retained by Sun and/or its licensors. Unless
enforcement is prohibited by applicable law, you may not
modify, decompile, or reverse engineer Software.



Are you aware that Sun gives away the source code too?
Check out your java installation directory for a file called src.zip. You are legally allowed to view that source code. If what you want is not in there, then its illegal.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what Kumaran means is that all the freely available decompilers (like JAD and JODE) do not support those Java 5 features, so the code they emit is pretty far from the original source code. I've had good luck adding support for generics to JODE (it's not all that hard), but supporting enums would be rather more complicated.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:Bad idea dude.

From the java license file


But you can still decompile your own code, or code from others that do allow decompiling.

As for the answer: you can't. The reason is quite clear: the compiler erases this information.

For instance:
  • Enums get compiled into a class that extends java.lang.Enum. All enum constants are public static final fields of the same type; the constructors get two fields added (ordinal and name) which get filled in by the compiler. values() returns a clone() of a private static array called $VALUES.
  • Generic types get completely erased at compile time, and converted in explicit casts where needed (search for type erasure for more information).
  • For-each loops get turned into simple loops using an Iterator or array index.


  • I've decompiled classes often enough (out of interest) to know
     
    Rob Spoor
    Sheriff
    Posts: 22784
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ulf Dittmer wrote:I've had good luck adding support for generics to JODE (it's not all that hard), but supporting enums would be rather more complicated.


    How did you do that? Using the type objects are cast to? That's the only remaining reference to the generic type after compiling, as far as I know.
    As for enums, those should also not be that hard if you follow the rules I mentioned in my previous post. Simply create some enums, decompile them and see how its done. You can simply tell it's an enum because it extends java.lang.Enum ;)
     
    Kumaran Rajakumar
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello..
    I understand the license. I dont want to decompile Sun's java code. I know that they distribute it.

    I need to decompile some other sample class file. which i downloaded from Internet.

    Thanks
    Kumaran
     
    Ulf Dittmer
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Prime wrote:

    Ulf Dittmer wrote:I've had good luck adding support for generics to JODE (it's not all that hard), but supporting enums would be rather more complicated.


    How did you do that? Using the type objects are cast to? That's the only remaining reference to the generic type after compiling, as far as I know.


    Let me qualify that: I added support for restoring the generics information in class and method signatures, as well as field declarations, not within the actual code. That information is mostly stripped away as you say. But the classfile attributes contain the information for the class itself, its fields and methods.

    As for enums, those should also not be that hard if you follow the rules I mentioned in my previous post. Simply create some enums, decompile them and see how its done. You can simply tell it's an enum because it extends java.lang.Enum ;)


    I don't recall all the details, but it seemed more involved than that. But it wasn't that important to me compared to generics) so I didn't pursue it.
     
    Kumaran Rajakumar
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Ulf and Rob, I was looking for these kind of information like why we are not able to decompile completely... ;-)
     
    Ulf Dittmer
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I found the example Enums I used for analyzing the bytecode that gets generated. For example, this simple Enum:

    is turned into:

    That's a fair amount of code to analyze until you can conclude that it's a bare bones Enum. Something slightly more complicated:

    is turned into

    So you'd have to analyze the Size constructor calls to get the values. Certainly not rocket science, but not particularly fun stuff to code, either...
     
    Rob Spoor
    Sheriff
    Posts: 22784
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It becomes even harder when enum constants override methods. A good example is java.util.concurrent.TimeUnit - a 332 line source file (and that's including lots of comments) turns into a 479 line source file after decompiling - without comments. That's because every one of the enum constants has its own anonymous class that overrides 9 methods.
     
    Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic