javalang icon indicating copy to clipboard operation
javalang copied to clipboard

How to get method information in one line code snippet

Open Simplewyl2000 opened this issue 3 years ago • 3 comments

I want to get method information(modifier, method name, parameter...) from a one-line code snippet, like:

tokens = javalang.tokenizer.tokenize('private Member getAccessible(Member original){}')
parser = javalang.parser.Parser(tokens)
parser.parse_method_or_field_declaraction()

while it keeps throwing JavaSyntaxError

Traceback (most recent call last):
  File "/Users/giaogiaowu/cs_related/code/pycode/analysis_jar/utils.py", line 114, in <module>
    parser.parse_method_or_field_declaraction()
  File "/Users/giaogiaowu/opt/anaconda3/lib/python3.8/site-packages/javalang/parser.py", line 836, in parse_method_or_field_declaraction
    member_type = self.parse_type()
  File "/Users/giaogiaowu/opt/anaconda3/lib/python3.8/site-packages/javalang/parser.py", line 468, in parse_type
    self.illegal("Expected type")
  File "/Users/giaogiaowu/opt/anaconda3/lib/python3.8/site-packages/javalang/parser.py", line 119, in illegal
    raise JavaSyntaxError(description, at)
javalang.parser.JavaSyntaxError

Then I saw the implementation of parser.parse_method_or_field_declaraction(), it doesn't look like something I want. So which API or what kind of input should I choose to get certain information. Thanks!XD

Simplewyl2000 avatar Jan 29 '22 08:01 Simplewyl2000

@c2nes

Simplewyl2000 avatar Jan 29 '22 08:01 Simplewyl2000

Hey, bro, did you solve the problem? I also need to parse the Java snippet.

ybsun0215 avatar Mar 25 '22 02:03 ybsun0215

I have made several attempts, but all of them failed, I finally parsed the whole .java file.

Simplewyl2000 avatar Jul 21 '22 07:07 Simplewyl2000