views:

297

answers:

2

How can I determine for any Java .class file if that was compiled with debug info or not?

How can I tell exactly what -g{source|lines|vars} option was used?

+1  A: 

You must check the Code structure in the class file and look for LineNumberTable and LocalVariableTable attributes.

Aaron Digulla
+2  A: 

If you're on the command line, then javap -l will display LineNumberTable and LocalVariableTable if present:

peregrino:$ javac -d bin -g:none src/Relation.java 
peregrino:$ javap -classpath bin -l Relation 
public class Relation extends java.lang.Object{
public Relation();

peregrino:$ javac -d bin -g:lines src/Relation.java 
peregrino:$ javap -classpath bin -l Relation 
public class Relation extends java.lang.Object{
public Relation();
  LineNumberTable: 
   line 1: 0
   line 33: 4

peregrino:$ javac -d bin -g:vars src/Relation.java 
peregrino:$ javap -classpath bin -l Relation 
public class Relation extends java.lang.Object{
public Relation();

  LocalVariableTable: 
   Start  Length  Slot  Name   Signature
   0      5      0    this       LRelation;

javap -c will display the source file if present at the start of the decompilation:

peregrino:$ javac -d bin -g:none src/Relation.java 
peregrino:$ javap -classpath bin -l -c Relation | head
public class Relation extends java.lang.Object{
  ...

peregrino:$ javac -d bin -g:source src/Relation.java 
peregrino:$ javap -classpath bin -l -c Relation | head
Compiled from "Relation.java"
public class Relation extends java.lang.Object{
  ...

Programmatically, I'd look at ASM rather than writing yet another bytecode reader.

Pete Kirkham