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.

InitializerTest.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import org.aspectj.testing.Tester;
  2. class C {
  3. public static String staticField = "initialized";
  4. public String state = "C-initialized";
  5. public C() {
  6. state = "C-constructed";
  7. }
  8. }
  9. class SubC extends C implements I {
  10. {
  11. state = "SubC-initialized";
  12. }
  13. public SubC() {
  14. state = "SubC-constructed";
  15. }
  16. }
  17. interface I {
  18. public static String s = "initialized";
  19. public static String s1 = new String("s1");
  20. }
  21. aspect A issingleton () {
  22. before(): staticinitialization(C) {
  23. Tester.checkEqual(C.staticField, null, "C.staticField");
  24. }
  25. after(): staticinitialization(C) {
  26. Tester.checkEqual(C.staticField, "initialized", "C.staticField");
  27. Tester.note("static initialized C");
  28. }
  29. after(): staticinitialization(SubC) {
  30. Tester.note("static initialized SubC");
  31. }
  32. /*
  33. before(): staticinitializations(I) {
  34. Tester.checkEqual(I.s, null, "I.s");
  35. }
  36. after(): staticinitializations(I) {
  37. Tester.checkEqual(I.s, "initialized", "I.s");
  38. Tester.note("static initialized I");
  39. }
  40. */
  41. before(C c): initialization(C.new(..)) && this(c) {
  42. Tester.checkEqual(c.state, null, "c.state");
  43. }
  44. before(C c): execution(C.new(..)) && this(c) {
  45. // change from 1.0 is that fields aren't initialized at this point
  46. Tester.checkEqual(c.state, null, "c.state pre-constructor"); //"C-initialized", "c.state");
  47. Tester.note("constructed C");
  48. }
  49. after(C c): initialization(C.new(..)) && this(c) {
  50. Tester.checkEqual(c.state, "C-constructed", "c.state");
  51. Tester.note("initialized C");
  52. }
  53. before(SubC subc): initialization(SubC.new(..)) && this(subc) {
  54. Tester.checkEqual(subc.state, "C-constructed", "c.state");
  55. }
  56. before(SubC subc): execution(SubC.new(..)) && this(subc) {
  57. // change from 1.0 is that fields aren't initialized at this point
  58. Tester.checkEqual(subc.state, "C-constructed", "c.state"); //"SubC-initialized", "c.state");
  59. Tester.note("constructed SubC");
  60. }
  61. after(SubC subc): initialization(SubC.new(..)) && this(subc) {
  62. Tester.checkEqual(subc.state, "SubC-constructed", thisJoinPoint.toString());
  63. Tester.note("initialized SubC");
  64. }
  65. before(I i): initialization(I.new()) && this(i) {
  66. Tester.checkEqual(((C)i).state, "C-constructed", thisJoinPoint.toString());
  67. }
  68. // before(I i): execution(I.new()) && this(i) {
  69. // Tester.checkEqual(((C)i).state, "C-constructed", thisJoinPoint.toString());
  70. // Tester.note("constructed I");
  71. // }
  72. after(I i): initialization(I.new()) && this(i) {
  73. Tester.checkEqual(((C)i).state, "C-constructed", thisJoinPoint.toString());
  74. Tester.note("initialized I");
  75. }
  76. }
  77. public class InitializerTest {
  78. public static void main(String[] args) {
  79. new SubC();
  80. Tester.check("initialized C");
  81. Tester.check("initialized SubC");
  82. Tester.check("constructed C");
  83. Tester.check("constructed SubC");
  84. Tester.check("initialized I");
  85. //Tester.check("constructed I");
  86. Tester.check("static initialized C");
  87. Tester.check("static initialized SubC");
  88. //XXX not doing static initializers in interfaces yet
  89. //XXX Tester.check("static initialized I");
  90. }
  91. }