0

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?

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Darek
  • 4,687
  • 31
  • 47
  • The code you posted from that tutorial is okay and the test works fine for me. Did you activate AspectJ support in IDEA? Did you follow the tutorial to build with Maven and auto-import the Maven project into IDEA? The problem must be in your build or IDE setup, not in AspectJ. – kriegaex Apr 18 '19 at 11:08
  • The test passes, but it never hits the `around` advice, because, as I understand it, the `ajc` is unable to match any methods prescribed by the `pointcut`. I am trying to comprehend why? – Darek Apr 19 '19 at 12:05
  • For me the aspect fires, I can see its output on the console. Please answer my questions. – kriegaex Apr 20 '19 at 01:21
  • Yes, I have activated AspectJ support and Auto-Importeur Maven project. Then switched to ajc compiler. – Darek Apr 20 '19 at 01:27
  • Does it work when running the Maven build, e.g. `mvn clean verify`? – kriegaex Apr 20 '19 at 02:00
  • Still no luck ... If I could trouble you to help me once more, would you mind dumping your solution to my OneDrive? I'd love to peek how your build has been setup. Link: https://1drv.ms/f/s!An09r5z8EmhlhIspWe33LJKLFKBV_A – Darek Apr 22 '19 at 13:16
  • Maybe you misunderstand how SO works. I will explain: You show me your solution and I help you fix it, not the other way around. So how about you posting an [MCVE](http://stackoverflow.com/help/mcve) on GitHub or in your OneDrive folder? GitHub would be better, because then I could fork, commit and send a pull request. – kriegaex Apr 23 '19 at 00:19
  • Sure. I will do it tonight. – Darek Apr 23 '19 at 17:46
  • First, let me state how much I appreciate your patience with my process. Here is the repo I have just created: https://github.com/DarekDan/java.learning.AOP – Darek Apr 23 '19 at 18:27

1 Answers1

2

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!

kriegaex
  • 63,017
  • 15
  • 111
  • 202