NoSuchMethodError aspectOf() at runtime from building with iajc

Viewed 11553

We used aspectJ to get some metric on an existing application. When building and weaving with AJDT in eclipse, everything works great. But in the integration env. we use an ant script to build and deploy the application.

The problem occur on an ExceptionHandler i did to be sure our aspect would not throw exception and break the application

@Aspect
public class ExceptionHandlerAspect {

    /**
     * Pointcut
     */
    @Pointcut("within(com.xxx.yyy.aop.aspect.*..*)")
    public void allMethodInAspectPackage() {}

    /**
     *  Pointcut
     */
    @Pointcut("!within(com.xxx.yyy.aop.aspect.ExceptionHandlerAspect)")
    public void notInExceptionHandlerAspectClass() {}
    /**
     *  Pointcut
     */
    @Pointcut("call(* *(..))")
    public void allClassAndMethod() {}

    /**
    @Around("allClassAndMethod() && allMethodInAspectPackage() && notInExceptionHandlerAspectClass()")
    public Object logException(ProceedingJoinPoint joinPoint) throws Throwable{
        Object ret = null;
        try {
            ret = joinPoint.proceed();
        }catch (Throwable exception) {
            if (joinPoint.getSignature().getDeclaringTypeName().equalsIgnoreCase("org.aspectj.lang.ProceedingJoinPoint")) {
                throw exception;
            }
            this.logException.info("Exception in " + joinPoint.getSignature().getDeclaringTypeName(),exception);
        }finally {
            return ret; 
        }

    }

}

Basicaly, i want to intercept every call within my aspects package except in the ExceptionHandler itself.

the ant build look like this :

<iajc inpath="${classes.dir}" destDir="${classes.dir}" fork="true" maxmem="${aspectj.maxmem}" verbose="true" showWeaveInfo="true" debug="true">
    <classpath refid="ajclasspath"/>
</iajc>

The ${classes.dir} is the classes directory where the javac task built the application and the aspects

From the result,

Exception in thread "main" java.lang.NoSuchMethodError: com.xxx.yyy.aop.aspect.ExceptionHandlerAspect.aspectOf()Lcom/xxx/yyy/aop/aspect/ExceptionHandlerAspect;
    at com.xxx.yyy.aop.aspect.ecs.AspectBaseEcs.inspectLoginInfo(AspectBaseEcs.java:65)
    at com.xxx.yyy.app.es.security.Security.loadApplications(Security.java:172)
    at com.xxx.yyy.app.es.gui.VSDlgLogin.loadSecurity(VSDlgLogin.java:346)
    at com.xx.yyy.app.es.ApplicationSuite.start(ApplicationSuite.java:839)
    at com.xxx.yyy.app.es.ApplicationSuite.main(ApplicationSuite.java:501)

it looks like the ExceptionHandler was not weaved!!!

I hope someone can help me on this one ;-)

5 Answers

I had this exception when I used Spring Boot, but my configuration of AspectJ was done in the "old way".

If you are using Spring Boot, please make sure you have only a dependency org.springframework.boot:spring-boot-starter-aop in pom.xml/build.gradle. Then you don't need any additional @Configuration for aspects, you can simply do:

@Aspect
@Component
public class LogEntityChangesAspect {

    @Autowired
    private ChangeLogDao changeLogDao;

For load-time weaving I needed to include the package of the aspect in the aop.xml

<aspectj>
    <aspects>
        <aspect name="abc.xyz.MyAspect"/>
    </aspects>
    <weaver options="-verbose -showWeaveInfo">
        <include within="abc.def..*"/>
        <!-- next line was added -->
        <include within="abc.xyz..*"/>
    </weaver>
</aspectj>
Related