Can javap show the original source code lines intermingled with the bytecode like objdump -S?

1.2k views Asked by At

I know this debug information is contained in the .class file when compiling with:

javac -g Main.java

and can be observed manually from the LineNumberTable: section of:

javap -c -constants -private -verbose '$<' > '$@'

What I want is to make javap display the source in the middle of the bytecode.

Sample input:

public class New {
    public static void main(String[] args) {
        System.out.println(new Integer(1));
    }
}

Actual javap output:

   0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
   3: new           #3                  // class java/lang/Integer
   6: dup
   7: iconst_1
   8: invokespecial #4                  // Method java/lang/Integer."<init>":(I)V
  11: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
  14: return
LineNumberTable:
  line 3: 0
  line 4: 14

Desired javap output:

       System.out.println(new Integer(1));
   0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
   3: new           #3                  // class java/lang/Integer
   6: dup
   7: iconst_1
   8: invokespecial #4                  // Method java/lang/Integer."<init>":(I)V
  11: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
    }
  14: return
LineNumberTable:
  line 3: 0
  line 4: 14

That would make it much easier to interpret javap output.

Similar but more generic question: How to use javap to see what lines of bytecode correspond to lines in the Java code?

I have tried to:

but there was no reply, and my messages don't even appear on those websites. Not a very open project.

1

There are 1 answers

0
MvG On

I don't think javap supports this use case, but I've been toying with some class file parsing code these past few days, and as of today it's capable of mixing source code with assembly code. See https://github.com/gagern/classfile for details and source code.