4

I want to extract all method calls from java code. I have written following two regular expression but they are not able to extract all the method calls.

Reg1 : Pattern.compile("([a-zA-Z][0-9_a-zA-Z]*\\([a-zA-Z0-9_\\s,\\[\\]\\(\\)\\.]+\\))");

Reg2 : Pattern.compile("([a-zA-Z][0-9_a-zA-Z]*\\([\\s]*\\))")

Input:

"{
     if ((war == null) && (config != null)) {
    sb.append( &config= );
    sb.append(URLEncoder.encode(config,getCharset()));
    }
    if ((war == null) && (localWar != null)) {
    sb.append( &war= );
    sb.append(URLEncoder.encode(localWar,getCharset()));
    }
    if (update) {
    sb.append( &update=true );
    }
    if (tag != null) {
      sb.append( &tag= );
      sb.append(URLEncoder.encode(tag,getCharset()));
     }
     }"

output:

getCharset getCharset getCharset append append append

I am not able to extract "encode".

Does anyone have any idea as an what should I add to regular expression?

5
  • 5
    This is (according to well-established principles of language theory) impossible to do using regular expressions, mainly because each call may contain calls that may contain calls that ... Commented Sep 17, 2015 at 5:02
  • Please suggest me some alternative of it. Commented Sep 17, 2015 at 5:06
  • 2
    Perhaps this post is of any help stackoverflow.com/questions/2206065/… Commented Sep 17, 2015 at 5:19
  • Thanks! but I want to extract method calls made in the program. Commented Sep 17, 2015 at 5:24
  • 1
    Same goes for Java: stackoverflow.com/questions/6751105/… Commented Sep 17, 2015 at 6:56

1 Answer 1

10

You need a Java Code Parser for this task. Here is an example which uses Java Parser:

public class MethodCallPrinter
{
    public static void main(String[] args) throws Exception
    {
        FileInputStream in = new FileInputStream("MethodCallPrinter.java");

        CompilationUnit cu;
        try
        {
            cu = JavaParser.parse(in);
        }
        finally
        {
            in.close();
        }
        new MethodVisitor().visit(cu, null);
    }

    private static class MethodVisitor extends VoidVisitorAdapter
    {
        @Override
        public void visit(MethodCallExpr methodCall, Object arg)
        {
            System.out.print("Method call: " + methodCall.getName() + "\n");
            List<Expression> args = methodCall.getArgs();
            if (args != null)
                handleExpressions(args);
        }

        private void handleExpressions(List<Expression> expressions)
        {
            for (Expression expr : expressions)
            {
                if (expr instanceof MethodCallExpr)
                    visit((MethodCallExpr) expr, null);
                else if (expr instanceof BinaryExpr)
                {
                    BinaryExpr binExpr = (BinaryExpr)expr;
                    handleExpressions(Arrays.asList(binExpr.getLeft(), binExpr.getRight()));
                }
            }
        }
    }
}

Output:

Method call: parse
Method call: close
Method call: visit
Method call: print
Method call: getName
Method call: getArgs
Method call: handleExpressions
Method call: visit
Method call: handleExpressions
Method call: asList
Method call: getLeft
Method call: getRight
Sign up to request clarification or add additional context in comments.

9 Comments

The main difficulty is to handle recursions in the sentences. You might add this to your answer, e.g., another visitor for expressions.
@laune what do you mean with "recursions in the sentences"?
The Java grammar defines how "sentences" of the language are to be constructed. In the definition of "method call", via some NTs in between, "method call" appears again: recursion. - So there must be an iteration for( Expression e: methodCall.getArgs() ){ ... } etc.
@laune Understand. I overlooked that. Thought that Java Parser would handle this gracefully. :-)
@Sangeeta you could use methodCall.getBeginLine() to check if the method call is inside of your desired part of the source code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.