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

JavaParser doesn't seem to be able to find dot-chained method calls #3753

Closed
geajack opened this issue Nov 14, 2022 · 2 comments
Closed

JavaParser doesn't seem to be able to find dot-chained method calls #3753

geajack opened this issue Nov 14, 2022 · 2 comments
Labels
Question (AST) Questions about nodes and relationships within the AST (not about how to use JavaParser)

Comments

@geajack
Copy link

geajack commented Nov 14, 2022

Taking the following code and input with javaparser 3.24.7, I get the output f().g(), but I would expect the output:

f().g()
f()

Code:

package parser;

import java.lang.System;

import com.github.javaparser.JavaParser;
import com.github.javaparser.Providers;
import com.github.javaparser.Provider;
import com.github.javaparser.ParseStart;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

public class Parser {
    
    public static void main(String[] args)
    {
        JavaParser parser = new JavaParser();
        Provider provider = Providers.provider(System.in, parser.getParserConfiguration().getCharacterEncoding());
        ParseStart<MethodDeclaration> start = ParseStart.METHOD_DECLARATION;        
        
        MethodDeclaration root = parser.parse(start, provider).getResult().get();

        ExpressionExtractor expressionExtractor = new ExpressionExtractor();
        root.accept(expressionExtractor, null);
    }

    static class ExpressionExtractor extends VoidVisitorAdapter<Void>
    {
        public void visit(MethodCallExpr node, Void none)
        {
            System.out.println(node.toString());
        }
    }

}

Input:

void function ()
{
    f().g();
}

It seems like the parser just doesn't visit the "inner" call to f(), only its ancestor in the AST.

@jlerbsc
Copy link
Collaborator

jlerbsc commented Nov 15, 2022

Your visitor stops visiting the AST after calling the first MethodCallExpr. You need to modify it like this.

static class ExpressionExtractor extends VoidVisitorAdapter<Void>
{
    public void visit(MethodCallExpr node, Void none)
    {
        System.out.println(node.toString());
        **super.visit(node, node);**
    }
}

@jlerbsc jlerbsc added the Question (AST) Questions about nodes and relationships within the AST (not about how to use JavaParser) label Nov 15, 2022
@jlerbsc
Copy link
Collaborator

jlerbsc commented Jan 20, 2023

I'm closing this issue because I think I answered the question. If not, you can reopen it.

@jlerbsc jlerbsc closed this as completed Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question (AST) Questions about nodes and relationships within the AST (not about how to use JavaParser)
Projects
None yet
Development

No branches or pull requests

2 participants