作者:大师傅放放风_769 | 来源:互联网 | 2023-05-19 15:16
So I have a Method
所以我有一个方法
public modifiers Foo foo(Bar bar){
blah;
blah;
veryInterestingStmt;
moreBlah();
return XYZ;
}
I now want to split this method s.t. everything in its body is extracted into a separate method (programmatically).
我现在想要分割这个方法s.t.其身体中的所有内容都被提取到一个单独的方法中(以编程方式)。
I.e.
即
public modifiers Foo foo(Bar bar){
return trulyFoo(bar);
}
public modifiers Foo trulyFoo(Bar bar){
blah;
blah;
veryInterestingStmt;
moreBlah();
return XYZ;
}
How do I do that, though?
但是我该怎么做呢?
The naive
天真
private void fracture(SootMethod sm) {
SootClass sc = sm.getDeclaringClass();
String auxMethodName = sm.getName() + FRACTURE_SUFFIX;
Type auxReturnType = sm.getReturnType();
ListauxParamTypes = new LinkedList<>(sm.getParameterTypes());
int auxModifiers = sm.getModifiers();
SootMethod auxMethod = sc.addMethod(new SootMethod(auxMethodName,auxParamTypes,auxReturnType,auxModifiers));
Body body = sm.getActiveBody();
Body auxBody = Jimple.v().newBody(auxMethod);
auxMethod.setActiveBody(auxBody);
for(Local l : body.getLocals()){
auxBody.getLocals().add(l);
}
PatchingChain units = body.getUnits();
PatchingChain auxUnits = auxBody.getUnits();
Iterator it = body.getUnits().snapshotIterator();
boolean passedFirstNOnidentity= false;
while(it.hasNext()){
Stmt stmt = (Stmt) it.next();
if(!passedFirstNonidentity && !(stmt instanceof IdentityStmt)) {
passedFirstNOnidentity= true;
//TODO: if added more parameters than original method had, add their identity stmts here
}
auxUnits.add(stmt);
// if(passedFirstNonidentity) units.remove(stmt); //TODO: uncomment this and later add call to {@code auxMethod}
}
}
}
Doesn't work. If I run, say
不起作用。如果我跑,说
DirectedGraph dg = new ExceptionalUnitGraph(auxMethod.getActiveBody());
I get a
我得到了
java.lang.RuntimeException: Unit graph contains jump to non-existing target
at soot.toolkits.graph.UnitGraph.buildUnexceptionalEdges(UnitGraph.java:128)
at soot.toolkits.graph.ExceptionalUnitGraph.initialize(ExceptionalUnitGraph.java:258)
at soot.toolkits.graph.ExceptionalUnitGraph.(ExceptionalUnitGraph.java:159)
at soot.toolkits.graph.ExceptionalUnitGraph.(ExceptionalUnitGraph.java:192)
2 个解决方案