Skip to content

Commit a007765

Browse files
authored
Fix AspectJ basics (#137)
* Centrally version-manage JUnit 4.13.2, also for tests ITs now use the same version as the project itself, no longer 3.8.2. * Bump AspectJ to 1.9.7.M3, supporting Java 15+16 Also pull up the version property into the parent POM, in order to be able to use it in ITs, too, similarly to the ErrorProne version. * Fix basic AspectJ compiler functionality + add integration test This is a first, basic step into improving AspectJ support for Plexus Compiler. It looks like nobody ever noticed, that target and release parameters are not forwarded to AJC, resulting in all class files being compiled to Java 1.2 target. I.e. that annotation-based @AspectJ syntax was completely unsupported before. Since Plexus was introduced 15 years ago, nobody ever did anything to upgrade the functionality a bit, only AspectJ versions were bumped before. Furthermore, only *.java files were being considered for compilation, no *.aj files. This was also fixed, but at the cost of duplication with regard to redefining two static methods 'getSourceFiles' and 'getSourceFilesForSourceRoot'. Other compiler components did it similarly, so the necessary refactoring to make the methods non-static and break them down into smaller parts in order to be able to override only the parts of them dealing with source file extensions, is a TODO which involves refactoring the other compiler components, too. I did not do that in this first step. Another TODO: In contrast to the ECJ adapter, which uses the batch compiler, AJC (an ECJ fork!) is used via the internal AJDT interface, which is designed to be used by the Eclipse IDE. It would have been easier to actually also use the batch compiler right from the start, because then we can more easily map command line parameters and keep the adapter layer thin. Why the original author did that in 2005, remains a mystery. The new AspectJ integration test uses a mixture of - native and @AspectJ syntax variants, - *.java and *.aj aspect source files, - production (src/main) and test (src/test) aspects. This is all covered by a single IT. In the end, the test asserts on the complete JUnit console log, which has to look a certain way in order to reflect that - compilation and test were successful, - all 3 aspects did what they are supposed to do. * Activate 'streamLogsOnFailures' for Maven Invoker This way, it is easier to debug failing integration tests on CI environments, where it is difficult or impossible to add the individual IT build logs. OTOH, the Maven debug statements in the test output are usually making it quite hard to find the actual error that occurred. While helpful in rare cases, most of the time the normal Maven build output should easily suffice in order to identify and fix integration test problems. Therefore, I deactivated 'debug' for Maven Invoker. This should strike a sensible balance between inlining IT error logs into the outer Maven build log in case of errors and not polluting it with too much information.
1 parent bfd5875 commit a007765

File tree

16 files changed

+351
-93
lines changed

16 files changed

+351
-93
lines changed

plexus-compiler-its/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
<goal>verify</goal>
4040
</goals>
4141
<configuration>
42-
<debug>true</debug>
42+
<debug>false</debug>
43+
<streamLogsOnFailures>true</streamLogsOnFailures>
4344
<projectsDirectory>src/main/it</projectsDirectory>
4445
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
4546
<postBuildHookScript>verify</postBuildHookScript>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.java.version = 1.8+
19+
invoker.goals = clean test
20+
#invoker.buildResult = failure
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<groupId>org.apache.maven.plugins.compiler.it</groupId>
26+
<artifactId>aspectj-compiler</artifactId>
27+
<version>1.0-SNAPSHOT</version>
28+
29+
<properties>
30+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
31+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
32+
<maven.compiler.source>1.8</maven.compiler.source>
33+
<maven.compiler.target>1.8</maven.compiler.target>
34+
</properties>
35+
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<artifactId>maven-compiler-plugin</artifactId>
40+
<version>3.8.1</version>
41+
<configuration>
42+
<compilerId>aspectj</compilerId>
43+
</configuration>
44+
<dependencies>
45+
<dependency>
46+
<groupId>org.codehaus.plexus</groupId>
47+
<artifactId>plexus-compiler-api</artifactId>
48+
<version>@pom.version@</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.codehaus.plexus</groupId>
52+
<artifactId>plexus-compiler-aspectj</artifactId>
53+
<version>@pom.version@</version>
54+
</dependency>
55+
</dependencies>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
<dependencies>
61+
<dependency>
62+
<groupId>junit</groupId>
63+
<artifactId>junit</artifactId>
64+
<version>@junit.version@</version>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.aspectj</groupId>
69+
<artifactId>aspectjrt</artifactId>
70+
<version>@aspectj.version@</version>
71+
</dependency>
72+
</dependencies>
73+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.acme;
2+
3+
public class Application {
4+
public static void main(String[] args) {
5+
System.out.println("Running application");
6+
new Application().greet(args[0]);
7+
}
8+
9+
public String greet(String name) {
10+
return "Hello " + name;
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.acme;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.annotation.Aspect;
5+
import org.aspectj.lang.annotation.Before;
6+
7+
@Aspect
8+
public class MyAnnotationDrivenAspect {
9+
@Before("execution(static * *(..)) && within(Application)")
10+
public void myAdvice(JoinPoint joinPoint) {
11+
System.out.println(joinPoint);
12+
}
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.acme;
2+
3+
public aspect MyNativeAspect {
4+
before() : execution(!static * *(..)) && within(Application) {
5+
System.out.println(thisJoinPoint);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
The Apache Maven Compiler (MC) API only allows a single source file extension, such as '.java'.
2+
The AspectJ Compiler (AJC) should consider two default extensions, though: '.java' and '.aj'.
3+
In order to achieve that, the Plexus AJC component tells MC to give it all files,
4+
subsequently filtering for those two extensions.
5+
6+
The purpose of this file is to make sure that even though MC finds it in the source folder,
7+
Plexus AJC filters it out and AJC does not try to compile it.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.acme;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertTrue;
7+
8+
public class ApplicationTest {
9+
@Test
10+
public void testGreet() {
11+
assertEquals("Hello Jane", new Application().greet("Jane"));
12+
}
13+
14+
@Test
15+
public void testMain() {
16+
Application.main(new String[] { "Joe" });
17+
assertTrue(true);
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.acme;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.annotation.Aspect;
5+
import org.aspectj.lang.annotation.Before;
6+
7+
@Aspect
8+
public class TestAspect {
9+
@Before("call(* *(..)) && !within(TestAspect)")
10+
public void beforeCall(JoinPoint joinPoint) {
11+
System.out.println(joinPoint);
12+
}
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
def logFile = new File( basedir, 'build.log' )
20+
assert logFile.exists()
21+
content = logFile.text.normalize()
22+
23+
def junitLog = """Running org.acme.ApplicationTest
24+
call(String org.acme.Application.greet(String))
25+
execution(String org.acme.Application.greet(String))
26+
call(void org.junit.Assert.assertEquals(Object, Object))
27+
call(void org.acme.Application.main(String[]))
28+
execution(void org.acme.Application.main(String[]))
29+
Running application
30+
execution(String org.acme.Application.greet(String))
31+
call(void org.junit.Assert.assertTrue(boolean))
32+
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0""".normalize()
33+
34+
assert content.contains( junitLog )

plexus-compiler-its/src/main/it/error-prone-compiler/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
<dependency>
3939
<groupId>junit</groupId>
4040
<artifactId>junit</artifactId>
41-
<version>3.8.2</version>
41+
<version>@junit.version@</version>
42+
<scope>test</scope>
4243
</dependency>
4344
</dependencies>
4445

plexus-compiler-its/src/main/it/simple-javac/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
<dependency>
3838
<groupId>junit</groupId>
3939
<artifactId>junit</artifactId>
40-
<version>3.8.2</version>
40+
<version>@junit.version@</version>
41+
<scope>test</scope>
4142
</dependency>
4243
</dependencies>
4344

plexus-compilers/plexus-compiler-aspectj/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
<name>Plexus AspectJ Compiler</name>
1414
<description>AspectJ Compiler support for Plexus Compiler component.</description>
1515

16-
<properties>
17-
<aspectj.version>1.9.6</aspectj.version>
18-
</properties>
19-
2016
<dependencies>
2117
<dependency>
2218
<groupId>org.codehaus.plexus</groupId>

0 commit comments

Comments
 (0)