1

I want to extract the name of the methods and the body from a given Java Code. For example if i have

public void addNode(int data) {    
    //Create a new node    
    Node newNode = new Node(data);    

    //Checks if the list is empty    
    if(head == null) {    
        //If list is empty, both head and tail will point to new node    
        head = newNode;    
        tail = newNode;    
    }    
    else {    
        //newNode will be added after tail such that tail's next will point to newNode    
        tail.next = newNode;    
        //newNode will become new tail of the list    
        tail = newNode;    
    }    
}    

//display() will display all the nodes present in the list    
public void display() {    
    //Node current will point to head    
    Node current = head;    

    if(head == null) {    
        System.out.println("List is empty");    
        return;    
    }    
    System.out.println("Nodes of singly linked list: ");    
    while(current != null) {    
        //Prints each node by incrementing pointer    
        System.out.print(current.data + " ");    
        current = current.next;    
    }    
    System.out.println();    
}    

So i want to extract the methods with their body and redirect it to file in json format. For example end result should be like

{method 1: public void addNode(int data) { ......}
 method 2: public void display(){....}
}
  • Are you looking for something like this - **javap MyClassName**. Run from cmd/terminal. The **javap** will return fields, methods, members, etc from respetive class. – Vikas Dubey Jun 17 '20 at 12:05
  • No @Vikas Dubey. According to example on [link](https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html#:~:text=DESCRIPTION,prints%20its%20output%20to%20stdout.) it outputs in the form of java.lang.String date; whereas i just want the exact method declaration and the same body without any further high level description. The output of i want would be like keeping in mind the example on the link { method 1: public void init(){ method body here} method 2: public void paint(Graphics g){method body here} } – Richard Gene Jun 17 '20 at 12:18
  • 1
    Look into reflection https://stackoverflow.com/questions/3023354/how-to-get-string-name-of-a-method-in-java – Igor Flakiewicz Jun 17 '20 at 12:50
  • I don't think this has the answer i am looking for. I want a way to extract user methods from .java files as explained in this [issue] (https://github.com/c2nes/javalang/issues/68). Any help would be greatly appreciated. – Richard Gene Jun 17 '20 at 15:35

0 Answers0