1 19 20 package soot.dava.toolkits.base.AST.transformations; 21 22 import soot.*; 23 import soot.dava.*; 24 import soot.tagkit.*; 25 import soot.jimple.*; 26 import soot.dava.internal.AST.*; 29 import soot.dava.internal.asg.*; 30 import soot.dava.internal.javaRep.*; 31 import soot.dava.toolkits.base.AST.analysis.*; 32 35 36 import java.util.*; 37 38 41 42 43 47 48 78 79 80 public class DeInliningFinalFields extends DepthFirstAdapter{ 81 SootClass sootClass=null; 82 SootMethod sootMethod=null; 83 DavaBody davaBody=null; 84 85 HashMap finalFields; 86 87 89 public DeInliningFinalFields(){ 90 } 91 public DeInliningFinalFields(boolean verbose){ 92 super(verbose); 93 } 94 95 96 public void inASTMethodNode(ASTMethodNode node){ 97 DavaBody davaBody = node.getDavaBody(); 98 sootMethod = davaBody.getMethod(); 99 sootClass = sootMethod.getDeclaringClass(); 100 101 finalFields = new HashMap(); 102 103 Iterator fieldIt = sootClass.getFields().iterator(); 104 while(fieldIt.hasNext()){ 105 SootField f = (SootField)fieldIt.next(); 106 if(f.isFinal()){ 107 108 Type fieldType = f.getType(); 110 if(fieldType instanceof DoubleType && f.hasTag("DoubleConstantValueTag")){ 111 double val = ((DoubleConstantValueTag)f.getTag("DoubleConstantValueTag")).getDoubleValue(); 112 finalFields.put(new Double (val),f); 113 } 114 else if (fieldType instanceof FloatType && f.hasTag("FloatConstantValueTag")){ 115 float val = ((FloatConstantValueTag)f.getTag("FloatConstantValueTag")).getFloatValue(); 116 finalFields.put(new Float (val),f); 117 } 118 else if (fieldType instanceof LongType && f.hasTag("LongConstantValueTag")){ 119 long val = ((LongConstantValueTag)f.getTag("LongConstantValueTag")).getLongValue(); 120 finalFields.put(new Long (val),f); 121 } 122 else if (fieldType instanceof CharType && f.hasTag("IntegerConstantValueTag")){ 123 int val = ((IntegerConstantValueTag)f.getTag("IntegerConstantValueTag")).getIntValue(); 124 finalFields.put(new Integer (val),f); 125 } 126 else if (fieldType instanceof BooleanType && f.hasTag("IntegerConstantValueTag")){ 127 int val = ((IntegerConstantValueTag)f.getTag("IntegerConstantValueTag")).getIntValue(); 128 if (val ==0) 129 finalFields.put(new Boolean (false),f); 130 else 131 finalFields.put(new Boolean (true),f); 132 } 133 else if ( (fieldType instanceof IntType || fieldType instanceof ByteType || fieldType instanceof ShortType) && 134 f.hasTag("IntegerConstantValueTag")){ 135 int val = ((IntegerConstantValueTag)f.getTag("IntegerConstantValueTag")).getIntValue(); 136 finalFields.put(new Integer (val),f); 137 } 138 else if(f.hasTag("StringConstantValueTag")){ 139 String val = ((StringConstantValueTag)f.getTag("StringConstantValueTag")).getStringValue(); 140 finalFields.put(val,f); 142 } 143 } } } 146 147 148 149 150 157 private boolean isConstant(Value val){ 158 if(val instanceof StringConstant || val instanceof DoubleConstant || 159 val instanceof FloatConstant || val instanceof IntConstant || val instanceof LongConstant){ 160 return true; 161 } 162 return false; 163 } 164 165 166 167 168 169 172 public void inASTSynchronizedBlockNode(ASTSynchronizedBlockNode node){ 173 } 175 176 177 178 public void checkAndSwitch(ValueBox valBox){ 179 Value val =valBox.getValue(); 180 181 Object finalField = check(val); 182 if(finalField!=null){ 183 valBox.setValue(new DStaticFieldRef(((SootField)finalField).makeRef(),true)); 185 } 186 } 189 190 191 192 public Object check(Value val){ 193 Object finalField=null; 194 if(isConstant(val)){ 195 197 if(val instanceof StringConstant){ 199 String myString = ((StringConstant)val).toString(); 200 myString = myString.substring(1,myString.length()-1); 201 finalField = finalFields.get(myString); 203 } 204 else if(val instanceof DoubleConstant){ 205 String myString = ((DoubleConstant)val).toString(); 206 207 finalField = finalFields.get(new Double (myString)); 208 } 209 else if(val instanceof FloatConstant){ 210 String myString = ((FloatConstant)val).toString(); 211 212 finalField = finalFields.get(new Float (myString)); 213 } 214 else if(val instanceof LongConstant){ 215 String myString = ((LongConstant)val).toString(); 216 217 finalField = finalFields.get(new Long (myString.substring(0,myString.length()-1))); 218 } 219 else if(val instanceof IntConstant){ 220 String myString = ((IntConstant)val).toString(); 221 if(myString.length()==0) 222 return null; 223 224 Type valType = ((IntConstant)val).getType(); 225 226 227 boolean charact=false; 228 Integer myInt=null; 229 try{ 230 if(myString.charAt(0)=='\''){ if(myString.length()<2) 232 return null; 233 234 myInt = new Integer ((int)myString.charAt(1)); 235 } 236 else 237 myInt = new Integer (myString); 238 } 239 catch(Exception e){ 240 return finalField; 242 } 243 244 245 if(valType instanceof ByteType){ 246 finalField = finalFields.get(myInt); 247 } 248 else if(valType instanceof IntType){ 249 if(myString.equals("false")) 250 finalField = finalFields.get(new Boolean (false)); 251 else if(myString.equals("true")) 252 finalField = finalFields.get(new Boolean (true)); 253 else { 254 finalField = finalFields.get(myInt); 255 } 256 } 257 else if(valType instanceof ShortType){ 258 finalField = finalFields.get(myInt); 259 } 260 } 261 } 262 return finalField; 263 } 264 265 266 267 268 269 270 271 272 273 274 281 public void inASTSwitchNode(ASTSwitchNode node){ 282 Value val = (Value)node.get_Key(); 283 284 if(isConstant(val)){ 285 288 checkAndSwitch(node.getKeyBox()); 289 return; 290 } 291 293 Iterator it = val.getUseBoxes().iterator(); 294 while(it.hasNext()){ 295 ValueBox tempBox = (ValueBox)it.next(); 296 checkAndSwitch(tempBox); 298 } 299 } 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 public void inASTStatementSequenceNode(ASTStatementSequenceNode node){ 315 List statements = node.getStatements(); 316 Iterator it = statements.iterator(); 317 318 while(it.hasNext()){ 319 AugmentedStmt as = (AugmentedStmt)it.next(); 320 Stmt s = as.get_Stmt(); 321 Iterator tempIt = s.getUseBoxes().iterator(); 322 while(tempIt.hasNext()){ 323 ValueBox tempBox = (ValueBox)tempIt.next(); 324 checkAndSwitch(tempBox); 326 } 327 } 328 } 329 330 331 332 333 334 335 336 337 public void inASTForLoopNode(ASTForLoopNode node){ 338 339 List init = node.getInit(); 341 Iterator it = init.iterator(); 342 while(it.hasNext()){ 343 AugmentedStmt as = (AugmentedStmt)it.next(); 344 Stmt s = as.get_Stmt(); 345 Iterator tempIt = s.getUseBoxes().iterator(); 346 while(tempIt.hasNext()){ 347 ValueBox tempBox = (ValueBox)tempIt.next(); 348 checkAndSwitch(tempBox); 350 } 351 } 352 353 ASTCondition cond = node.get_Condition(); 355 checkConditionalUses(cond,node); 356 357 358 List update = node.getUpdate(); 360 it = update.iterator(); 361 while(it.hasNext()){ 362 AugmentedStmt as = (AugmentedStmt)it.next(); 363 Stmt s = as.get_Stmt(); 364 Iterator tempIt = s.getUseBoxes().iterator(); 365 while(tempIt.hasNext()){ 366 ValueBox tempBox = (ValueBox)tempIt.next(); 367 checkAndSwitch(tempBox); 369 } 370 } 371 } 372 373 374 375 378 public void checkConditionalUses(Object cond,ASTNode node){ 379 if(cond instanceof ASTAggregatedCondition){ 380 checkConditionalUses((((ASTAggregatedCondition)cond).getLeftOp()),node); 381 checkConditionalUses(((ASTAggregatedCondition)cond).getRightOp(),node ); 382 return; 383 } 384 else if(cond instanceof ASTBinaryCondition){ 385 Value val = ((ASTBinaryCondition)cond).getConditionExpr(); 387 Iterator tempIt = val.getUseBoxes().iterator(); 388 while(tempIt.hasNext()){ 389 ValueBox tempBox = (ValueBox)tempIt.next(); 390 checkAndSwitch(tempBox); 392 } 393 } 394 } 395 396 397 398 399 400 401 402 403 407 public void inASTIfNode(ASTIfNode node){ 408 ASTCondition cond = node.get_Condition(); 409 checkConditionalUses(cond,node); 410 } 411 412 413 417 public void inASTIfElseNode(ASTIfElseNode node){ 418 ASTCondition cond = node.get_Condition(); 419 checkConditionalUses(cond,node); 420 } 421 422 423 424 425 429 public void inASTWhileNode(ASTWhileNode node){ 430 ASTCondition cond = node.get_Condition(); 431 checkConditionalUses(cond,node); 432 } 433 434 435 436 440 public void inASTDoWhileNode(ASTDoWhileNode node){ 441 ASTCondition cond = node.get_Condition(); 442 checkConditionalUses(cond,node); 443 } 444 } | Popular Tags |