回答編集履歴

2

thisについての注意点追記

2017/09/20 09:10

投稿

masaya_ohashi
masaya_ohashi

スコア9206

test CHANGED
@@ -10,6 +10,8 @@
10
10
 
11
11
  ```Java
12
12
 
13
+ import org.aspectj.lang.JoinPoint;
14
+
13
15
  import org.aspectj.lang.annotation.Aspect;
14
16
 
15
17
  import org.aspectj.lang.annotation.Before;
@@ -78,10 +80,20 @@
78
80
 
79
81
  + " || call(void funcC(int, int))")
80
82
 
81
- public void funcPre() {
83
+ public void funcPre(JoinPoint joinPoint) {
84
+
85
+ // JoinPointは呼び出し元などの情報が入っている
86
+
87
+
82
88
 
83
89
  System.out.println("funcPre");
84
90
 
91
+ // このメソッド内でthisと書いても、funcAを呼んでいるインスタンスとは別物なので注意
92
+
93
+ // 以下のように呼び出し元インスタンスはgetTargetで得る
94
+
95
+ // Sample sample = (Sample) joinPoint.getTarget();
96
+
85
97
  }
86
98
 
87
99
 
@@ -102,6 +114,8 @@
102
114
 
103
115
  }
104
116
 
117
+
118
+
105
119
  ```
106
120
 
107
121
 

1

MainクラスをSampleクラスに修正

2017/09/20 09:10

投稿

masaya_ohashi
masaya_ohashi

スコア9206

test CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
21
  @Aspect
22
22
 
23
- public class Main {
23
+ public class Sample {
24
24
 
25
25
 
26
26
 
@@ -88,7 +88,7 @@
88
88
 
89
89
  public static void main(String[] args) {
90
90
 
91
- Main m = new Main();
91
+ Sample m = new Sample();
92
92
 
93
93
  m.funcA(0);
94
94
 
@@ -102,8 +102,6 @@
102
102
 
103
103
  }
104
104
 
105
-
106
-
107
105
  ```
108
106
 
109
107