1 19 20 package soot.javaToJimple; 21 22 import soot.*; 23 import java.util.*; 24 25 public class LocalGenerator{ 26 27 private soot.Body body; 28 public LocalGenerator(Body b){ 29 body = b; 30 } 31 32 private boolean bodyContainsLocal(String name){ 33 Iterator it = body.getLocals().iterator(); 34 while (it.hasNext()){ 35 if (((soot.Local)it.next()).getName().equals(name)) return true; 36 } 37 return false; 38 } 39 40 43 public soot.Local generateLocal(soot.Type type){ 44 45 String name = "v"; 46 if (type instanceof soot.IntType) { 47 while (true){ 48 name = nextIntName(); 49 if (!bodyContainsLocal(name)) break; 50 } 51 } 52 else if (type instanceof soot.ByteType) { 53 while (true){ 54 name = nextByteName(); 55 if (!bodyContainsLocal(name)) break; 56 } 57 } 58 else if (type instanceof soot.ShortType) { 59 while (true){ 60 name = nextShortName(); 61 if (!bodyContainsLocal(name)) break; 62 } 63 } 64 else if (type instanceof soot.BooleanType) { 65 while (true){ 66 name = nextBooleanName(); 67 if (!bodyContainsLocal(name)) break; 68 } 69 } 70 else if (type instanceof soot.VoidType) { 71 while (true){ 72 name = nextVoidName(); 73 if (!bodyContainsLocal(name)) break; 74 } 75 } 76 else if (type instanceof soot.CharType) { 77 while (true){ 78 name = nextCharName(); 79 if (!bodyContainsLocal(name)) break; 80 } 81 type = soot.CharType.v(); 82 } 83 else if (type instanceof soot.DoubleType) { 84 while (true){ 85 name = nextDoubleName(); 86 if (!bodyContainsLocal(name)) break; 87 } 88 } 89 else if (type instanceof soot.FloatType) { 90 while (true){ 91 name = nextFloatName(); 92 if (!bodyContainsLocal(name)) break; 93 } 94 } 95 else if (type instanceof soot.LongType) { 96 while (true){ 97 name = nextLongName(); 98 if (!bodyContainsLocal(name)) break; 99 } 100 } 101 else if (type instanceof soot.RefLikeType) { 102 while (true){ 103 name = nextRefLikeTypeName(); 104 if (!bodyContainsLocal(name)) break; 105 } 106 } 107 else { 108 throw new RuntimeException ("Unhandled Type of Local variable to Generate - Not Implemented"); 109 } 110 111 return createLocal(name, type); 112 113 } 114 115 private int tempInt = -1; 116 private int tempVoid = -1; 117 private int tempBoolean = -1; 118 private int tempLong = -1; 119 private int tempDouble = -1; 120 private int tempFloat = -1; 121 private int tempRefLikeType = -1; 122 private int tempByte = -1; 123 private int tempShort = -1; 124 private int tempChar = -1; 125 126 private String nextIntName(){ 127 tempInt++; 128 return "$i"+tempInt; 129 } 130 131 private String nextCharName(){ 132 tempChar++; 133 return "$c"+tempChar; 134 } 135 136 private String nextVoidName(){ 137 tempVoid++; 138 return "$v"+tempVoid; 139 } 140 141 private String nextByteName(){ 142 tempByte++; 143 return "$b"+tempByte; 144 } 145 146 private String nextShortName(){ 147 tempShort++; 148 return "$s"+tempShort; 149 } 150 151 private String nextBooleanName(){ 152 tempBoolean++; 153 return "$z"+tempBoolean; 154 } 155 156 private String nextDoubleName(){ 157 tempDouble++; 158 return "$d"+tempDouble; 159 } 160 161 private String nextFloatName(){ 162 tempFloat++; 163 return "$f"+tempFloat; 164 } 165 166 private String nextLongName(){ 167 tempLong++; 168 return "$l"+tempLong; 169 } 170 171 private String nextRefLikeTypeName(){ 172 tempRefLikeType++; 173 return "$r"+tempRefLikeType; 174 } 175 176 private soot.Local createLocal(String name, soot.Type sootType) { 178 soot.Local sootLocal = soot.jimple.Jimple.v().newLocal(name, sootType); 179 body.getLocals().add(sootLocal); 180 return sootLocal; 181 } 182 } 183 | Popular Tags |