Search code examples
javaaopaspectjaspectj-maven-plugin

Getting error in AspectJ sample project


I am new to aspectJ. I am getting compilation errors in Aspect class. When I run the project, I am getting the compilation error. Please help me. Placing the code snippet and error obtained below:

Project structure:

enter image description here

MannersAspect.java

package main.java.testaop;

public aspect MannersAspect {
    pointcut callSayMessage() :
        call(public static void HelloWorld.say*(..));
    before() : callSayMessage() {
        System.out.println("Good day!");
    }
    after() : callSayMessage() {
        System.out.println("Thank you!");
    }
}

HelloWorld.java

package main.java.testaop;

public class HelloWorld {
    public static void say(String message) {
        System.out.println(message);
    }
    public static void sayToPerson(String message, String name) {
        System.out.println(name + ", " + message);
    }
}

pom.xml

<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>com.test</groupId>
    <artifactId>testaop</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>testaop</name>

    <dependencies>                            
        <dependency>       
            <groupId>org.aspectj</groupId>    
            <artifactId>aspectjrt</artifactId>
            <version>1.8.4</version>          
        </dependency>                         
    </dependencies> 

    <build>
        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-compiler-plugin</artifactId>
                                        <versionRange>[2.5.1,)</versionRange>
                                        <goals><goal>compile</goal></goals>
                                    </pluginExecutionFilter>
                                    <action><ignore></ignore></action>
                                </pluginExecution>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.codehaus.mojo</groupId>
                                        <artifactId>aspectj-maven-plugin</artifactId>
                                        <versionRange>[1.7,)</versionRange>
                                        <goals><goal>compile</goal></goals>
                                    </pluginExecutionFilter>
                                    <action><ignore></ignore></action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>                                                 
                <groupId>org.codehaus.mojo</groupId>            
                <artifactId>aspectj-maven-plugin</artifactId>        
                <executions>                                         
                    <execution>                                      
                        <goals><goal>compile</goal></goals>                                     
                        <configuration>                              
                            <source>1.5</source>                     
                            <target>1.5</target>                     
                        </configuration>                             
                    </execution>                                     
                </executions>                                        
            </plugin>
            <plugin>                                                 
                <groupId>org.codehaus.mojo</groupId>         
                <artifactId>exec-maven-plugin</artifactId>           
                <executions>                                         
                    <execution><goals><goal>java</goal></goals></execution>                                     
                </executions>                                        
                <configuration>                                      
                    <mainClass>main.java.testaop.HelloWorld</mainClass>            
                </configuration>                                     
            </plugin>
        </plugins>
    </build>
</project>

Error:

[ERROR] COMPILATION ERROR : 
[ERROR] D:\workspacedummy\testaop\src\main\java\testaop\MannersAspect.java:[3,7] error: class, interface, or enum expected
[ERROR] D:\workspacedummy\testaop\src\main\java\testaop\MannersAspect.java:[4,51] error: class, interface, or enum expected
[ERROR] D:\workspacedummy\testaop\src\main\java\testaop\MannersAspect.java:[4,72] error: malformed floating point literal
[ERROR] D:\workspacedummy\testaop\src\main\java\testaop\MannersAspect.java:[5,4] error: class, interface, or enum expected
[ERROR] D:\workspacedummy\testaop\src\main\java\testaop\MannersAspect.java:[7,4] error: class, interface, or enum expected
[ERROR] D:\workspacedummy\testaop\src\main\java\testaop\MannersAspect.java:[10,4] error: class, interface, or enum expected
[INFO] 6 errors 
[INFO] BUILD FAILURE
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project testaop: Compilation failure: Compilation failure:

Solution

  • You have created your aspect in Eclipse via "New" -> "Class" (thus the ".java" extension), but you should create it via "New" -> "Aspect" (then it will get an ".aj" extension).

    P.S.: You do have AJDT (AspectJ Development Tools) installed in Eclipse, don't you? ;-)

    P.P.S.: Your Maven POM also contains a few problems, e.g. a runtime dependency on AspectJ Runtime 1.8.4, but the AspectJ Maven Plugin in the old version 1.5 uses AspectJ Tools 1.7.3. The current plugin version 1.7 uses AspectJ Tools 1.8.2 and can be upgraded to 1.8.4 via plugin dependency configuration, if necessary.