Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude methods from aspectj

I'm trying to exclude several methods from log files using aspectj (Im usong spring and Load-time weaving). Is there a way to list the excluded methods in the aop.xml? I know i can do this for full classes but I'm looking for specific methods. or can i make a list in the aspect class? Thanks

like image 889
lior Avatar asked Dec 30 '13 07:12

lior


People also ask

What is pointcut in AspectJ?

AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)

Does Spring AOP use AspectJ?

In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style). Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception.

What is advice in AspectJ?

AspectJ supports three kinds of advice. The kind of advice determines how it interacts with the join points it is defined over. Thus AspectJ divides advice into that which runs before its join points, that which runs after its join points, and that which runs in place of (or "around") its join points.


2 Answers

I don't know how to do it in an XML, but it's easy enough to do it in the aspects themselves, as pointcuts can be combined using boolean operators.

Traditional aspectj syntax:

pointcut whatIDontWantToMatch() : within(SomeClass+) || execution(* @SomeAnnotation *.*(..));
pointcut whatIWantToMatch()     : execution(* some.pattern.here.*(..));
pointcut allIWantToMatch()      : whatIWantToMatch() && ! whatIDontWantToMatch();

@AspectJ syntax:

@Pointcut("within(SomeClass+) || execution(* @SomeAnnotation *.*(..))")
public void whatIDontWantToMatch(){}
@Pointcut("execution(* some.pattern.here.*(..))")
public void whatIWantToMatch(){}
@Pointcut("whatIWantToMatch() && ! whatIDontWantToMatch()")
public void allIWantToMatch(){}

These are of course just samples. whatIDontWantToMatch() could also be composed of several pointcuts etc.

like image 101
Sean Patrick Floyd Avatar answered Oct 21 '22 07:10

Sean Patrick Floyd


Here is way for exclusion of method based on aop.xml or aop-ajc.xml in aspectj

<aspectj>
<aspects>
<aspect name="com.perf.aspects.EJBAspect"/>
    <concrete-aspect name="com.perf.aspects.ConcreteAspectEJBInclude" extends="com.perf.aspects.EJBAspect">
    <pointcut name="ejbPointCutInclude"
        expression="execution(* com.perf.test.TestClass..*(..))" /> 
       <!-- Methods to exclude for the above execution methods in TestClass. This will also exclude all calls below excluded method -->
     <pointcut name="ejbPointCutExclude"
        expression="(execution(* com.perf.test.TestClass.getName(..))) || (execution(* com.perf.test.TestClass.getCity(..))) || cflow(execution(* com.perf.test.TestClass.getCity(..))) || cflow(execution(* com.perf.test.TestClass.getName(..))) " /> 
    </concrete-aspect>               
</aspects>
</aspectj>

Create below abstract aspect

public abstract  aspect EJBAspect 
{

    public pointcut ejbPointCutInclude();
    public pointcut ejbPointCutExclude(): ;     


    before() : ejbPointCutInclude() &&  !ejbPointCutExclude() 
    {               
        doBefore(thisJoinPointStaticPart.getSignature());       
    }

    after() :  ejbPointCutInclude() && !ejbPointCutExclude() 
    {               
        doAfter(thisJoinPointStaticPart.getSignature());
    }
}
like image 22
Sujit Pandey Avatar answered Oct 21 '22 09:10

Sujit Pandey