c2nes / javalang

Pure Python Java parser and tools
MIT License
736 stars 161 forks source link

Method of a casted object not parsed #24

Closed henshin closed 8 years ago

henshin commented 8 years ago

Hi!

I was trying to use your parse to parse some Java code, but I noticed a problem in the parser. Here's a sample piece of code:

    public void setTest(final Valvel valve) {
        ((Valvel)valve).stop();     
    }

If you parse this code with the library, it will not recognize the stop method invocation. In fact anything after the Cast seems to be ignored, including any arguments passed to the stop method. Here's an output of the tree:

BODY: [StatementExpression]
CPATH: () CNODE: StatementExpression
Children: [None, Cast]
CPATH: (StatementExpression,) CNODE: Cast
Children: [ReferenceType, MemberReference]
CPATH: (StatementExpression, Cast) CNODE: ReferenceType
Children: [u'Valvel', [], None, None]
CPATH: (StatementExpression, Cast) CNODE: MemberReference
Children: [None, None, '', [], u'valve']

Do you know why this happens?

Thank you

c2nes commented 8 years ago

I think there is an issue with the attributes exposed by the Cast tree node, but I can find the method invocation within the selectors attribute of the expression,

>>> s = 'class Foo { public void setTest(final Valvel valve) {((Valvel)valve).stop(); }}'
>>> cu = javalang.parse.parse(s)
>>> cu.types[0].methods[0].body[0].expression.selectors[0]
MethodInvocation
henshin commented 8 years ago

I didn't knew about the selectors trick :) I was able to fix my code with your suggestion. Thank you!