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.

Context.java 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package language;
  2. public class Context {
  3. public static void main(String[] argList) {
  4. new C().run();
  5. }
  6. }
  7. class C {
  8. static int MAX = 2;
  9. int i;
  10. C() {
  11. i = 1;
  12. }
  13. public void run() {
  14. try {
  15. more();
  16. } catch (MoreError e) {
  17. // log but continue
  18. System.out.println(e.getMessage());
  19. }
  20. }
  21. private void more() {
  22. i++;
  23. if (i >= MAX) {
  24. i = 0;
  25. throw new MoreError();
  26. }
  27. }
  28. static class MoreError extends Error {
  29. MoreError() {
  30. super("was too much!");
  31. }
  32. }
  33. }
  34. /** @author Erik Hilsdale, Wes Isberg */
  35. aspect A {
  36. // START-SAMPLE language-fieldSetContext Check input and result for a field set.
  37. /**
  38. * Check input and result for a field set.
  39. */
  40. void around(int input, C targ) : set(int C.i)
  41. && args(input) && target(targ) {
  42. String m = "setting C.i=" + targ.i + " to " + input;
  43. System.out.println(m);
  44. proceed(input, targ);
  45. if (targ.i != input) {
  46. throw new Error("expected " + input);
  47. }
  48. }
  49. // END-SAMPLE language-fieldSetContext
  50. // START-SAMPLE language-handlerContext Log exception being handled
  51. /**
  52. * Log exception being handled
  53. */
  54. before (C.MoreError e) : handler(C.MoreError)
  55. && args(e) && within(C) {
  56. System.out.println("handling " + e);
  57. }
  58. // END-SAMPLE language-handlerContext
  59. // See Initialization.java for constructor call,
  60. // constructor execution, and {pre}-initialization
  61. }