Intersection of problems in the middle of methods

this is convenient for AOP (for example, AspectJ, SpringAOP) to solve cross-reference problems in pointcut methods using the methods below,

Sandwich code

methodA {
    crosscut code
    user code A
    crosscut code
}

methodB {
    crosscut code
    user code B
    crosscut code
}

Is the AOP prone to overlapping issues that overlap with the user code below? How?

Spaghetti Code

methodX {
    user code x1
    crosscut code

    user code x2
    crosscut code
}

methodY {

    user code y1
    crosscut code

    user code y2
    crosscut code
}

Thank!

+3
source share
2 answers

Spring AOP will not help, as it only understands the execution pointcut ().

AspectJ includes a lot more pointcuts, including the innercode () construct, which sounds the way you want:

withincode(* YourClass.methodX(. .))

this allows you to consult all junction points within the selected method

AspectJ , AspectJ Spring AOP.


EDIT:

:

package com.dummy.aspectj;

import java.util.Arrays;
import java.util.Collections;

public class DummyClass{

    public static void main(final String[] args){
        System.out.println(Arrays.asList("One", Collections.singleton("Two")));
        System.out.println("Enough?");
    }

}

package com.dummy.aspectj;

import java.util.Arrays;

public aspect DummyAspect{

    pointcut callWithinMain() : 
        withincode(* com.dummy.aspectj.DummyClass.main(..)) // anything inside DummyClass.main
        && call(* *.*(..));                                 // but only method calls

    before() : callWithinMain() {
        System.out.println("\n***************************************************");
        System.out.println("** Calling:\n**\t"
            + thisJoinPointStaticPart.getSignature()
            + "\n** with arguments:\n**\t "
            + Arrays.deepToString(thisJoinPoint.getArgs()) );
        System.out.println("***************************************************\n");
    }

}

DummyClass Eclipse/AJDT :

***************************************************
** Calling:
**  Set java.util.Collections.singleton(Object)
** with arguments:
**   [Two]
***************************************************


***************************************************
** Calling:
**  List java.util.Arrays.asList(Object[])
** with arguments:
**   [[One, [Two]]]
***************************************************


***************************************************
** Calling:
**  void java.io.PrintStream.println(Object)
** with arguments:
**   [[One, [Two]]]
***************************************************

[One, [Two]]

***************************************************
** Calling:
**  void java.io.PrintStream.println(String)
** with arguments:
**   [Enough?]
***************************************************

Enough?
+4

AOP , , , . :

methodX 
{
    usercodemethod1();
    usercodemethod2();
}

usercodemethod1
{
    user code x1
    crosscut code
}

usercodemethod2
{
    user code x2
    crosscut code
}
+2

Source: https://habr.com/ru/post/1760128/


All Articles