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.

StubReplace.java 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 1998-2002 PARC Inc. All rights reserved.
  3. *
  4. * Use and copying of this software and preparation of derivative works based
  5. * upon this software are permitted. Any distribution of this software or
  6. * derivative works must comply with all applicable United States export
  7. * control laws.
  8. *
  9. * This software is made available AS IS, and PARC Inc. makes no
  10. * warranty about the software, its performance or its conformity to any
  11. * specification.
  12. */
  13. public class StubReplace {
  14. public static void main(String[] args) {
  15. new PrintJob().run();
  16. }
  17. }
  18. /** @author Wes Isberg */
  19. aspect Stubs {
  20. // article page 76 - stubs
  21. // START-SAMPLE testing-inoculated-replaceWithProxy Replace object with proxy on constructiono
  22. /**
  23. * Replace all PrintStream with our StubStream
  24. * by replacing the call to any constructor of
  25. * PrinterStream or any subclasses.
  26. */
  27. PrinterStream around () : within(PrintJob)
  28. && call (PrinterStream+.new(..)) && !call (StubStream+.new(..)) {
  29. return new StubStream(thisJoinPoint.getArgs());
  30. }
  31. // END-SAMPLE testing-inoculated-replaceWithProxy
  32. // START-SAMPLE testing-inoculated-adviseProxyCallsOnly Advise calls to the proxy object only
  33. pointcut stubWrite() : printerStreamTestCalls() && target(StubStream);
  34. pointcut printerStreamTestCalls() : call(* PrinterStream.write());
  35. before() : stubWrite() {
  36. System.err.println("picking out stubWrite" );
  37. }
  38. // END-SAMPLE testing-inoculated-adviseProxyCallsOnly
  39. }
  40. class PrinterStream {
  41. public void write() {}
  42. }
  43. class StubStream extends PrinterStream {
  44. public StubStream(Object[] args) {}
  45. }
  46. class PrintJob {
  47. public void run() {
  48. PrinterStream p = new PrinterStream();
  49. System.err.println("not PrinterStream: " + p);
  50. System.err.println("now trying call...");
  51. p.write();
  52. }
  53. }