javalang icon indicating copy to clipboard operation
javalang copied to clipboard

Get all methods' code

Open anshul17024 opened this issue 5 years ago • 3 comments

Can someone please tell how can I get code of all methods present in a .java file. For example, if the code is: ` 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;    
}    

}

public void display() {
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();    

} `

I need the following output: `{method 1: public void addNode(int data) { ......}

method 2: public void display(){....} }`

anshul17024 avatar Jun 17 '20 15:06 anshul17024

tree = javalang.parse.parse(file.read())
for path, node in tree.filter(javalang.ree.MethodDeclaration):
     print(node) $ node is what you need

lyriccoder avatar Jul 15 '20 15:07 lyriccoder

tree = javalang.parse.parse(file.read())
for path, node in tree.filter(javalang.ree.MethodDeclaration):
     print(node) $ node is what you need

@lyriccoder Hello, your code doesn't seem to work. AttributeError: module 'javalang' has no attribute 'ree' I tried to replace the erroneous statement with tree.filter(javalang.tree.MethodDeclaration) and while there isn't any syntax error, the output is still incorrect

MethodDeclaration(annotations=[Annotation(element=[ElementValuePair(name=origins, value=Literal(postfix_operators=[], prefix_operators=[], qualifier=None, selectors=[], value="*"))], name=CrossOrigin), Annotation(element=[ElementValuePair(name=value, value=Literal(postfix_operators=[], prefix_operators=[], qualifier=None, selectors=[], value="/login")), ElementValuePair(name=method, value=MemberReference(member=POST, postfix_operators=[], prefix_operators=[], qualifier=RequestMethod, selectors=[])), ElementValuePair(name=produces, value=Literal(postfix_operators=[], prefix_operators=[], qualifier=None, selectors=[], value="application/json")), ElementValuePair(name=consumes, value=Literal(postfix_operators=[], prefix_operators=[], qualifier=None, selectors=[], value="application/json"))], name=RequestMapping)], body=[LocalVariableDeclaration(annotations=[], declarators=[VariableDeclarator(dimensions=[], initializer=MethodInvocation(arguments=[MemberReference(member=username, postfix_operators=[], prefix_operators=[], qualifier=input, selectors=[])], member=fetch, postfix_operators=[], prefix_operators=[], qualifier=User, selectors=[], type_arguments=None), name=user)], modifiers=set(), type=ReferenceType(arguments=None, dimensions=[], name=User, sub_type=None)), IfStatement(condition=MethodInvocation(arguments=[MemberReference(member=password, postfix_operators=[], prefix_operators=[], qualifier=input, selectors=[])], member=md5, postfix_operators=[], prefix_operators=[], qualifier=Postgres, selectors=[MethodInvocation(arguments=[MemberReference(member=hashedPassword, postfix_operators=[], prefix_operators=[], qualifier=user, selectors=[])], member=equals, postfix_operators=None, prefix_operators=None, qualifier=None, selectors=None, type_arguments=None)], type_arguments=None), else_statement=BlockStatement(label=None, statements=[ThrowStatement(expression=ClassCreator(arguments=[Literal(postfix_operators=[], prefix_operators=[], qualifier=None, selectors=[], value="Access Denied")], body=None, constructor_type_arguments=None, postfix_operators=[], prefix_operators=[], qualifier=None, selectors=[], type=ReferenceType(arguments=None, dimensions=None, name=Unauthorized, sub_type=None)), label=None)]), label=None, then_statement=BlockStatement(label=None, statements=[ReturnStatement(expression=ClassCreator(arguments=[MethodInvocation(arguments=[MemberReference(member=secret, postfix_operators=[], prefix_operators=[], qualifier=, selectors=[])], member=token, postfix_operators=[], prefix_operators=[], qualifier=user, selectors=[], type_arguments=None)], body=None, constructor_type_arguments=None, postfix_operators=[], prefix_operators=[], qualifier=None, selectors=[], type=ReferenceType(arguments=None, dimensions=None, name=LoginResponse, sub_type=None)), label=None)]))], documentation=None, modifiers=set(), name=login, parameters=[FormalParameter(annotations=[Annotation(element=None, name=RequestBody)], modifiers=set(), name=input, type=ReferenceType(arguments=None, dimensions=[], name=LoginRequest, sub_type=None), varargs=False)], return_type=ReferenceType(arguments=None, dimensions=[], name=LoginResponse, sub_type=None), throws=None, type_parameters=None)

is there a solution for this?

cyberjj999 avatar Nov 19 '20 12:11 cyberjj999

@cyberjj999 Right, you've got the class MethodDeclaration. You've got what you need. I think you need to get the name of the method. This class has lots of fields. You need the name filed.

node.name

lyriccoder avatar Dec 04 '20 07:12 lyriccoder