简体   繁体   中英

AspectJ weaving with ajc and IntelliJ

I am at a loss ... I have followed the steps from Intro to AspectJ , but when I try to compile the samples with ajc, I get a "ajc: advice defined in learning.AccountAspect has not been applied [Xlint:adviceDidNotMatch]" warning on my before, around and after advices. Here is my full code:

Account.java

package learning;


public class Account {
    int balance = 20;

    public boolean withdraw(int amount) {
        if (balance < amount) {
            return false;
        }
        balance = balance - amount;
        return true;
    }
}

AccoutnAspect.aj

package learning;

public aspect AccountAspect {
    final int MIN_BALANCE = 10;

    pointcut callWithDraw(int amount, Account acc) :
            call(boolean Account.withdraw(int)) && args(amount) && target(acc);

    before(int amount, Account acc): callWithDraw(amount, acc) {
    }

    boolean around(int amount, Account acc) :
            callWithDraw(amount, acc) {
        if (acc.balance < amount) {
            System.out.println("Insufficient funds");
            return false;
        }
        System.out.println("Withdrawal approved");
        return proceed(amount, acc);
    }

    after(int amount, Account balance) : callWithDraw(amount, balance) {
    }
}

AccountTest.java

package learning;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class AccountTest {
    private Account account;

    @Before
    public void before() {
        account = new Account();
    }

    @Test
    public void given20AndMin10_whenWithdraw5_thenSuccess() {
        assertTrue(account.withdraw(5));
    }

    @Test
    public void given20AndMin10_whenWithdraw100_thenFail() {
        System.out.println(account.balance);
        assertFalse(account.withdraw(100));
        System.out.println(account.balance);
    }
}

I have general understanding of AOP and decent experience in C# flavor of AOP, PostSharp, but I can't wrap my head around the AspectJ implementation. Could someone shed some light on what obvious point am I missing?

Thanks for the MCVE . I cloned it and found the problem. As I said in my previous comment...

The problem must be in your build or IDE setup, not in AspectJ.

... you had a build management problem, to be more precise your Maven POM was wrong. You configured AspectJ Maven in the <pluginManagement> section but forgot to actually add the plugin to your Maven module in the <plugins> section like this:

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
        </plugin>
    </plugins>

Maybe you should learn some Maven basics first. BTW, the tutorial you were reading differs from what you did in your POM, thus the problem.

Furthermore, the plugin version RELEASE does not work, you really need to set a real version number like 1.11. I also did that for you, plus I removed your IDEA project files from the Git repository and streamlined/improved your .gitignore file. All these changes can be found and reviewed in my pull request .

Now the Maven build with mvn clean test as well as running the test from IntelliJ IDEA both work nicely.

Enjoy!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM