java - Counting Methods declaration + Methods Calls in a class using JavaParser -
i'm trying code rfc metric (response class), counts method declarations + method calls.
method declarations works fine, got problem @ counting method calls using javaparser api.
here code:
public class methodprinter { public static void main(string[] args) throws exception { // creates input stream file parsed fileinputstream in = new fileinputstream("d://test.java"); compilationunit cu; try { // parse file cu = javaparser.parse(in); } { in.close(); } // visit , print methods names methodvisitor mv =new methodvisitor(); cu.accept(new voidvisitoradapter<void>(){ @override public void visit(final methodcallexpr n, final void arg){ system.out.println("method call :"+n); super.visit(n, arg); } }, null); } }
test.java
package test; import java.util.*; public class classa { private int test; private classb classb = new classb(); public void dosomething(int t){ system.out.println ( tostring()); int i= dosomethingbasedonclassbbv(classb.tostring()); } protected void dosomethingbasedonclassb(){ system.out.println (classb.tostring()); } } public class classb { private classa classa = new classa(); public void dosomethingbasedoneclassa(){ system.out.println (classa.tostring()); } private string tostring(){ return "classb"; } private string tostring(){ return "classb"; } public void dosomethingbasedoneclassa(){ system.out.println (classa.tostring()); } protected void dosomethingbasedonclassb(){ system.out.println (classb.tostring()); } }
the result of code:
*method call :system.out.println(tostring()) method call :tostring() method call :dosomethingbasedonclassbbv(classb.tostring()) method call :classb.tostring() method call :system.out.println(classb.tostring()) method call :classb.tostring() method call :system.out.println(classa.tostring()) method call :classa.tostring() method call :system.out.println(classa.tostring()) method call :classa.tostring() method call :system.out.println(classb.tostring()) method call :classb.tostring()*
indeed, code inspects method calls, in case don't want count libraries method calls system.out.println(tostring())
, want count tostring()
.
if got better code, or api use... welcomed, thank in advance.
you cannot resolve issue using exclusively parser, because need resolve method call , find out if call method part of standard library.
you can using javasymbolsolver, library created complement javaparser. in practice call:
javaparserfacade.solvemethodasusage(mymethodcallexpr)
and methoddeclaration tells type on method defined. once have type can @ package , check if starts java
or javax
, exclude it.