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.

AJDBTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import org.aspectj.tools.ajdb.Main;
  2. import org.aspectj.debugger.tty.CommandLineDebugger;
  3. import org.aspectj.debugger.base.*;
  4. import com.sun.jdi.*;
  5. import com.sun.jdi.event.*;
  6. import java.io.*;
  7. import java.util.*;
  8. import org.aspectj.testing.Tester;
  9. public class AJDBTest implements StopListener, VMListener {
  10. String classpath;
  11. {
  12. classpath = "\"" + Tester.outputDir();
  13. String javaClasspath = System.getProperty("java.class.path");
  14. if (javaClasspath != null) {
  15. classpath += ";" + javaClasspath;
  16. }
  17. classpath += "\"";
  18. }
  19. protected String[] stringCommands() {
  20. return new String[] {
  21. "workingdir " + Tester.workingDir(),
  22. "use " + "./new",
  23. "stop on AJDBClass.java:24",
  24. "run -classpath " + classpath + " AJDBClass",
  25. };
  26. }
  27. protected String getSourceName() {
  28. return "AJDBClass.java";
  29. }
  30. // Methods for VMListener
  31. protected void death(VMDeathEvent e) {
  32. System.out.println("*** Death: " + e);
  33. }
  34. protected void disconnect(VMDisconnectEvent e) {
  35. System.out.println("*** Disconnect: " + e);
  36. }
  37. protected void start(VMStartEvent e) {
  38. System.out.println("*** Start: " + e);
  39. }
  40. // Methods for StopListener
  41. protected void access(AccessWatchpointEvent e) {
  42. }
  43. protected void breakpoint(BreakpointEvent e) {
  44. checkLines((List) ex("where"), "next");
  45. }
  46. protected void exception(ExceptionEvent e) {
  47. }
  48. protected void modification(ModificationWatchpointEvent e) {
  49. }
  50. protected void step(StepEvent e) {
  51. List lines = (List) ex("where");
  52. checkLines(lines);
  53. try {
  54. StackFrame frame = (StackFrame) lines.get(0);
  55. Location loc = frame.location();
  56. if (loc.sourceName().equals("Thread.java") &&
  57. loc.method().name().equals("exit")) {
  58. isRunning = false;
  59. }
  60. } catch (Throwable t) {}
  61. ex("next");
  62. }
  63. public void checkLines(Collection lines, Object then) {
  64. checkLines(lines);
  65. if (then != null) ex(then);
  66. }
  67. public void checkLines(Collection lines) {
  68. Iterator iter = lines.iterator();
  69. while (iter.hasNext()) {
  70. StackFrame frame = (StackFrame) iter.next();
  71. String source = "no.source";
  72. try {
  73. source = debugger.sourceName(frame.location());
  74. } catch (Throwable t) {}
  75. int line = debugger.lineNumber(frame.location());
  76. if (source.equals(getSourceName())) {
  77. Tester.check(line>0, "non-mapping line for " + frame);
  78. }
  79. }
  80. }
  81. // VMListener
  82. public void vmDeathEvent(VMDeathEvent e) {
  83. death(e);
  84. }
  85. public void vmDisconnectEvent(VMDisconnectEvent e) {
  86. disconnect(e);
  87. }
  88. public void vmStartEvent(VMStartEvent e) {
  89. start(e);
  90. }
  91. // StopListener
  92. public final void accessWatchpointEvent(AccessWatchpointEvent e) {
  93. access(e);
  94. }
  95. public final void breakpointEvent(BreakpointEvent e) {
  96. breakpoint(e);
  97. }
  98. public final void exceptionEvent(ExceptionEvent e) {
  99. exception(e);
  100. }
  101. public final void modificationWatchpointEvent(ModificationWatchpointEvent e) {
  102. modification(e);
  103. }
  104. public final void stepEvent(StepEvent e) {
  105. step(e);
  106. }
  107. AJDebugger debugger;
  108. CommandLineDebugger ajdb;
  109. public void realMain(String[] args) {
  110. String fileName = null;
  111. String[] newArgs = args;
  112. if (args.length > 0) {
  113. fileName = args[0];
  114. newArgs = new String[args.length-1];
  115. System.arraycopy(args, 1, newArgs, 0, newArgs.length);
  116. }
  117. realMain(fileName, newArgs);
  118. }
  119. private void realMain(String fileName, String[] args) {
  120. debugger = (ajdb = new Main().debug(args)).getDebugger();
  121. debugger.addStopListener(this);
  122. debugger.addVMListener(this);
  123. ex(fileName == null ? commands() : commands(fileName));
  124. while (isRunning) {
  125. //System.out.println(">>>>>>>> " + debugger.isRunning());
  126. }
  127. }
  128. private boolean isRunning = true;
  129. public final Collection commands() {
  130. Collection list = new Vector();
  131. String[] commands = stringCommands();
  132. for (int i = 0; i < commands.length; i++) {
  133. list.add(commands[i]);
  134. }
  135. return list;
  136. }
  137. Object ex(Object command) {
  138. return ajdb.executeCommand(command+"");
  139. }
  140. void ex(Collection list) {
  141. Iterator iter = list.iterator();
  142. while (iter.hasNext()) {
  143. ex(iter.next());
  144. }
  145. }
  146. final static String COMMENT = "#";
  147. final static String FILENAME = "script.txt";
  148. Collection commands(String fileName) {
  149. Collection list = new Vector();
  150. try {
  151. BufferedReader in = new BufferedReader(new FileReader(fileName));
  152. String line;
  153. while ((line = in.readLine()) != null) {
  154. line = line.trim();
  155. if (line.startsWith(COMMENT)) {
  156. continue;
  157. }
  158. list.add(line);
  159. }
  160. in.close();
  161. } catch (IOException ioe) {
  162. ioe.printStackTrace();
  163. System.exit(-1);
  164. }
  165. return list;
  166. }
  167. public static void main(String[] args) {
  168. new AJDBTest().realMain(args);
  169. }
  170. }