javalang icon indicating copy to clipboard operation
javalang copied to clipboard

Array argument

Open dfagneray opened this issue 7 years ago • 2 comments

I'm currently using the Java parser Javalang because I need to iterate through an AST from a java source code, but I need to do it from a python file.

It appears to be quite useful but I have a problem when parsing arguments in a method signature. For exemple in a classic

public void main(String[] args){}

The String[] args is being parsed as a FormalParameter, who's a subclass of Declaration, itself a subclass of Node. In this particular exemple, the type field of FormalParamter will be ReferenceType, that got 4 fields: name,declarations,arguments,sub_type. The name field return only String, and the sub_type returns None. There is no indication of "args" being an array. How can i get that back ?

dfagneray avatar Apr 16 '18 11:04 dfagneray

This is indicated by the dimensions field of ReferenceType (inherited from Type). Here's an example,

import javalang

ast = javalang.parse.parse_member_signature('public static void main(String args) {}')
print ast.parameters[0].type.dimensions # []

ast = javalang.parse.parse_member_signature('public static void main(String[] args) {}')
print ast.parameters[0].type.dimensions # [None]

ast = javalang.parse.parse_member_signature('public static void main(String[][] args) {}')
print ast.parameters[0].type.dimensions # [None, None]

c2nes avatar Apr 22 '18 14:04 c2nes

Oh, thanks a lot, I didn't understand what dimensions was !

dfagneray avatar Apr 22 '18 18:04 dfagneray