[ファイル]-[新規]-[その他]
jButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { try{ System.out.println("UI wrote : START actionPerformed()"); System.out.println("UI wrote : CALL OmikujiModel.createOracle()"); String oracle = OmikujiModel.createOracle(); jLabel.setText(oracle); System.out.println("UI wrote : RET OmikujiModel.createOracle()"); System.out.println("UI wrote : TERM actionPerformed()"); return; }catch(Exception ex){ jLabel.setText("ERROR"); }finally{ System.out.println("UI wrote : FINAL actionPerformed()"); } } });
package com.snail.exam.aj.omikuji;
public class OmikujiModel {
private static int count = 0;
private static final String[] ORACLES
= new String[]{"excellent lucky","lucky","a bit lucky","misfortune","great misfortune"};
public static String createOracle(){
try{
System.out.println("MODEL wrote : START createOracle()");
System.out.println("MODEL wrote : TERM createOracle()");
return ORACLES[ (count++) ];
}finally{
System.out.println("MODEL wrote : FINAL createOracle()");
}
}
}
package com.snail.exam.aj.omikuji;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
@Aspect
public class OmikujiLogger {
@Pointcut("execution(public * com.snail.exam.aj.omikuji.*.*(..)) && !within(OmikujiLogger)")
public void someMethodExecute() {}
@Pointcut("call(public * com.snail.exam.aj.omikuji.*.*(..)) && !within(OmikujiLogger)")
public void someMethodCall() {}
@Before("someMethodExecute()")
public void beforeExecute(JoinPoint thisJoinPoint){
dumpJoinPoint("beforeExecute()",thisJoinPoint);
}
@After("someMethodExecute()")
public void afterExecute(JoinPoint thisJoinPoint){
dumpJoinPoint("afterExecute()",thisJoinPoint);
}
@AfterReturning("someMethodExecute()")
public void returnExecute(JoinPoint thisJoinPoint){
dumpJoinPoint("returnExecute()",thisJoinPoint);
}
@AfterThrowing("someMethodExecute()")
public void throwExecute(JoinPoint thisJoinPoint){
dumpJoinPoint("throwExecute()",thisJoinPoint);
}
@Before("someMethodCall()")
public void beforeCall(JoinPoint thisJoinPoint){
dumpJoinPoint("beforeCall()",thisJoinPoint);
}
@After("someMethodCall()")
public void afterCall(JoinPoint thisJoinPoint){
dumpJoinPoint("afterCall()",thisJoinPoint);
}
@AfterReturning("someMethodCall()")
public void returnCall(JoinPoint thisJoinPoint){
dumpJoinPoint("returnCall()",thisJoinPoint);
}
@AfterThrowing("someMethodExecute()")
public void throwCall(JoinPoint thisJoinPoint){
dumpJoinPoint("throwCall()",thisJoinPoint);
}
private void dumpJoinPoint(String src,JoinPoint joinPoint){
Signature sig = joinPoint.getSignature();
String jPointName = sig.toLongString();
System.out.println(src+"\t("+jPointName+")");
}
}
@Pointcut("execution(public * com.snail.exam.aj.omikuji.*.*(..)) && !within(OmikujiLogger)")
ボタンを押したときに、Method と Advice がどういう順番で動くのか? 実際にこの「おみくじプログラム」を動かしてみた。
UI wrote : START actionPerformed() UI wrote : CALL OmikujiModel.createOracle() beforeCall() (public static java.lang.String com.snail.exam.aj.omikuji.OmikujiModel.createOracle()) beforeExecute() (public static java.lang.String com.snail.exam.aj.omikuji.OmikujiModel.createOracle()) MODEL wrote : START createOracle() MODEL wrote : TERM createOracle() MODEL wrote : FINAL createOracle() afterExecute() (public static java.lang.String com.snail.exam.aj.omikuji.OmikujiModel.createOracle()) returnExecute() (public static java.lang.String com.snail.exam.aj.omikuji.OmikujiModel.createOracle()) afterCall() (public static java.lang.String com.snail.exam.aj.omikuji.OmikujiModel.createOracle()) returnCall() (public static java.lang.String com.snail.exam.aj.omikuji.OmikujiModel.createOracle()) UI wrote : RET OmikujiModel.createOracle() UI wrote : TERM actionPerformed() UI wrote : FINAL actionPerformed()
UI wrote : START actionPerformed() UI wrote : CALL OmikujiModel.createOracle() beforeCall() (public static java.lang.String com.snail.exam.aj.omikuji.OmikujiModel.createOracle()) beforeExecute() (public static java.lang.String com.snail.exam.aj.omikuji.OmikujiModel.createOracle()) MODEL wrote : START createOracle() MODEL wrote : TERM createOracle() MODEL wrote : FINAL createOracle() afterExecute() (public static java.lang.String com.snail.exam.aj.omikuji.OmikujiModel.createOracle()) throwExecute() (public static java.lang.String com.snail.exam.aj.omikuji.OmikujiModel.createOracle()) throwCall() (public static java.lang.String com.snail.exam.aj.omikuji.OmikujiModel.createOracle()) afterCall() (public static java.lang.String com.snail.exam.aj.omikuji.OmikujiModel.createOracle()) UI wrote : FINAL actionPerformed()