Caller pointcuts@JBoss AOPを使って、java.util.Date#getTimeをインターセプトしてみる。
今回いじるサンプルは、nekopさんに教えていただいた、
jboss-aop_1.5.6.GA\docs\aspect-framework\examples\caller
です。
まずは、Driver.javaに追加。
/* * JBoss, Home of Professional Open Source * Copyright 2005, JBoss Inc., and individual contributors as indicated * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ import java.util.ArrayList; public class Driver { private static void caller() { System.out.println("--- caller is calling new ArrayList(); ---"); ArrayList list = new ArrayList(); System.out.println("--- caller is calling list.size(); ---"); list.size(); } private static void anotherCaller() { System.out.println("--- anotherCaller is calling new ArrayList(); ---"); ArrayList list = new ArrayList(); System.out.println("--- anotherCaller is calling list.size(); ---"); list.size(); } public static void main(String[] args) { System.out.println("--- main is calling new ArrayList(); ---"); ArrayList list = new ArrayList(); System.out.println("--- main is calling list.size(); ---"); list.size(); caller(); anotherCaller(); // 追加 System.out.println("--- caller is calling java.util.Date.getTime(); ---"); System.out.println(new java.util.Date().getTime()); } }
<?xml version="1.0" encoding="UTF-8"?> <aop> <bind pointcut="call(int java.util.ArrayList->size()) AND withincode(void Driver->caller())"> <interceptor class="CallerInterceptor1"/> </bind> <bind pointcut="call(int java.util.ArrayList->size()) AND withincode(* Driver->*(..)) AND !withincode(void Driver->caller())"> <interceptor class="CallerInterceptor2"/> </bind> <bind pointcut="call(java.util.ArrayList->new()) AND within(Driver)"> <interceptor class="CallerInterceptor1"/> </bind> <!-- add --> <bind pointcut="call(long java.util.Date->getTime()) AND withincode(void Driver->main(java.lang.String[]))"> <interceptor class="CallerInterceptor1"/> </bind> </aop>
でantで実行してみる。
...(省略) [java] --- anotherCaller is calling list.size(); --- [java] <<< Entering CallerInterceptor2 [java] >>> Leaving CallerInterceptor2 [java] --- caller is calling java.util.Date.getTime(); --- [java] <<< Entering CallerInterceptor1 [java] >>> Leaving CallerInterceptor1 [java] 1206276690750 BUILD SUCCESSFUL Total time: 4 seconds >
見事インターセプトされました!