Freude
Freude

Reputation: 1039

AspectJ list all Joinpoints

I would like to use AspectJ with Java to print a list of all joinpoints in the program.

I found an old code example on page two in this document.

public aspect Logging {
  before (): !within    (aspects.*) {
        System.out.println(thisJoinPointStaticPart);
  }
}

The main class looks like the following:

public class Main {
    public static void main(String[] args) {
      methodeA();
    }

   public static void methodeA(){
   }    
 }

Sadly an exception occurs if I run this code with Eclipse Luna Service Release 2 (4.4.2):

Exception in thread "main" java.lang.ExceptionInInitializerError
at Main.<clinit>(Main.java:1)
Caused by: org.aspectj.lang.NoAspectBoundException: Logging
at Logging.aspectOf(Logging.aj:1)
at Logging.<clinit>(Logging.aj)
... 1 more

I expect output like:

...
call(void Main.methodeA())
execution(void Main.methodeA())

Upvotes: 3

Views: 357

Answers (1)

SubOptimal
SubOptimal

Reputation: 22971

Maybe your directory structure is wrong. You could try it with following small example project (which is based on your code) and can be executed via maven.

Assumed file/directory structure

pom.xml
src/main/java/aspects/Logging.aj
src/main/java/Main.java

Logging.aj

package aspects;
public aspect Logging {
    before(): !within(aspects.*) {
        System.out.println(thisJoinPointStaticPart);
    }
}

Main.java

public class Main {
    public static void main(String[] args) {
        methodeA();
    }
    public static void methodeA() {
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
            http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sub.optimal</groupId>
    <artifactId>AspectJScratch</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.7</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Executing mvn clean compile exec:java will produce following output.

[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ AspectJScratch ---
staticinitialization(Main.<clinit>)
execution(void Main.main(String[]))
call(void Main.methodeA())
execution(void Main.methodeA())

Upvotes: 1

Related Questions