我正在尝试使用INRIA开发的工具spoon来解析一个项目的方法调用,我已经有一个方法mysql表,其中存储了所有方法(methodid、methodname、classid、classname)。我使用以下代码来提取项目中的方法调用:
SpoonAPI spoon = new Launcher();
spoon.addInputResource("C:\\Users\\mouna\\Downloads\\chess and gantt code\\workspace_codeBase\\Chess\\src");
spoon.getEnvironment().setAutoImports(true);
spoon.getEnvironment().setNoClasspath(true);
CtModel model = spoon.buildModel();
//List<String> classnames= new ArrayList<String>();
// Interact with model
Factory factory = spoon.getFactory();
ClassFactory classFactory = factory.Class();
MethodFactory methodFactory = factory.Method();
List<methodcalls> methodcallsList = new ArrayList<methodcalls>();
for(CtType<?> clazz : classFactory.getAll()) {
for(CtMethod<?> method :clazz.getMethods()) {
String methname=method.getSimpleName();
System.out.println("CALLER METHOD=====>"+methname);
List<CtInvocation> methodcalls = method.getElements(new TypeFilter<>(CtInvocation.class));
for( CtInvocation calledmethod: methodcalls) {
//CALLEE EXECUTABLE
String CALLEENAME= calledmethod.getExecutable().getSignature().toString();
String methodCallee=calledmethod.getExecutable().toString();
if(calledmethod.getTarget()!=null) {
//CALLEE TARGET
String CALLEENAMETARGET= calledmethod.getTarget().toString();
System.out.println("TARGET: "+ CALLEENAMETARGET);
}
//CALLEE METHOD INFORMATION
ResultSet callingmethodsrefined2 = st.executeQuery("SELECT methods.* from methods where methods.methodname='"+CALLEENAME+"' ");
while(callingmethodsrefined2.next()) {
String CALLEECLASSNAME = callingmethodsrefined2.getString("classname");
String CALLEECLASSID = callingmethodsrefined2.getString("classid");
String CALLEEID = callingmethodsrefined2.getString("id");
System.out.println("CALLEE CLASS NAME: "+ CALLEECLASSNAME);
}
//CALLER METHOD INFORMATION
String CALLERCLASSNAME=clazz.getQualifiedName() ;
String CallerMethod= method.getSignature();
ResultSet callingmethodsrefined = st.executeQuery("SELECT methods.* from methods where methods.methodname='"+CallerMethod+"'and methods.classname='"+CALLERCLASSNAME+"'");
if(callingmethodsrefined.next()) {
String CallerMethodID = callingmethodsrefined.getString("id");
String CALLERCLASSNAME = callingmethodsrefined.getString("classname");
String CALLERCLASSID = callingmethodsrefined.getString("classid");
System.out.println("CALLEE METHOD ID: "+ CALLEEID);
}
String fullcaller= CALLERCLASSNAME+"."+CallerMethod;
String fullcallee= CALLEECLASSNAME+"."+CALLEENAME;
methodcalls methodcall= new methodcalls(CALLEEID, fullcaller, CALLEECLASSNAME, CallerMethodID, fullcallee, CALLERCLASSNAME);
if( methodcall.contains(methodcallsList, methodcall)==false && CallerMethodID!=null && CALLEEID!=null) {
String statement = "INSERT INTO `methodcalls`(`callermethodid`, `callername`, `callerclass`, `callerclassid`,`fullcaller`,`calleemethodid`, `calleename`, `calleeclass`, `calleeclassid`, `fullcallee`) VALUES ('"+CallerMethodID +"','" +CallerMethod+"','" +CALLERCLASSNAME+"','" +CALLERCLASSID+"','" +fullcaller+"','" +CALLEEID+"','" +CALLEENAME+"','" +CALLEECLASSNAME+"','" +CALLEECLASSID+"','" +fullcallee+"')";
st.executeUpdate(statement);
methodcallsList.add(methodcall);
}
}
}
}
问题是我无法检索被调用者所属的类,我只能检索方法签名,这是一个问题,因为可以在多个类中找到相同的方法(例如 isWhite()),因此当我仅根据方法名称查询数据库“方法”表时,我最终会得到多个结果。我尝试使用它来检索被调用者所属的类 String methodCallee=usedmethod.getExecutable().getClass().getSimpleName();但返回:“CtExecutableReferenceImpl”。
我遇到的另一个问题是我依赖 calledmethod.getExecutable() 来提取方法被调用者。但是, Calledmethod.getTarget() 本身可以包含一些被调用者,我打印了 Calledmethod.getTarget() 的内容,并为其中一个调用者获得了 TARGET: getLastPly().getDestination() 。如果目标由一系列被调用者组成,是否有办法将它们全部分开?另外,我需要知道每个“目标被调用者”所属的类列表,我对 Spoon 还很陌生,所以我真的不知道要使用哪些 API
最佳答案
I have tried using this to retrieve the class to which the callee belongs to String methodCallee=calledmethod.getExecutable().getClass().getSimpleName(); but that returned: "CtExecutableReferenceImpl".
我认为您正在寻找的方法实际上是getDeclaringType()
。此方法在 CtExecutable
接口(interface)中不可用,因此您必须检查您的可执行文件确实是 CtMethod
的实例,并且在转换它之后您可以使用 getDeclaringType
。
请参阅:http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/reflect/declaration/CtTypeMember.html#getDeclaringType--
我不确定您的第二个问题:您能否提供一个示例,这会有所帮助。
关于java - INRIA SPOON API MethodCalls - 检索 method.getExecutable() 和 method.getTarget() 的被调用者类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51460797/