You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DomTreeTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package test.javassist.bytecode.analysis;
  2. import javassist.ClassPool;
  3. import javassist.bytecode.analysis.ControlFlow;
  4. import javassist.bytecode.analysis.ControlFlow.Block;
  5. import javassist.bytecode.analysis.ControlFlow.Node;
  6. import junit.framework.TestCase;
  7. public class DomTreeTest extends TestCase {
  8. private ClassPool pool = ClassPool.getDefault();
  9. public void testDomtree() throws Exception {
  10. ControlFlow cf = new ControlFlow(pool.get(DomTreeTest.class.getName()).getDeclaredMethod("test1"));
  11. Block[] blocks = cf.basicBlocks();
  12. // for (int i = 0; i < blocks.length; i++)
  13. // System.out.println(i + ": " + blocks[i]);
  14. testBlock(blocks[0], new int[] {}, new int[] { 11, 6 } );
  15. testBlock(blocks[1], new int[] { 0 }, new int[] { 17, 11 } );
  16. testBlock(blocks[2], new int[] { 0, 6 }, new int[] { 19, 17 });
  17. testBlock(blocks[3], new int[] { 6, 11 }, new int[] { 19 });
  18. testBlock(blocks[4], new int[] { 11, 17 }, new int[] {});
  19. Node[] dom = cf.dominatorTree();
  20. assertNull(dom[0].parent());
  21. assertEquals(0, dom[1].parent().block().position());
  22. assertEquals(0, dom[2].parent().block().position());
  23. assertEquals(0, dom[3].parent().block().position());
  24. assertEquals(0, dom[4].parent().block().position());
  25. Node[] pdom = cf.postDominatorTree();
  26. assertEquals(19, pdom[0].parent().block().position());
  27. assertEquals(19, pdom[1].parent().block().position());
  28. assertEquals(19, pdom[2].parent().block().position());
  29. assertEquals(19, pdom[3].parent().block().position());
  30. assertNull(pdom[4].parent());
  31. }
  32. private void testBlock(Block b, int[] incoming, int[] outgoing) {
  33. assertEquals(incoming.length, b.incomings());
  34. int i = 0;
  35. for (int index: incoming)
  36. assertEquals(index, b.incoming(i++).position());
  37. i = 0;
  38. assertEquals(outgoing.length, b.exits());
  39. for (int index: outgoing)
  40. assertEquals(index, b.exit(i++).position());
  41. }
  42. private void testNode(Node n, int[] incoming, int[] outgoing) {
  43. int i = 0;
  44. for (int index: incoming)
  45. assertEquals(index, n.parent().block().index());
  46. }
  47. public void test1(){
  48. int k=0;
  49. if (k != 0 && k!=2 || k < 7) {
  50. k = 3 ;
  51. }
  52. }
  53. public void testDomtree2() throws Exception {
  54. ControlFlow cf = new ControlFlow(pool.get(DomTreeTest.class.getName()).getDeclaredMethod("test2"));
  55. Block[] blocks = cf.basicBlocks();
  56. // for (int i = 0; i < blocks.length; i++)
  57. // System.out.println(i + ": " + blocks[i]);
  58. testBlock(blocks[0], new int[] { 7 }, new int[] { 14, 7 } );
  59. testBlock(blocks[1], new int[] { 0 }, new int[] { 0, 12 } );
  60. testBlock(blocks[2], new int[] { 7 }, new int[] {});
  61. testBlock(blocks[3], new int[] { 0 }, new int[] {});
  62. Node[] dom = cf.dominatorTree();
  63. assertNull(dom[0].parent());
  64. assertEquals(0, dom[1].parent().block().position());
  65. assertEquals(7, dom[2].parent().block().position());
  66. assertEquals(0, dom[3].parent().block().position());
  67. Node[] pdom = cf.postDominatorTree();
  68. assertNull(pdom[0].parent());
  69. assertNull(pdom[1].parent());
  70. assertNull(pdom[2].parent());
  71. assertNull(pdom[3].parent());
  72. }
  73. public int test2(int i){
  74. while (i-- > 0)
  75. if (i == 3)
  76. return 1;
  77. return i + 3;
  78. }
  79. public void testDomtree3() throws Exception {
  80. ControlFlow cf = new ControlFlow(pool.get(DomTreeTest.class.getName()).getDeclaredMethod("test3"));
  81. Block[] blocks = cf.basicBlocks();
  82. for (int i = 0; i < blocks.length; i++)
  83. System.out.println(blocks[i]);
  84. }
  85. public int test3(int i, int j) {
  86. while (i > 0) {
  87. try {
  88. j++;
  89. }
  90. catch (Throwable t) {
  91. j = 0;
  92. }
  93. i--;
  94. }
  95. return j;
  96. }
  97. }