Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Re: RE:interecepting of all calls and forwarding them

> Question 1:
>
> Let's say I have two classes: Stopped and StoppedStub. During testing I
want all calls to Stopped
> be forwarded to StoppedStub. The only way I know of doing that is to catch
each and every method
> call Stoppped and then forward this call to the same method but on
StoppedStub (this is where
> interfaces come in handy). I was wondering if there was a way to do this
automatically.
>
> So, instead of this:
>
> public aspect StoppedStubIns {
> pointcut a(): call(void Stopped.a());
> pointcut b(): call(void Stopped.b());
>
> around(): a() {
> StoppedStub.a();
> }
>
> around(): b() {
> StoppedStub.b();
> }
> }
>
> I want a way to do this:
>
> public aspect StoppedStubIns {
> pointcut allCalls(): call(void Stopped.*());
>
> around(): allCalls() {
> StoppedStub.["method invoked on Stopped"]();
> }
> }
>


Here is my attempt
 A and B musth have the same methods and signature

public class A {

public static void m1(){

System.out.println("A.m1()");

}


public static void m2(){

System.out.println("A.m2()");

}


public static void m3(int i){

System.out.println("A.m3(i) " + i);

}


public static void m4(String str){

System.out.println("A.m4() "+ str);

}


public static void m5(String[] str){

System.out.println("A.m5()");

for (int i = 0; i < str.length; i++) {

System.out.println("A.m5() " + i + " " + str[i]);

}

}

}



public class B {

public static void m1() {

System.out.println("B.m1()");

}

public static void m2() {

System.out.println("B.m2()");

}

public static void m3(int i) {

System.out.println("B.m3(i) " + i);

}

public static void m4(String str) {

System.out.println("B.m4() " + str);

}

public static void m5(String[] str) {

System.out.println("B.m5()");

for (int i = 0; i < str.length; i++) {

System.out.println("B.m5() " + i + " " + str[i]);

}

}

}



public aspect TestAspect {

pointcut aPC() : execution (public static * A.*(..));


void around() : aPC(){

System.err.println(thisJoinPoint);

Object[] methodArgs = thisJoinPoint.getArgs();

String methodName = thisJoinPoint.getSignature().getName();

Class[] argClasses = getArgClasses(methodArgs);

try{

Method m = B.class.getMethod(methodName,argClasses);

m.invoke(null,methodArgs);

}catch(Exception exp){

exp.printStackTrace();

}

}


Class[] getArgClasses(Object[] methodArgs){

if(methodArgs == null){

return null;

}


Class[] argClasses = new Class[methodArgs.length];

for(int i=0;i<methodArgs.length;i++){

if(methodArgs[i] instanceof Integer){

argClasses[i] = Integer.TYPE;

}else{

argClasses[i] = methodArgs[i].getClass();

}

}

return argClasses;

}

}



Sample Run



public class Run {

public static void main(String[] args){

A.m1();

A.m2();

A.m3(5);

A.m4("hello");

String[] sr = {"foo","bar"};

A.m5(sr);


B.m1();

B.m2();

B.m3(5);

B.m4("hello");

B.m5(sr);

}

}



Result

execution(void test.A.m1())

B.m1()

execution(void test.A.m2())

B.m2()

execution(void test.A.m3(int))

B.m3(i) 5

execution(void test.A.m4(String))

B.m4() hello

execution(void test.A.m5(String[]))

B.m5()

B.m5() 0 foo

B.m5() 1 bar

B.m1()

B.m2()

B.m3(i) 5

B.m4() hello

B.m5()

B.m5() 0 foo

B.m5() 1 bar



hope this helps

iyad





Back to the top