Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting full method declarations and fieldnames #121

Open
pjcuser opened this issue Sep 5, 2022 · 2 comments
Open

Extracting full method declarations and fieldnames #121

pjcuser opened this issue Sep 5, 2022 · 2 comments

Comments

@pjcuser
Copy link

pjcuser commented Sep 5, 2022

All the previous question ask to extract method + its body as a text.

I want to know given a .java source, how can I extract method declarations which means:

  • Modifiers
  • Return type
  • Function name
  • Function arguments/parameters

as a python list.

PS: I don't need method's body.

Can the same be done for fields in class as well?

Is it possible with javalang?

@cryptax
Copy link

cryptax commented Sep 26, 2022

Yes, it's possible, but you'll have to do some work yourself to make the list the way you want it.

  • list all methods with tree.filter(javalang.tree.MethodDeclaration)
  • for each method, its function name is name field
  • modifier is modifiers
  • arguments: they will be listed in parameters field. Then, you need to retrieve the type and name of the parameter.
  • return value is return_type field. Get the type name with name.

This is an example of very simple method:

MethodDeclaration(annotations=[], body=[ReturnStatement(expression=This(postfix_operators=[], prefix_operators=[], qualifier=None, selectors=[MemberReference(member=msg, postfix_operators=None, prefix_operators=None, qualifier=None, selectors=None)]), label=None)], documentation=None, modifiers={'public'}, name=getMessage, parameters=[], return_type=ReferenceType(arguments=None, dimensions=[], name=String, sub_type=None), throws=None, type_parameters=None)

@faysalhossain2007
Copy link

faysalhossain2007 commented Oct 6, 2022

how can I get the body from this ?
I tried the following code, but nothing works:

for path, node in tree.filter(javalang.tree.MethodDeclaration):
     print(node['body'])

for path, node in tree.filter(javalang.tree.MethodDeclaration):
     print(node[1])

EDIT: I am able to access it using node.body

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants