Skip to content

Commit

Permalink
Add 'javaModules' and 'enablePreview' options
Browse files Browse the repository at this point in the history
They correspond to AJC/ECJ/Javac parameters '--enable-preview' and
'--module-path'.

A few minor code clean-ups are also included.
  • Loading branch information
kriegaex committed Jun 7, 2021
1 parent 64a8537 commit 6034bc1
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 10 deletions.
34 changes: 30 additions & 4 deletions src/main/java/org/codehaus/mojo/aspectj/AbstractAjcCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,18 @@ public abstract class AbstractAjcCompiler extends AbstractAjcMojo {
@Parameter
protected List<String> additionalCompilerArgs = new ArrayList<>();

/**
* Activates compiler preview features (e.g. sealed classes in Java 16) when used with a suitable JDK version
*
* @since 1.13
*/
// TODO:
// Create tickets for at least Eclipse IDE and IntelliJ IDEA to recognise this switch and import it as a compiler
// and possibly runtime setting. As for AJDT, maybe we have to implement it ourselves, but actually I found no
// references to the Maven module there, so I guess the import is implemented somewhere else.
@Parameter( defaultValue = "false" )
protected boolean enablePreview;

/**
* Holder for ajc compiler options
*/
Expand Down Expand Up @@ -631,6 +643,10 @@ protected void assembleArguments()
addModulesArgument("-aspectpath", ajcOptions, aspectLibraries, getAdditionalAspectPaths(),
"an aspect library");

// Add Java 9+ modules needed for compilation
addModulesArgument("--module-path", ajcOptions, javaModules, null,
"Java module");

// Add xmlConfigured option and argument
if (null != xmlConfigured) {
ajcOptions.add("-xmlConfigured");
Expand Down Expand Up @@ -890,13 +906,23 @@ public void setShowWeaveInfo(boolean showWeaveInfo) {
}

public void setTarget(String target) {
ajcOptions.add("-target");
ajcOptions.add(target);
if (AjcHelper.isValidComplianceLevel(target)) {
ajcOptions.add("-target");
ajcOptions.add(target);
}
}

public void setSource(String source) {
ajcOptions.add("-source");
ajcOptions.add(source);
if (AjcHelper.isValidComplianceLevel(source)) {
ajcOptions.add("-source");
ajcOptions.add(source);
}
}

public void setEnablePreview(boolean enablePreview) {
if (enablePreview) {
ajcOptions.add("--enable-preview");
}
}

public void setVerbose(boolean verbose) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/codehaus/mojo/aspectj/AbstractAjcMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public abstract class AbstractAjcMojo extends AbstractMojo
@Parameter
protected String[] weaveDirectories;

/**
* Java 9+ modules to build the module path from.
* Corresponds to <code>ajc --module-path</code> option.
*
* @since 1.13
*/
@Parameter
protected Module[] javaModules;

/**
* Weave binary aspects from the jars.
* The aspects should have been output by the same version of the compiler.
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/codehaus/mojo/aspectj/AjcHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static String createClassPath( MavenProject project, List<Artifact> plugi
String cp = "";
Set<Artifact> classPathElements = Collections.synchronizedSet(
// LinkedHashSet preserves order by insertion for iteration
new LinkedHashSet<>()
new LinkedHashSet<>()
);
Set<Artifact> dependencyArtifacts = project.getDependencyArtifacts();
// Set.addAll only adds if absent, so we want to add the project artifacts first
Expand Down Expand Up @@ -141,7 +141,7 @@ public static String createClassPath( MavenProject project, List<Artifact> plugi
public static Set<String> getBuildFilesForAjdtFile( String ajdtBuildDefFile, File basedir )
throws MojoExecutionException
{
Set<String> result = new LinkedHashSet<String>();
Set<String> result = new LinkedHashSet<>();

Properties ajdtBuildProperties = new Properties();
try
Expand Down Expand Up @@ -176,7 +176,7 @@ public static Set<String> getBuildFilesForAjdtFile( String ajdtBuildDefFile, Fil
public static Set<String> getBuildFilesForSourceDirs( List<String> sourceDirs, String[] includes, String[] excludes )
throws MojoExecutionException
{
Set<String> result = new LinkedHashSet<String>();
Set<String> result = new LinkedHashSet<>();

for ( String sourceDir : sourceDirs )
{
Expand Down Expand Up @@ -225,7 +225,7 @@ public static Set<String> getWeaveSourceFiles( String[] weaveDirs )
}
catch ( IOException e )
{
throw new MojoExecutionException( "IO Error resolving weavedirs", e );
throw new MojoExecutionException( "IO Error resolving weave directories", e );
}
}
}
Expand Down Expand Up @@ -270,7 +270,7 @@ public static void writeBuildConfigToFile( List<String> arguments, String fileNa
public static List<String> readBuildConfigFile( String fileName, File outputDir )
throws IOException
{
List<String> arguments = new ArrayList<String>();
List<String> arguments = new ArrayList<>();
File argFile = new File( outputDir, fileName );
if ( FileUtils.fileExists( argFile.getAbsolutePath() ) )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,86 @@ public void testGetAjc_libraryArtifacts()
assertTrue( weavePath.indexOf( mod1Artifact ) != -1 );
assertTrue( weavePath.indexOf( mod2Artifact ) != -1 );
}


/**
* Verifies that if not stated no --module-path or -p argument should
* be found in the ajc arguments
* {@link AbstractAjcCompiler#execute()}
*
* @throws Exception on test error
*/
public void testGetAjcArguments_noModulePath()
throws Exception
{
ajcCompMojo.assembleArguments();
List args = ajcCompMojo.ajcOptions;
assertFalse( args.contains( "--module-path" ) );
assertFalse( args.contains( "-p" ) );
}

/**
* Tests that the compiler fails as it should if told to weave a module artifact not listed in the project
* dependencies.
*/
public void testGetAjcArguments_moduleArtifactsNotProjectDependency()
{
Module module1 = new Module();
String mod1Group = "dill.group";
module1.setGroupId( mod1Group );
String mod1Artifact = "dall.artifact";
module1.setArtifactId( mod1Artifact );
try
{
ajcCompMojo.javaModules = new Module[1];
ajcCompMojo.javaModules[0] = module1;
ajcCompMojo.assembleArguments();
fail( "Should fail quite miserably" );
}
catch ( MojoExecutionException e )
{
// good thing
}
}

/**
* Tests that Java 9+ module path works as expected if listed modules also exist as dependencies
*
* @throws MojoExecutionException if the mojo fails to execute
*/
public void testGetAjc_moduleArtifacts() throws MojoExecutionException
{
ajcCompMojo.javaModules = new Module[2];
Module module1 = new Module();
String mod1Group = "dill.group";
module1.setGroupId( mod1Group );
String mod1Artifact = "dall.artifact";
module1.setArtifactId( mod1Artifact );
ajcCompMojo.javaModules[0] = module1;
Module module2 = new Module();
String mod2Group = "foooup";
module2.setGroupId( mod2Group );
String mod2Artifact = "bartifact";
module2.setArtifactId( mod2Artifact );
ajcCompMojo.javaModules[1] = module2;
// Modify project to include depencies
Set artifacts = new HashSet();
artifacts.add( new MockArtifact( mod1Group, mod1Artifact ) );
artifacts.add( new MockArtifact( mod2Group, mod2Artifact ) );
ajcCompMojo.project.setArtifacts( artifacts );
ajcCompMojo.assembleArguments();
List args = ajcCompMojo.ajcOptions;
assertTrue( args.contains( "--module-path" ) );
Iterator it = args.iterator();
while ( !it.next().equals( "--module-path" ) )
{
// don't do nothing
}
String modulePath = (String) it.next();
assertTrue( modulePath.indexOf( File.pathSeparator ) != -1 );
assertTrue( modulePath.indexOf( mod1Artifact ) != -1 );
assertTrue( modulePath.indexOf( mod2Artifact ) != -1 );
}

// MASPECTJ-103
public void testGetAJc_EmptyClassifier() throws Exception
{
Expand Down

0 comments on commit 6034bc1

Please sign in to comment.