Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AspectJ basics #137

Merged
merged 4 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plexus-compiler-its/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
<goal>verify</goal>
</goals>
<configuration>
<debug>true</debug>
<debug>false</debug>
Copy link
Contributor Author

@kriegaex kriegaex Jun 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When searching for the problem in the IT earlier today, I noticed that the debug output was confusing me more than helping me. I think, it is better to activate it on a case-by-case basis. The standard build also does not run in Maven debug mode.

<streamLogsOnFailures>true</streamLogsOnFailures>
<projectsDirectory>src/main/it</projectsDirectory>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
<postBuildHookScript>verify</postBuildHookScript>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

invoker.java.version = 1.8+
invoker.goals = clean test
#invoker.buildResult = failure
73 changes: 73 additions & 0 deletions plexus-compiler-its/src/main/it/aspectj-compiler/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<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>org.apache.maven.plugins.compiler.it</groupId>
<artifactId>aspectj-compiler</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerId>aspectj</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-api</artifactId>
<version>@pom.version@</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-aspectj</artifactId>
<version>@pom.version@</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>@junit.version@</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>@aspectj.version@</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.acme;

public class Application {
public static void main(String[] args) {
System.out.println("Running application");
new Application().greet(args[0]);
}

public String greet(String name) {
return "Hello " + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.acme;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class MyAnnotationDrivenAspect {
@Before("execution(static * *(..)) && within(Application)")
public void myAdvice(JoinPoint joinPoint) {
System.out.println(joinPoint);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.acme;

public aspect MyNativeAspect {
before() : execution(!static * *(..)) && within(Application) {
System.out.println(thisJoinPoint);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
The Apache Maven Compiler (MC) API only allows a single source file extension, such as '.java'.
The AspectJ Compiler (AJC) should consider two default extensions, though: '.java' and '.aj'.
In order to achieve that, the Plexus AJC component tells MC to give it all files,
subsequently filtering for those two extensions.

The purpose of this file is to make sure that even though MC finds it in the source folder,
Plexus AJC filters it out and AJC does not try to compile it.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.acme;

import org.junit.Test;

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

public class ApplicationTest {
@Test
public void testGreet() {
assertEquals("Hello Jane", new Application().greet("Jane"));
}

@Test
public void testMain() {
Application.main(new String[] { "Joe" });
assertTrue(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.acme;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class TestAspect {
@Before("call(* *(..)) && !within(TestAspect)")
public void beforeCall(JoinPoint joinPoint) {
System.out.println(joinPoint);
}
}
34 changes: 34 additions & 0 deletions plexus-compiler-its/src/main/it/aspectj-compiler/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
def logFile = new File( basedir, 'build.log' )
assert logFile.exists()
content = logFile.text.normalize()

def junitLog = """Running org.acme.ApplicationTest
call(String org.acme.Application.greet(String))
execution(String org.acme.Application.greet(String))
call(void org.junit.Assert.assertEquals(Object, Object))
call(void org.acme.Application.main(String[]))
execution(void org.acme.Application.main(String[]))
Running application
execution(String org.acme.Application.greet(String))
call(void org.junit.Assert.assertTrue(boolean))
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0""".normalize()

assert content.contains( junitLog )
3 changes: 2 additions & 1 deletion plexus-compiler-its/src/main/it/error-prone-compiler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<version>@junit.version@</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
3 changes: 2 additions & 1 deletion plexus-compiler-its/src/main/it/simple-javac/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<version>@junit.version@</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
4 changes: 0 additions & 4 deletions plexus-compilers/plexus-compiler-aspectj/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
<name>Plexus AspectJ Compiler</name>
<description>AspectJ Compiler support for Plexus Compiler component.</description>

<properties>
<aspectj.version>1.9.6</aspectj.version>
</properties>

<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
Expand Down
Loading