Newer
Older
MagnetRON / src / org / ntlab / trace / TracePoint.java
  1. package org.ntlab.trace;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class TracePoint {
  6. private MethodExecution methodExecution;
  7. private int order = 0;
  8. public TracePoint(MethodExecution methodExecution, int order) {
  9. this.methodExecution = methodExecution;
  10. this.order = order;
  11. }
  12. public TracePoint duplicate() {
  13. return new TracePoint(methodExecution, order);
  14. }
  15. public Statement getStatement() {
  16. return methodExecution.getStatements().get(order);
  17. }
  18. public MethodExecution getMethodExecution() {
  19. return methodExecution;
  20. }
  21. public ArrayList<MethodExecution> getPreviouslyCalledMethods() {
  22. ArrayList<MethodExecution> children = new ArrayList<MethodExecution>();
  23. ArrayList<Statement> statements = methodExecution.getStatements();
  24. for (int i = 0; i < order; i++) {
  25. Statement statement = statements.get(i);
  26. if (statement instanceof MethodInvocation) {
  27. MethodExecution child = ((MethodInvocation)statement).getCalledMethodExecution();
  28. children.add(child);
  29. }
  30. }
  31. return children;
  32. }
  33. /**
  34. * 順方向の全探索(ただし、実行文が記録されていないメソッド実行には潜らない)
  35. * @return false: これ以上辿れない場合, true: それ以外
  36. */
  37. public boolean stepFull() {
  38. if (getStatement() instanceof MethodInvocation) {
  39. MethodExecution calledMethodExecution = ((MethodInvocation)getStatement()).getCalledMethodExecution();
  40. if (calledMethodExecution.getStatements().size() > 0) {
  41. methodExecution = calledMethodExecution;
  42. order = 0;
  43. return true;
  44. }
  45. }
  46. while (order >= methodExecution.getStatements().size() - 1) {
  47. order = methodExecution.getCallerStatementExecution();
  48. methodExecution = methodExecution.getCallerMethodExecution();
  49. if (methodExecution == null) {
  50. order = -1;
  51. return false;
  52. }
  53. }
  54. order++;
  55. return true;
  56. }
  57. /**
  58. * 逆方向の全探索(ただし、実行文が記録されていないメソッド実行には潜らない。また、呼び出し先のメソッド実行内を探索した後に、呼び出し元のメソッド呼び出し文を訪問するので注意!!)
  59. * @return false: これ以上辿れない場合, true: それ以外
  60. */
  61. public boolean stepBackFull() {
  62. if (order <= 0) {
  63. order = methodExecution.getCallerStatementExecution();
  64. methodExecution = methodExecution.getCallerMethodExecution();
  65. if (methodExecution == null) {
  66. order = -1;
  67. return false;
  68. }
  69. return true;
  70. }
  71. order--;
  72. while (getStatement() instanceof MethodInvocation) {
  73. MethodExecution calledMethodExecution = ((MethodInvocation)getStatement()).getCalledMethodExecution();
  74. if (calledMethodExecution.getStatements().size() == 0) break;
  75. methodExecution = calledMethodExecution;
  76. order = methodExecution.getStatements().size() - 1;
  77. }
  78. return true;
  79. }
  80. /**
  81. * 順方向に探索する。呼び出し元に戻るが呼び出し先には潜らない。
  82. * @return false: 呼び出し元に戻った場合またはこれ以上辿れない場合, true: それ以外
  83. */
  84. public boolean stepOver() {
  85. if (order < methodExecution.getStatements().size() - 1) {
  86. order++;
  87. return true;
  88. }
  89. order = methodExecution.getCallerStatementExecution();
  90. methodExecution = methodExecution.getCallerMethodExecution();
  91. if (methodExecution == null) {
  92. order = -1;
  93. return false;
  94. }
  95. return false;
  96. }
  97. /**
  98. * 逆方向に探索する。呼び出し元に戻るが呼び出し先には潜らない。
  99. * @return false: 呼び出し元に戻った場合またはこれ以上辿れない場合, true: それ以外
  100. */
  101. public boolean stepBackOver() {
  102. if (order > 0) {
  103. order--;
  104. return true;
  105. }
  106. order = methodExecution.getCallerStatementExecution();
  107. methodExecution = methodExecution.getCallerMethodExecution();
  108. if (methodExecution == null) {
  109. order = -1;
  110. return false;
  111. }
  112. return false;
  113. }
  114. /**
  115. * 順方向に探索する。呼び出し先を辿るが呼び出し元には戻らない。
  116. * @return false: 呼び出し先に移った場合またはこれ以上辿れない場合, true: それ以外
  117. */
  118. public boolean stepNoReturn() {
  119. if (getStatement() instanceof MethodInvocation) {
  120. methodExecution = ((MethodInvocation)getStatement()).getCalledMethodExecution();
  121. if (methodExecution.getStatements().size() > 0) {
  122. order = 0;
  123. } else {
  124. order = -1; // 呼び出し先で実行文が記録されていない場合
  125. }
  126. return false;
  127. }
  128. order++;
  129. if (order < methodExecution.getStatements().size()) {
  130. return true;
  131. }
  132. return false;
  133. }
  134. /**
  135. * 逆方向に探索する。呼び出し先を辿るが呼び出し元には戻らない。(先に呼び出し元のメソッド呼び出し文を訪問してから呼び出し先のメソッド実行を訪問するので注意!!)
  136. * @return false: 呼び出し先に移った場合またはこれ以上辿れない場合, true: それ以外
  137. */
  138. public boolean stepBackNoReturn() {
  139. if (getStatement() instanceof MethodInvocation) {
  140. methodExecution = ((MethodInvocation)getStatement()).getCalledMethodExecution();
  141. order = methodExecution.getStatements().size() - 1; // -1 になる場合もある(呼び出し先で実行文が記録されていない場合)
  142. return false;
  143. }
  144. order--;
  145. if (order >= 0) {
  146. return true;
  147. }
  148. return false;
  149. }
  150. /**
  151. * 同じメソッド内で順方向に1つ進める。呼び出し先にも呼び出し元にも行かない。
  152. * @return false:メソッドを抜け出た場合, true: それ以外
  153. */
  154. public boolean stepNext() {
  155. order++;
  156. return (order < methodExecution.getStatements().size());
  157. }
  158. public boolean isValid() {
  159. if (methodExecution == null || order == -1 || order >= methodExecution.getStatements().size()) return false;
  160. return true;
  161. }
  162. public boolean isMethodEntry() {
  163. return (order == 0);
  164. }
  165. public boolean isStepBackOut() {
  166. if (order < 0) return true;
  167. return false;
  168. }
  169. public boolean equals(Object other) {
  170. if (this == other) return true;
  171. if (!(other instanceof TracePoint)) return false;
  172. if (methodExecution != ((TracePoint) other).methodExecution) return false;
  173. if (order != ((TracePoint) other).order) return false;
  174. return true;
  175. }
  176. }