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