2

As we know, we can get the abstract syntax tree from the source codes, using the tool like

org.eclipse.jdt.astview

But given the compiled class files, how to get the ASTs? Is there any existing tools? Can soot do it?

Thanks!

JackWM
  • 10,085
  • 22
  • 65
  • 92

3 Answers3

4

But given the compiled class files, how to get the ASTs?

You can't. It is not technically possible to reconstruct an AST for the original source code from a ".class" file. A lot of the information needed is no longer present in any form, and other information has been transformed irreversibly.

Is there any existing tools?

No.

The standard "answer" is to use a decompiler but:

  • a decompiler cannot reconstruct the original AST (see above)
  • the output of a decompiler often doesn't even remotely resemble the original source code
  • often the decompiled code won't even compile.
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    I would add that there are tools that allow you to browse the structure of a classfile. Any formal specification, including a Java classfile, can be internalized as some form of "AST", though the internalization of a classfile will be markedly different from the internalization of a source file. – Nathan Ryan Jun 19 '12 at 11:41
  • @NathanD.Ryan Your point is interesting. could you please mention some tool names for browsing the structure of a classfile. – JackWM Jun 19 '12 at 12:21
  • @JackWM: ASM (http://asm.ow2.org/) and BCEL (http://commons.apache.org/bcel/) are the ones that come immediately to mind. There are other, lesser known ones, some of which are only half implemented. – Nathan Ryan Jun 19 '12 at 14:56
  • 1
    I think Stephen is over the top here. As others here have pointed out, you can "decompile" the file to get an equivalent Java program, and then producing an AST is straightfoward.. Whether that is *useful* to do is another matter. (You certainly won't get the original comments or even most of the variable names) If all you want is to model what the class file *does*, then an "AST" representing its byte code (or some other model such as PDG from soot) is just fine. – Ira Baxter Apr 26 '15 at 03:15
0

Decompile and do the same thing.

Bytecode has no S, bytecode is the result of an AST-to-bytecode transformation.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

How about for a given classfile, we first decompile it using tool like jad, then we could get the "source code".

Even though this "source code" from decompilation is not exactly the same with the original one, it shares the same semantics. Below is a simple test.

Original java file:

package shape.circle;
public class Circle
{   int r;  // this is radius of a circle
    public Circle(int r)
    {   this.r = r;
    }

    /* get the diameter
       of this circle
    */
    public int getDiameter()
    {
        if(r==0)
        {   System.out.println("r is 0");
            return -1;
        }
        else
        {   int d = multiply(2,r);
            return d;
        }
    }

    int multiply(int a, int b)
    {   int c;
        c = a * b;
        return c;
    }
}

Below is the decompiled java file from Circle.class.

package shape.circle;
import java.io.PrintStream;
public class Circle
{

    public Circle(int i)
    {  r = i;
    }

    public int getDiameter()
    {   if(r == 0)
        {   System.out.println("r is 0");
            return -1;
        } else
        {
            int i = multiply(2, r);
            return i;
        }
    }

    int multiply(int i, int j)
    {
        int k = i * j;
        return k;
    }

    int r;
}

They are almost the same. Then as before, we can use the tool for source code

org.eclipse.jdt.astview

to get the AST.

JackWM
  • 10,085
  • 22
  • 65
  • 92