Skip to content

Commit

Permalink
Add a custom compile argument replaceProcessorPathWithProcessorModule…
Browse files Browse the repository at this point in the history
…Path to force the plugin replace processorPath with processormodulepath

Closes #191 #192
  • Loading branch information
vipcxj authored and slachiewicz committed May 6, 2022
1 parent 18d380e commit 8cc8c9f
Showing 1 changed file with 78 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,12 @@
* SOFTWARE.
*/

import org.codehaus.plexus.compiler.AbstractCompiler;
import org.codehaus.plexus.compiler.Compiler;
import org.codehaus.plexus.compiler.CompilerConfiguration;
import org.codehaus.plexus.compiler.CompilerException;
import org.codehaus.plexus.compiler.CompilerMessage;
import org.codehaus.plexus.compiler.CompilerOutputStyle;
import org.codehaus.plexus.compiler.CompilerResult;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.jdt.core.compiler.CompilationProgress;
import org.eclipse.jdt.core.compiler.batch.BatchCompiler;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
Expand All @@ -56,6 +44,19 @@
import java.util.ServiceLoader;
import java.util.Set;

import org.codehaus.plexus.compiler.AbstractCompiler;
import org.codehaus.plexus.compiler.Compiler;
import org.codehaus.plexus.compiler.CompilerConfiguration;
import org.codehaus.plexus.compiler.CompilerException;
import org.codehaus.plexus.compiler.CompilerMessage;
import org.codehaus.plexus.compiler.CompilerOutputStyle;
import org.codehaus.plexus.compiler.CompilerResult;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.jdt.core.compiler.CompilationProgress;
import org.eclipse.jdt.core.compiler.batch.BatchCompiler;

/**
*
*/
Expand Down Expand Up @@ -206,7 +207,14 @@ public CompilerResult performCompile( CompilerConfiguration config )

if ( processorPathEntries != null && processorPathEntries.size() > 0 )
{
args.add( "-processorpath" );
if ( isReplaceProcessorPath( config ) )
{
args.add( "--processor-module-path" );
}
else
{
args.add( "-processorpath" );
}
args.add( getPathString( processorPathEntries ) );
}

Expand Down Expand Up @@ -478,6 +486,22 @@ public void worked( int i, int i1 )
}
}

private static final String OPT_REPLACE_PROCESSOR_PATH = "replaceProcessorPathWithProcessorModulePath";
private static final String OPT_REPLACE_PROCESSOR_PATH_ = "-" + OPT_REPLACE_PROCESSOR_PATH;

static boolean isReplaceProcessorPath( CompilerConfiguration config )
{
for ( Entry<String, String> entry : config.getCustomCompilerArgumentsEntries() )
{
String opt = entry.getKey();
if ( opt.equals( OPT_REPLACE_PROCESSOR_PATH ) || opt.equals( OPT_REPLACE_PROCESSOR_PATH_ ) )
{
return true;
}
}
return false;
}

static List<String> resortSourcesToPutModuleInfoFirst( List<String> allSources )
{
ArrayList<String> resorted = new ArrayList<>();
Expand Down Expand Up @@ -538,46 +562,49 @@ static boolean processCustomArguments( CompilerConfiguration config, List<String
continue;
}

/*
* The compiler mojo makes quite a mess of passing arguments, depending on exactly WHICH
* way is used to pass them. The method method using <compilerArguments> uses the tag names
* of its contents to denote option names, and so the compiler mojo happily adds a '-' to
* all of the names there and adds them to the "custom compiler arguments" map as a
* name, value pair where the name always contains a single '-'. The Eclipse compiler (and
* javac too, btw) has options with two dashes (like --add-modules for java 9). These cannot
* be passed using a <compilerArguments> tag.
*
* The other method is to use <compilerArgs>, where each SINGLE argument needs to be passed
* using an <arg>xxxx</arg> tag. In there the xxx is not manipulated by the compiler mojo, so
* if it starts with a dash or more dashes these are perfectly preserved. But of course these
* single <arg> entries are not a pair. So the compiler mojo adds them as pairs of (xxxx, null).
*
* We use that knowledge here: if a pair has a null value then do not mess up the key but
* render it as a single value. This should ensure that something like:
* <compilerArgs>
* <arg>--add-modules</arg>
* <arg>java.se.ee</arg>
* </compilerArgs>
*
* is actually added to the command like as such.
*
* (btw: the above example will still give an error when using ecj <= 4.8M6:
* invalid module name: java.se.ee
* but that seems to be a bug in ecj).
*/
if ( null == optionValue )
{
//-- We have an option from compilerArgs: use the key as-is as a single option value
args.add( opt );
}
else
if ( !opt.equals( OPT_REPLACE_PROCESSOR_PATH ) && !opt.equals( OPT_REPLACE_PROCESSOR_PATH_ ) )
{
if ( !opt.startsWith( "-" ) )
/*
* The compiler mojo makes quite a mess of passing arguments, depending on exactly WHICH
* way is used to pass them. The method method using <compilerArguments> uses the tag names
* of its contents to denote option names, and so the compiler mojo happily adds a '-' to
* all of the names there and adds them to the "custom compiler arguments" map as a
* name, value pair where the name always contains a single '-'. The Eclipse compiler (and
* javac too, btw) has options with two dashes (like --add-modules for java 9). These cannot
* be passed using a <compilerArguments> tag.
*
* The other method is to use <compilerArgs>, where each SINGLE argument needs to be passed
* using an <arg>xxxx</arg> tag. In there the xxx is not manipulated by the compiler mojo, so
* if it starts with a dash or more dashes these are perfectly preserved. But of course these
* single <arg> entries are not a pair. So the compiler mojo adds them as pairs of (xxxx, null).
*
* We use that knowledge here: if a pair has a null value then do not mess up the key but
* render it as a single value. This should ensure that something like:
* <compilerArgs>
* <arg>--add-modules</arg>
* <arg>java.se.ee</arg>
* </compilerArgs>
*
* is actually added to the command like as such.
*
* (btw: the above example will still give an error when using ecj <= 4.8M6:
* invalid module name: java.se.ee
* but that seems to be a bug in ecj).
*/
if ( null == optionValue )
{
//-- We have an option from compilerArgs: use the key as-is as a single option value
args.add( opt );
}
else
{
opt = "-" + opt;
if ( !opt.startsWith( "-" ) )
{
opt = "-" + opt;
}
args.add( opt );
args.add( optionValue );
}
args.add( opt );
args.add( optionValue );
}
}
return result;
Expand Down

0 comments on commit 8cc8c9f

Please sign in to comment.