9

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 ;-)

Betlista
  • 10,327
  • 13
  • 69
  • 110
Cygnusx1
  • 5,329
  • 2
  • 27
  • 39

5 Answers5

6

Finally found the problem. Our Application had a dependency on common module jar that was containing aspect too.

The base package name was the same: com.xxx.aop and the base class we used for our aspects was the same name. So 2 com.xxx.aop.AspectBase.class were loaded.

Since we used a flag in our Ant build file to enable compile time weaving at yes/no, one of our AspectBase.class was not weaved while the other was.

gvlasov
  • 18,638
  • 21
  • 74
  • 110
Cygnusx1
  • 5,329
  • 2
  • 27
  • 39
2

I had very similar problem

java.lang.NoSuchMethodError: ....aspectOf()...

I'm using Spring 3.1.2 + Maven 3.0.4 + Tomcat 7.0.29 and at od times it happens that when I'm starting Tomcat I'm getting this Exception.

I have no idea why that happens. I'm using Spring Tool Suit IDE, but when I perform mvn clean install from command line (not in IDE) it somehow get fixed, maybe this helps someone.

Betlista
  • 10,327
  • 13
  • 69
  • 110
  • Seriously man...I was searching for this issue for almost 2 hours...but only your solution worked for me...so giving you a thumbs up. – Gurinder Feb 19 '19 at 10:33
0

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;
Piotr Sagalara
  • 2,247
  • 3
  • 22
  • 25
0

it looks like you have forgotten to declare the aspect path. All your task does is to compile but not weave. And that results in the java.lang.NoSuchMethodError

Below is a sample that shows how to use the iajc task with weaving:

<iajc outjar="demo.jar">
    <sourceroots>
        <pathelement location=”src” />
        <pathelement location=".." />
    </sourceroots>
    <aspectpath>
        <pathelement location="org.springframework.aspects-3.0.0.RC1.jar" />
    </aspectpath>
</iajc>

I hope this helps!

Betlista
  • 10,327
  • 13
  • 69
  • 110
Espen
  • 10,545
  • 5
  • 33
  • 39
  • Thanks for the reply. Actually, i can see that my aspects are woven. Since when i decompile them, i see that aspectOf() and hasAspect() are visible in the decompiled code of my ExceptionHandler. All these aspects are in a package inside the project. They are all included in the inpath. What are we supose to put in the aspectpath? the aspecjrt.jar? – Cygnusx1 Jul 13 '11 at 15:12
  • The aspect you want to weave into your code. In the example above contains a transaction aspect that weaves transaction logic to all methods marked with @Transactional. If you have your aspect in the inpath, it should also work. – Espen Jul 14 '11 at 15:33
  • Thanks again to take the time to respond. Actually you respond to another question i have posted about aspectpath ;-) But unfortunatly it still does not work. My aspects are contain in the same project as the base code of the application. so they are in fact in the inpath. but still no luck with the ExceptionHandler having a NoSuchMethodError at runtime. If i comment the pointcuts of the ExceptionHandler, everything works great. So the ExceptionHandler is in cause... but why^ i still don't know :-( – Cygnusx1 Jul 14 '11 at 19:49
0

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>
cnmuc
  • 6,025
  • 2
  • 24
  • 29