1 18 19 package alt.jiapi.reflect; 20 21 import java.util.Iterator ; 22 import junit.framework.TestCase; 23 24 import alt.jiapi.reflect.instruction.Opcodes; 25 26 43 public class InstructionListTest extends TestCase { 44 private JiapiClass clazz1; 45 private JiapiClass clazz2; 46 47 public String getName() { 48 return "InstructionListTest: " + super.getName(); 49 } 50 51 public InstructionListTest(String name) { 52 super(name); 53 } 54 55 protected void setUp() { 56 try { 57 Loader loader = new Loader(); 58 clazz1 = loader.loadClass("java.net.URLClassLoader"); 59 clazz2 = loader.loadClass("java.net.URLClassLoader"); 60 } catch (Exception e) { 61 e.printStackTrace(); 62 } 63 } 64 65 67 public void testEquals() { 68 JiapiMethod[] methods = clazz1.getDeclaredMethods(); 69 for (int i = 0; i < methods.length; i++) { 70 InstructionList l1 = methods[i].getInstructionList(); 71 InstructionList l2 = methods[i].getInstructionList(); 72 73 assertEquals(l1, l2); 74 } 75 } 76 77 87 91 public void testAdd() { 92 JiapiMethod[] methods1 = clazz1.getDeclaredMethods(); 93 InstructionList il1 = methods1[0].getInstructionList(); 94 JiapiMethod[] methods2 = clazz2.getDeclaredMethods(); 95 InstructionList il2 = methods2[0].getInstructionList(); 96 il1.add(methods1[0].getInstructionFactory().pushConstant("sdgdfh")); 98 assertTrue(il1.size() > il2.size()); 99 } 100 101 104 public void testView() throws Exception { 105 JiapiMethod m = 106 clazz1.getDeclaredMethod("findClass", 107 new String [] { "java.lang.String" }); 108 assertNotNull(m); 109 InstructionList il = m.getInstructionList(); 110 111 int size = il.size(); 112 113 int div = size / 2; 116 InstructionList view1 = il.createView(0, div); 117 InstructionList view2 = il.createView(div, size); 118 119 InstructionList view3 = il.createView(div - 3, div + 3); 121 122 InstructionList view4 = view1.createView(div - 3, view1.size()); 125 InstructionList view5 = view3.createView(0, 3); 126 127 128 try { 130 InstructionList foo = il.createView(0, size + 1); 131 fail("should throw an IndexOutOfBoundsException: " + (size + 1) + 132 " > " + size); 133 } catch (IndexOutOfBoundsException ioobe) { 134 } 135 136 assertEquals(div, view1.size()); 138 assertEquals(size - div, view2.size()); 139 assertEquals(6, view3.size()); 140 assertEquals(3, view5.size()); 141 assertEquals(view4.size(), view5.size()); 142 143 assertTrue(!(il.equals(view1))); 146 assertTrue(!(view1.equals(view3))); 147 148 158 InstructionFactory factory = m.getInstructionFactory(); 160 int size1 = il.size(); 161 int size2 = view2.size(); 162 view2.insert(0, factory.pushNull()); 164 assertEquals(size1 + 1, il.size()); 165 assertEquals(size2 + 1, view2.size()); 166 view2.add(factory.pushNull()); 168 assertEquals(size1 + 2, il.size()); 169 assertEquals(size2 + 2, view2.size()); 170 171 } 181 182 public void testOpcodeSearch() { 184 int indexTestCount = 0; 185 int lastIndexTestCount = 0; 186 187 JiapiMethod[] methods = clazz1.getDeclaredMethods(); 188 for (int i = 0; i < methods.length; i++) { 189 InstructionList il = methods[i].getInstructionList(); 190 InstructionList view0 = il.createView(0); InstructionList view1 = il.createView(1); InstructionList view2 = il.createView(0, il.size() - 1); 194 int index = il.indexOf(Opcodes.INVOKEVIRTUAL); 195 int index0 = view0.indexOf(Opcodes.INVOKEVIRTUAL); 196 assertEquals(index, index0); 197 198 int index1 = view1.indexOf(Opcodes.INVOKEVIRTUAL); 199 int index2 = view2.indexOf(Opcodes.INVOKEVIRTUAL); 200 201 if (index > 0) { 202 assertEquals(Opcodes.INVOKEVIRTUAL, 204 il.get(index).getOpcode()); 205 assertEquals(Opcodes.INVOKEVIRTUAL, 206 view1.get(index1).getOpcode()); 207 208 assertEquals(index, index1); 210 indexTestCount++; 211 } 212 } 213 214 assertTrue(indexTestCount > 0); 216 } 217 218 219 public void testReplace() { 221 JiapiMethod[] methods = clazz1.getDeclaredMethods(); 222 JiapiMethod m = methods[0]; 223 224 InstructionList il = m.getInstructionList(); 225 InstructionList r = il.createEmptyList(); 226 InstructionFactory f = il.getInstructionFactory(); 227 228 if (il.stackUsage() == 0) { 229 r.add(f.pushNull()); } 231 232 try { 233 il.replace(0, il.size(), r); 235 fail("Should have thrown IllegalArgumentException"); 236 } 237 catch(IllegalArgumentException iae) { 238 } 239 240 r.replace(0, r.size(), r); 242 } 243 244 245 public void testStackUsage() { 246 JiapiMethod[] methods = clazz1.getDeclaredMethods(); 247 for (int i = 0; i < methods.length; i++) { 248 InstructionList il = methods[i].getInstructionList(); 249 if (il != null) { 250 System.out.println(il.stackUsage()); 251 } 253 } 254 } 255 } 256 257 | Popular Tags |