1
votes

I'm having some trouble getting my aspects to work correctly. For some reason they seem to only be working when I am really generic about where the aspect is applied. Out of the four pointcut/advice only the last matches and outputs anything. What am I missing here?

public aspect VerboseAspect
{
    after(Game game) returning:
            call(* Game.addPlayer(Player))
            && target(game) {
        System.out.println("addPlayer(Player): player has been added");
    }

    after(Game game) returning:
            call(* Game.addPlayer(..))
            && target(game) {
        System.out.println("addPlayer(..): player has been added");
    }

    after(Game game) returning:
            call(* Game.*(Player))
            && target(game) {
        System.out.println("*(Player):player has been added");
    }

    after(Game game) returning:
            call(* Game.*(..))
            && target(game) {
        System.out.println("*(..):player has been added");
    }
}

Java class:

public class Game {
    public void addPlayer(Player player) {
        System.out.println("inside class");
        this.players.add(player);
    }
}

Output:

inside class
*(..):player has been added
1

1 Answers

0
votes

(Comment, but too long.)

What version of AspectJ are you using? How are you building? Ant? Maven? Other?

It works fine for me inside IntelliJ; running this:

public static void main(String[] args) {
    Game g = new Game();
    g.addPlayer(new Player());
}

Produces:

inside class
addPlayer(Player): player has been added
addPlayer(..): player has been added
*(Player):player has been added
*(..):player has been added