public class A { | |||||
S s = new S(); | |||||
public void method() { | |||||
System.out.println("A.method() running"); | |||||
s.m1(); | |||||
} | |||||
} |
public class B { | |||||
T t = new T(); | |||||
public void method() { | |||||
System.out.println("B.method() running"); | |||||
t.m2(); | |||||
} | |||||
} |
import java.lang.reflect.Method; | |||||
public class Main { | |||||
public static void main(String []argv) { | |||||
try { | |||||
System.out.println("into:main"); | |||||
Class clazzA = Class.forName("A"); | |||||
Method clazzAMethod = clazzA.getMethod("method",null); | |||||
clazzAMethod.invoke(clazzA.newInstance(),null); | |||||
Class clazzB= Class.forName("B"); | |||||
Method clazzBMethod = clazzB.getMethod("method",null); | |||||
clazzBMethod.invoke(clazzB.newInstance(),null); | |||||
System.out.println("leave:main"); | |||||
} catch (Throwable t) { | |||||
t.printStackTrace(); | |||||
} | |||||
} | |||||
} |
public class T { | |||||
public void m1() {System.out.println("T.m1() running");} | |||||
public void m2() {System.out.println("T.m2() running");} | |||||
} |
import java.io.Serializable; | |||||
public aspect X { | |||||
declare parents: T implements Serializable; | |||||
before(): call(* Serializable+.m*(..)) { | |||||
System.out.println("advice running"); | |||||
} | |||||
} |
<aspectj> | |||||
<weaver options="-showWeaveInfo -verbose"/> | |||||
<aspects> | |||||
<aspect name="X"/> | |||||
</aspects> | |||||
</aspectj> | |||||
public class A { | |||||
T t = new T(); | |||||
public void method() { | |||||
System.out.println("A.method() running"); | |||||
t.m1(); | |||||
} | |||||
} |
import java.lang.reflect.Method; | |||||
public class Main { | |||||
public static void main(String []argv) { | |||||
try { | |||||
System.out.println("into:main"); | |||||
Class clazzA = Class.forName("A"); | |||||
Method clazzAMethod = clazzA.getMethod("method",null); | |||||
clazzAMethod.invoke(clazzA.newInstance(),null); | |||||
System.out.println("leave:main"); | |||||
} catch (Throwable t) { | |||||
t.printStackTrace(); | |||||
} | |||||
} | |||||
} |
public class T { | |||||
public void m1() {System.out.println("T.m1() running");} | |||||
} |
import java.io.Serializable; | |||||
public aspect X { | |||||
declare parents: T implements Serializable; | |||||
before(): call(* Serializable+.m*(..)) { | |||||
System.out.println("advice running"); | |||||
} | |||||
} |
<aspectj> | |||||
<weaver options="-showWeaveInfo -verbose"/> | |||||
<aspects> | |||||
<aspect name="X"/> | |||||
</aspects> | |||||
</aspectj> | |||||
Basic case, where type T needs munging with a declare parents before the join point in A.method() are matched. |
public class A { | |||||
T t = new T(); | |||||
public void method() { | |||||
System.out.println("A.method() running"); | |||||
t.m1(); | |||||
} | |||||
} |
import java.lang.reflect.Method; | |||||
public class Main { | |||||
public static void main(String []argv) { | |||||
try { | |||||
System.out.println("into:main"); | |||||
Class clazzA = Class.forName("A"); | |||||
Method clazzAMethod = clazzA.getMethod("method",null); | |||||
clazzAMethod.invoke(clazzA.newInstance(),null); | |||||
System.out.println("leave:main"); | |||||
} catch (Throwable t) { | |||||
t.printStackTrace(); | |||||
} | |||||
} | |||||
} |
import java.lang.annotation.*; | |||||
public @interface MarkerAnnotation {} |
public class T { | |||||
public void m1() {System.out.println("T.m1() running");} | |||||
} |
import java.io.Serializable; | |||||
public aspect X { | |||||
declare @type: T: @MarkerAnnotation; | |||||
before(): call(* (@MarkerAnnotation *).m*(..)) { | |||||
System.out.println("advice running"); | |||||
} | |||||
} |
<aspectj> | |||||
<weaver options="-showWeaveInfo -verbose"/> | |||||
<aspects> | |||||
<aspect name="X"/> | |||||
</aspects> | |||||
</aspectj> | |||||
Now type T needs munging with a declare annotation |
public class A { | |||||
S s = new S(); | |||||
public void method() { | |||||
System.out.println("A.method() running"); | |||||
s.m1(); | |||||
} | |||||
} |
import java.lang.reflect.Method; | |||||
public class Main { | |||||
public static void main(String []argv) { | |||||
try { | |||||
System.out.println("into:main"); | |||||
Class clazzA = Class.forName("A"); | |||||
Method clazzAMethod = clazzA.getMethod("method",null); | |||||
clazzAMethod.invoke(clazzA.newInstance(),null); | |||||
System.out.println("leave:main"); | |||||
} catch (Throwable t) { | |||||
t.printStackTrace(); | |||||
} | |||||
} | |||||
} |
public class S extends T { | |||||
} |
public class T { | |||||
public void m1() {System.out.println("T.m1() running");} | |||||
} |
import java.io.Serializable; | |||||
public aspect X { | |||||
declare parents: T implements Serializable; | |||||
before(): call(* Serializable+.m*(..)) { | |||||
System.out.println("advice running"); | |||||
} | |||||
} |
<aspectj> | |||||
<weaver options="-showWeaveInfo -verbose"/> | |||||
<aspects> | |||||
<aspect name="X"/> | |||||
</aspects> | |||||
</aspectj> | |||||
declare parents, but this time there is an extra layer in the hierarchy, A calls S but S extends T and T is decp targeted |
<aspectj> | |||||
<weaver options="-showWeaveInfo"/> | |||||
<aspects> | |||||
<aspect name="child.Advisor"/> | |||||
</aspects> | |||||
</aspectj> | |||||
/******************************************************************************* | |||||
* Copyright (c) 2006 Contributors. | |||||
* All rights reserved. | |||||
* This program and the accompanying materials are made available | |||||
* under the terms of the Eclipse Public License v1.0 | |||||
* which accompanies this distribution and is available at | |||||
* http://eclipse.org/legal/epl-v10.html | |||||
* | |||||
* Contributors: | |||||
* Ron Bodkin | |||||
*/ | |||||
package child; | |||||
import util.A; | |||||
import util.T; | |||||
aspect Advisor { | |||||
declare parents: A* implements T; | |||||
before() : call(* T+.*(..)) { | |||||
System.out.println("T call"); | |||||
} | |||||
} |
/******************************************************************************* | |||||
* Copyright (c) 2006 Contributors. | |||||
* All rights reserved. | |||||
* This program and the accompanying materials are made available | |||||
* under the terms of the Eclipse Public License v1.0 | |||||
* which accompanies this distribution and is available at | |||||
* http://eclipse.org/legal/epl-v10.html | |||||
* | |||||
* Contributors: | |||||
* Ron Bodkin | |||||
*/ | |||||
package child; | |||||
import util.A; | |||||
public class Executor implements Runnable { | |||||
public void run() { | |||||
new A().foo(); | |||||
} | |||||
} |
<!-- empty aop.xml file. Used to turn on load-time weaving with no aspects defined at top-level --> | |||||
<aspectj/> |
/******************************************************************************* | |||||
* Copyright (c) 2006 Contributors. | |||||
* All rights reserved. | |||||
* This program and the accompanying materials are made available | |||||
* under the terms of the Eclipse Public License v1.0 | |||||
* which accompanies this distribution and is available at | |||||
* http://eclipse.org/legal/epl-v10.html | |||||
* | |||||
* Contributors: | |||||
* Ron Bodkin | |||||
*/ | |||||
package top; | |||||
public class SimpleMain { | |||||
public static void main(String args[]) throws Exception { | |||||
new child.Executor().run(); | |||||
} | |||||
} |
/******************************************************************************* | |||||
* Copyright (c) 2006 Contributors. | |||||
* All rights reserved. | |||||
* This program and the accompanying materials are made available | |||||
* under the terms of the Eclipse Public License v1.0 | |||||
* which accompanies this distribution and is available at | |||||
* http://eclipse.org/legal/epl-v10.html | |||||
* | |||||
* Contributors: | |||||
* Ron Bodkin | |||||
*/ | |||||
package util; | |||||
public class A { | |||||
public void foo() {} | |||||
} |
/******************************************************************************* | |||||
* Copyright (c) 2006 Contributors. | |||||
* All rights reserved. | |||||
* This program and the accompanying materials are made available | |||||
* under the terms of the Eclipse Public License v1.0 | |||||
* which accompanies this distribution and is available at | |||||
* http://eclipse.org/legal/epl-v10.html | |||||
* | |||||
* Contributors: | |||||
* Ron Bodkin | |||||
*/ | |||||
package util; | |||||
public interface T {} |