Java method call handler?

I need to parse specific method invocation calls, including all the signature from some Java classes, for example.

public class MyClass {
    public void myMthod() {
        // ... some code here
        result = someInstance.someOtherMethod(param1, param2);
        // ... some other code here
    }
}

As a result, I would like to get something like:

serviceName = someInstance
methodName = someOtherMethod
arguments = {
   argument = java.lang.String,
   argument = boolean
}
result = java.lang.Long

What will be the fastest way to achieve this? I was thinking about using a RegEx parser. The problem is that there are several entry patterns, for example.

a)
result = someInstance.someOtherMethod(getSomething(), param);

b)
result = 
    getSomeInstance().someOtherMethod(param);

c)
result = getSomeInstance()
            .someOtherMethod(
                    getSomethingElse(), null, param);

Any help would be really appreciated! Thank!

+3
source share
2 answers

Do not use regex! Use tools that java understands.

Use either:

ASM , .


javaparser: , VoidVisitorAdapter

public void visit(MethodCallExpr n, A arg)

:

public static void main(final String[] args) throws Exception{
    parseCompilationUnit(new File("src/main/java/foo/bar/Phleem.java"));
}

public static void parseCompilationUnit(final File sourceFile)
    throws ParseException, IOException{
    final CompilationUnit cu = JavaParser.parse(sourceFile);
    cu.accept(new VoidVisitorAdapter<Void>(){

        @Override
        public void visit(final MethodCallExpr n, final Void arg){
            System.out.println(n);
            super.visit(n, arg);
        }
    }, null);
}

, , , / , . , ASM - .


ASM: ,

+7

, langauge Java ; . , ( , ) langauge, .

, ( ) , . , Java, , ; oherwise,   , "someService" , , - , , ?

0

Source: https://habr.com/ru/post/1771724/


All Articles