You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by "Nikhil C. Khedkar" <ni...@yahoo.com> on 2004/01/27 08:09:24 UTC

Source Line Numbers from Class files

Hi,
  I am working on getting the source code line numbers
from class files using BCEL. 

ConstantPool constants  =
java_class.getConstantPool();
ConstantPoolGen cp = new ConstantPoolGen(constants);

MethodGen methodGen = new MethodGen(method,
java_class.getClassName(), cp);
Instruction [] instructions =
methodGen.getInstructionList().getInstructions();
LineNumberGen[] aLineNumberGen =
methodGen.getLineNumbers();

I intend to use getSourceLine() API of LineNumberGen
class. But the array aLineNumberGen is returned of 0
length. Is there any setting of the compiler that will
save the line numbers in the class files, because of
which it is not working right now.

Thanks,
   Nikhil

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Re: Source Line Numbers from Class files

Posted by Martin Schöffler <sc...@t-online.de>.
Nikhil C. Khedkar wrote:
> Hi,
>   I am working on getting the source code line numbers
> from class files using BCEL. 
> 
> ConstantPool constants  =
> java_class.getConstantPool();
> ConstantPoolGen cp = new ConstantPoolGen(constants);
> 
> MethodGen methodGen = new MethodGen(method,
> java_class.getClassName(), cp);
> Instruction [] instructions =
> methodGen.getInstructionList().getInstructions();
> LineNumberGen[] aLineNumberGen =
> methodGen.getLineNumbers();
> 
> I intend to use getSourceLine() API of LineNumberGen
> class. But the array aLineNumberGen is returned of 0
> length. Is there any setting of the compiler that will
> save the line numbers in the class files, because of
> which it is not working right now.

The compiler has to generate debugging-info. This can be ensured using 
the -g option. I think you can also use -g:lines, haven't checked myself 
if that is enough though.

fyi, just if you are interested - i used another approach to get the 
linenumbers out of a class file:

LineNumberTable lnt = method.getLineNumberTable();
Code code = method.getCode();
InstructionList il = new InstructionList(code.getCode());
InstructionHandle ih = il.getStart();

int sourceLine = lnt.getSourceLine(ih.getPosition());

then you can iterate over the instruction handles using ih.getNext();

greetings,
Martin


---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Re: Source Line Numbers from Class files

Posted by Alessandra Bez <al...@yahoo.it>.
The source code line number are stored in the InstructionList class in 
 int[] byte_positions. you can try:
Method[] methods=clazz.getMethods();
....
MethodGen mg = new MethodGen(methods[m ], class_name, cp);
InstructionList the = mg.getInstructionList(); 
int[ ] position=il.getInstructionPositions(); 
 
where m is the index of method.

"Nikhil C. Khedkar" <ni...@yahoo.com> wrote:
Hi,
I am working on getting the source code line numbers
from class files using BCEL. 

ConstantPool constants =
java_class.getConstantPool();
ConstantPoolGen cp = new ConstantPoolGen(constants);

MethodGen methodGen = new MethodGen(method,
java_class.getClassName(), cp);
Instruction [] instructions =
methodGen.getInstructionList().getInstructions();
LineNumberGen[] aLineNumberGen =
methodGen.getLineNumbers();

I intend to use getSourceLine() API of LineNumberGen
class. But the array aLineNumberGen is returned of 0
length. Is there any setting of the compiler that will
save the line numbers in the class files, because of
which it is not working right now.

Thanks,
Nikhil

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org



---------------------------------
Yahoo! Mail: 6MB di spazio gratuito, 30MB per i tuoi allegati, l'antivirus, il filtro Anti-spam