The Artima Developer Community
Sponsored Link

Weblogs Forum
Decompiling Java

2 replies on 1 page. Most recent reply: Jun 22, 2005 7:14 AM by Christopher Diggins

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Decompiling Java (View in Weblogs)
Posted: Jun 21, 2005 6:46 PM
Reply to this message Reply
Summary
Here is some Java code which uses the BCEL to convert a class file into java byte code.
Advertisement
I have been playing with Java byte code lately and I've just discovered the awesome Byte Code Engineering Library (BCEL). I feel like a kitten with a ball of yarn. Here is the source for a simple decompiler:
import java.io.*;

import java.util.Iterator;
import org.apache.bcel.*;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.*;
import org.apache.bcel.Repository;

public class Decompiler
{
  public static void main(String[] argv)
  {
    try
    {
      // Load the class from CLASSPATH.
      JavaClass       clazz   = Repository.lookupClass(argv[0]);
      Method[]        methods = clazz.getMethods();
      ConstantPoolGen cp = new ConstantPoolGen(clazz.getConstantPool());
      InstructionFactory f = new InstructionFactory(cp);

      for(int i=0; i < methods.length; i++)
      {
        Method m = methods[i];
        MethodGen mg = new MethodGen(m, clazz.getClassName(), cp);
        decompileMethod(mg, f);
      }
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  private static final void decompileMethod(MethodGen mg, InstructionFactory f)
  {
    InstructionList   il    = mg.getInstructionList();
    int               count = 0;

    System.out.println("Method " + mg.getName());
    InstructionHandle ih = il.getStart();
    while (ih != null) {
      System.out.println(ih.toString());
      ih = ih.getNext();
    }

    il.dispose(); // Reuse instruction handles
  }
}
I am planning next on a writing a ClassLoader which hacks code as it loads it. This is so much fun!


Eugene Kuleshov

Posts: 1
Nickname: eu
Registered: Jun, 2005

Re: Decompiling Java Posted: Jun 22, 2005 2:42 AM
Reply to this message Reply
Christopher, it is really time to discover ASM. :-)
http://www.jroller.com/page/eu/20050622#it_is_time_to_discover

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Decompiling Java Posted: Jun 22, 2005 7:14 AM
Reply to this message Reply
> Christopher, it is really time to discover ASM. :-)
> http://www.jroller.com/page/eu/20050622#it_is_time_to_disco
> ver

Very nice! Thanks for bringing ASM to my attention. The direct link to ASM for those following the discussion is:

http://asm.objectweb.org/

Flat View: This topic has 2 replies on 1 page
Topic: Adding Optional Static Typing to Python -- Part II Previous Topic   Next Topic Topic: We Need A New Information-Sharing Model for the Internet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use