1 19 20 package soot.dava.toolkits.base.AST; 21 22 import soot.*; 23 import java.util.*; 24 import soot.util.*; 25 import soot.jimple.*; 26 import soot.dava.internal.AST.*; 27 28 public class TryContentsFinder extends ASTAnalysis 29 { 30 public TryContentsFinder( Singletons.Global g ) {} 31 public static TryContentsFinder v() { return G.v().soot_dava_toolkits_base_AST_TryContentsFinder(); } 32 33 private IterableSet curExceptionSet = new IterableSet(); 34 private HashMap node2ExceptionSet = new HashMap(); 35 36 public int getAnalysisDepth() 37 { 38 return ANALYSE_VALUES; 39 } 40 41 public IterableSet remove_CurExceptionSet() 42 { 43 IterableSet s = curExceptionSet; 44 45 set_CurExceptionSet( new IterableSet()); 46 47 return s; 48 } 49 50 public void set_CurExceptionSet( IterableSet curExceptionSet) 51 { 52 this.curExceptionSet = curExceptionSet; 53 } 54 55 public void analyseThrowStmt( ThrowStmt s) 56 { 57 Value op = ((ThrowStmt) s).getOp(); 58 59 if (op instanceof Local) 60 add_ThrownType( ((Local) op).getType()); 61 else if (op instanceof FieldRef) 62 add_ThrownType( ((FieldRef) op).getType()); 63 } 64 65 private void add_ThrownType( Type t) 66 { 67 if (t instanceof RefType) 68 curExceptionSet.add( ((RefType) t).getSootClass()); 69 } 70 71 public void analyseInvokeExpr( InvokeExpr ie) 72 { 73 curExceptionSet.addAll( ie.getMethod().getExceptions()); 74 } 75 76 public void analyseInstanceInvokeExpr( InstanceInvokeExpr iie) 77 { 78 analyseInvokeExpr( iie); 79 } 80 81 public void analyseASTNode( ASTNode n) 82 { 83 if (n instanceof ASTTryNode) { 84 85 ASTTryNode tryNode = (ASTTryNode) n; 86 87 ArrayList toRemove = new ArrayList(); 88 IterableSet tryExceptionSet = (IterableSet) node2ExceptionSet.get( tryNode.get_TryBodyContainer()); 89 if (tryExceptionSet == null) { 90 tryExceptionSet = new IterableSet(); 91 node2ExceptionSet.put( tryNode.get_TryBodyContainer(), tryExceptionSet); 92 } 93 94 List catchBodies = tryNode.get_CatchList(); 95 List subBodies = tryNode.get_SubBodies(); 96 97 Iterator cit = catchBodies.iterator(); 98 while (cit.hasNext()) { 99 Object catchBody = cit.next(); 100 SootClass exception = (SootClass) tryNode.get_ExceptionMap().get( catchBody); 101 102 if ((catches_Exception( tryExceptionSet, exception) == false) && (catches_RuntimeException( exception) == false)) 103 toRemove.add( catchBody); 104 } 105 106 Iterator trit = toRemove.iterator(); 107 while (trit.hasNext()) { 108 Object catchBody = trit.next(); 109 110 subBodies.remove( catchBody); 111 catchBodies.remove( catchBody); 112 } 113 114 IterableSet passingSet = (IterableSet) tryExceptionSet.clone(); 115 cit = catchBodies.iterator(); 116 while (cit.hasNext()) 117 passingSet.remove( tryNode.get_ExceptionMap().get( cit.next())); 118 119 cit = catchBodies.iterator(); 120 while (cit.hasNext()) 121 passingSet.addAll( get_ExceptionSet( cit.next())); 122 123 node2ExceptionSet.put( n, passingSet); 124 } 125 126 else { 127 Iterator sbit = n.get_SubBodies().iterator(); 128 while (sbit.hasNext()) { 129 Iterator it = ((List) sbit.next()).iterator(); 130 while (it.hasNext()) 131 add_ExceptionSet( n, get_ExceptionSet( it.next())); 132 } 133 } 134 135 136 remove_CurExceptionSet(); 137 } 138 139 public IterableSet get_ExceptionSet( Object node) 140 { 141 IterableSet fullSet = (IterableSet) node2ExceptionSet.get( node); 142 if (fullSet == null) { 143 fullSet = new IterableSet(); 144 node2ExceptionSet.put( node, fullSet); 145 } 146 147 return fullSet; 148 } 149 150 public void add_ExceptionSet( Object node, IterableSet s) 151 { 152 IterableSet fullSet = (IterableSet) node2ExceptionSet.get( node); 153 if (fullSet == null) { 154 fullSet = new IterableSet(); 155 node2ExceptionSet.put( node, fullSet); 156 } 157 158 fullSet.addAll( s); 159 } 160 161 private boolean catches_Exception( IterableSet tryExceptionSet, SootClass c) 162 { 163 Iterator it = tryExceptionSet.iterator(); 164 while (it.hasNext()) { 165 SootClass thrownException = (SootClass) it.next(); 166 167 while (true) { 168 if (thrownException == c) 169 return true; 170 171 if (thrownException.hasSuperclass() == false) 172 break; 173 174 thrownException = thrownException.getSuperclass(); 175 } 176 } 177 178 return false; 179 } 180 181 private boolean catches_RuntimeException( SootClass c) 182 { 183 if ((c == Scene.v().getSootClass( "java.lang.Throwable")) || 184 (c == Scene.v().getSootClass( "java.lang.Exception"))) 185 return true; 186 187 SootClass 188 caughtException = c, 189 runtimeException = Scene.v().getSootClass( "java.lang.RuntimeException"); 190 191 while (true) { 192 if (caughtException == runtimeException) 193 return true; 194 195 if (caughtException.hasSuperclass() == false) 196 return false; 197 198 caughtException = caughtException.getSuperclass(); 199 } 200 } 201 } 202 | Popular Tags |