How do I get the line numbers of a method and its body using our own eclipse plugin?

I created an eclipse plugin where the user right click on a method in the package explorer. It will generate a file with the method body. Currently my conclusion is

public int add(int x,int y){
 int z=x+y;
 return z;


I am using SourceMethod

to get the content of a method. The code looks like this.

 public void methodGenerator(SourceMethod methodname){
     String methodcontent=methodname.getSource();
     filegenerator = new FileWriter(FilePath);
     writer = new BufferedWriter(filegenerator);


Here's what I'm using org.eclipse.jdt.internal.core.SourceMethod

to get the content of the method. Now I need to get the line numbers. So how can I get the line numbers of the method?

I went through How do I get the line number of a method? here we can only get the start line no. My requirement is the generated file content should be as follows:

14.public int add(int x,int y){
15. int z=x+y;
16. return z;


Please give me some advice. I really need this logic. Please help me!


source to share

All Articles