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.

Tricky1.java 888B

123456789101112131415161718192021222324252627282930313233
  1. public class Tricky1 {
  2. public static void main(String[] args) {
  3. Product1 p1, p2, p3;
  4. p1 = Product1.create();
  5. p2 = Product1.create();
  6. p3 = Product1.create();
  7. System.out.println("p1: " + p1 + ", p2: " + p2 + ", p3: " + p3);
  8. }
  9. }
  10. class Product1 {
  11. public static Product1 create() {
  12. return new Product1();
  13. }
  14. }
  15. aspect MakeProduct1Singleton {
  16. private static Product1 singletonProduct1 = null;
  17. // all calls to the Product1.create() method
  18. pointcut creationCut(): calls(Product1, new(..)); //Product1 create());
  19. // all calls to the above that don't happen in this aspect
  20. pointcut externalCreationCut(): !within(MakeProduct1Singleton) && creationCut();
  21. static around () returns Product1: externalCreationCut() {
  22. if (singletonProduct1 == null) {
  23. singletonProduct1 = new Product1(); //Product1.create();
  24. }
  25. return singletonProduct1;
  26. }
  27. }