javaconstructoraspectjpointcuts

AspectJ, general pointcuts without constructors


I've made a profiling method:

@Around("tld.mycompany.business.aspects.SystemArchitecture.inServiceLayer() && !tld.mycompany.business.aspects.SystemArchitecture.publicConstructor()")
public Object profileBusiness(ProceedingJoinPoint pjp) throws Throwable {
 try {
    long start = System.currentTimeMillis();
    String name = pjp.getSourceLocation().toString() + " " + pjp.getSignature().getName();
    Object output = pjp.proceed();
    long elapsedTime = System.currentTimeMillis() - start;
    if(elapsedTime > 100)
        System.err.println("TimerAspect: Businessmethod " + name + " execution time: " + elapsedTime + " ms.");

    return output;
 } catch (Exception ex) {
     ex.printStackTrace(System.err);
     throw ex;
 }
}

And defined the pointcuts in tld.mycompany.business.aspects.SystemArchitecture as

@Pointcut("execution(public new(..))")
public void publicConstructor() {}

and

@Pointcut("within(tld.mycompany.business..*Impl) && 
           !execution(private * tld.mycompany.business.*.dataType()) && 
           !handler(java.lang.Exception)")

public void inServiceLayer() {}

I want to profile all methods in my service layer that aren't constructors and exceptions (so that I don't get the "around on initialization not supported (compiler limitation)" and "around on pre-initialization not supported (compiler limitation)" warnings) and ignore dataType() that I've got in a few.

However, I still get the warnings about the constructor and exceptions. It also seems to advice just about any Java method there is, so debugging my application has become close to impossible as I hit many advices for every line. Eclipse tells me it has 2747 advices for the profileBusiness line alone.

Clearly I must have misunderstood something, but what? How can I make it specifically to being Around all methods (except for constructors) in the classes within the tld.mycompany.business hierarchy that end with Impl?

Cheers

Nik


Solution

  • This part of your pointcut:

    within(tld.mycompany.business..*Impl)
    

    targets all joinpoints in all of your *Impl classes. That is why you are seeing advice markers on every line.

    You need to add a line like:

    execution(* tld.mycompany.business..*Impl.*(..))
    

    Also, !handler(java.lang.Exception) is meaningless since the handler pointcut refers to catch clauses (that exclusive of execution pointcuts).

    Finally, your publicConstructor pointcut seems wrong to me. Wouldn't you also want to remove protected, private and package protected constructors?