aboutsummaryrefslogtreecommitdiffstats
path: root/tests/features164/declareMixin/CaseEConcurrent.java
blob: df1e1377418d3eda28ded5bb84b5e3cf79ed6179 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// TESTING: multiple instances causing factory invocation multiple times (but is cached correctly)
// Concurrency fix regression test for https://github.com/eclipse/org.aspectj/issues/198
import org.aspectj.lang.annotation.*;
import java.util.concurrent.atomic.AtomicInteger;

public class CaseEConcurrent {
  private String id;

  public static void main(String[]argv) throws InterruptedException {
    final CaseEConcurrent cea = new CaseEConcurrent("a");
    final CaseEConcurrent ceb = new CaseEConcurrent("b");

    Thread t1 = new Thread(new Runnable() { public void run() { ((I)cea).methodOne(); } });
    Thread t2 = new Thread(new Runnable() { public void run() { ((I)cea).methodTwo(); } });
    Thread t3 = new Thread(new Runnable() { public void run() { ((I)ceb).methodOne(); } });
    Thread t4 = new Thread(new Runnable() { public void run() { ((I)ceb).methodTwo(); } });

    t1.start();
    t2.start();
    t3.start();
    t4.start();

    t1.join();
    t2.join();
    t3.join();
    t4.join();
  }

  public CaseEConcurrent(String id) {
    this.id=id;
  }

  public String toString() {
    return "CaseEConcurrent instance: "+id;
  }

  // Helper methods 'doSomething' and 'callMe' help to produce byte code similar to what we need in order to fix
  // https://github.com/eclipse/org.aspectj/issues/198. If necessary, just temporarily uncomment, compile and analyse
  // the byte code, e.g. with JDK tool 'javap -v'.
  /*
  public void doSomething() {
    synchronized (this) {
      if (id == null)
        id = "doing something";
    }
    callMe(id);
  }

  public void callMe(String param) {
    System.out.println("I was called with param " + param);
  }
  */

}

aspect X {
  @DeclareMixin("CaseEConcurrent")
  public I createImplementation(Object o) {
    System.out.println("Delegate factory invoked for " + o);
    try { Thread.sleep(250); } catch (InterruptedException e) { throw new RuntimeException(e); }
    Implementation impl = new Implementation(o);
    return impl;
  }
}

interface I {
  void methodOne();
  void methodTwo();
}

class Implementation implements I {
  Object o;

  public Implementation(Object o) {
    this.o = o;
  }

  public void methodOne() {
    System.out.println("methodOne running on "+o);
  }
  public void methodTwo() {
    System.out.println("methodTwo running on "+o);
  }
}