1 19 20 25 26 27 28 29 30 31 package soot.jimple.toolkits.base; 32 import soot.options.*; 33 34 import soot.*; 35 import soot.toolkits.scalar.*; 36 import soot.jimple.*; 37 import soot.toolkits.graph.*; 38 import soot.grimp.*; 39 import soot.util.*; 40 import java.util.*; 41 import soot.tagkit.*; 42 43 public class PartialConstructorFolder extends BodyTransformer 44 { 45 48 private List types; 49 50 public void setTypes(List t){ 51 types = t; 52 } 53 54 public List getTypes(){ 55 return types; 56 } 57 58 60 61 public void internalTransform(Body b, String phaseName, Map options) 62 { 63 JimpleBody body = (JimpleBody)b; 64 65 if(Options.v().verbose()) 66 G.v().out.println("[" + body.getMethod().getName() + 67 "] Folding Jimple constructors..."); 68 69 Chain units = body.getUnits(); 70 List stmtList = new ArrayList(); 71 stmtList.addAll(units); 72 73 Iterator it = stmtList.iterator(); 74 Iterator nextStmtIt = stmtList.iterator(); 75 nextStmtIt.next(); 77 78 ExceptionalUnitGraph graph = new ExceptionalUnitGraph(body); 79 80 LocalDefs localDefs = new SmartLocalDefs(graph, new SimpleLiveLocals(graph)); 81 LocalUses localUses = new SimpleLocalUses(graph, localDefs); 82 83 84 while (it.hasNext()) 85 { 86 Stmt s = (Stmt)it.next(); 87 88 89 if (!(s instanceof AssignStmt)) 90 continue; 91 92 93 Value lhs = ((AssignStmt)s).getLeftOp(); 95 if (!(lhs instanceof Local)) 96 continue; 97 98 Value rhs = ((AssignStmt)s).getRightOp(); 99 if (!(rhs instanceof NewExpr)) 100 continue; 101 102 103 if (nextStmtIt.hasNext()){ 107 Stmt next = (Stmt)nextStmtIt.next(); 108 if (next instanceof InvokeStmt){ 109 InvokeStmt invoke = (InvokeStmt)next; 110 111 if (invoke.getInvokeExpr() instanceof SpecialInvokeExpr) { 112 SpecialInvokeExpr invokeExpr = (SpecialInvokeExpr)invoke.getInvokeExpr(); 113 if (invokeExpr.getBase() == lhs){ 114 break; 115 } 116 } 117 } 118 } 119 120 121 if (!types.contains(((NewExpr)rhs).getType())) continue; 123 124 List lu = localUses.getUsesOf((DefinitionStmt)s); 125 Iterator luIter = lu.iterator(); 126 boolean MadeNewInvokeExpr = false; 127 128 while (luIter.hasNext()) 129 { 130 Unit use = ((UnitValueBoxPair)(luIter.next())).unit; 131 if (!(use instanceof InvokeStmt)) 132 continue; 133 InvokeStmt is = (InvokeStmt)use; 134 if (!(is.getInvokeExpr() instanceof SpecialInvokeExpr) || 135 lhs != ((SpecialInvokeExpr)is.getInvokeExpr()).getBase()) 136 continue; 137 138 AssignStmt constructStmt = Jimple.v().newAssignStmt 140 (((DefinitionStmt)s).getLeftOp(), ((DefinitionStmt)s).getRightOp()); 141 constructStmt.setRightOp 142 (Jimple.v().newNewExpr 143 (((NewExpr)rhs).getBaseType())); 144 MadeNewInvokeExpr = true; 145 146 use.redirectJumpsToThisTo(constructStmt); 148 units.insertBefore(constructStmt, use); 150 151 constructStmt.addTag((SourceLnPosTag)s.getTag("SourceLnPosTag")); 152 } 153 if (MadeNewInvokeExpr) 154 { 155 units.remove(s); 156 } 157 } 158 } 159 } 160 | Popular Tags |