简体   繁体   中英

How to invoke methods of hooked class within aspect?

Maybe name of theme is bad. I try to explain clarity.

I have class:

public class A(){
   Field1 field1;
   Field2 field2;
   public void method1(){...}
   public void method2(){...}
   public void sourceMethod(ParameterClass parameter1){
     //some code
     method1();
     //some code
      method2();
     //some cdoe
  }
}

I will hook source method: ...

@Around(value = "execution(* A.sourceMethod(ParameterClass))")
    public void aroundSourceMethod(JoinPoint joinPoint){
      //I need to write my realization sourceMethod here
      // I want to invoke method1 and method2 here
}

Here I want to rewrite all code. But I need to invoke method1() and method2()

Is it possibly using AspectJ ?

@Around(value = "execution(* A.sourceMethod(Parameter)) && target(target)")
public void aroundSourceMethod(JoinPoint joinPoint, Object target){
   // I need to write my realization sourceMethod here
   // I want to invoke method1 and method2 here
}

Target will contain the object which sourceMethod is executed on. Since you only advise A.sourceMethod(), you can assume that it is of type A, cast it to that and call its methods as you like:

((A) target).method1()
...

It's not pretty, but it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM