Artem K. created MCOMPILER-510:
----------------------------------

             Summary: NullPointerException when using --patch-module compiler 
argument
                 Key: MCOMPILER-510
                 URL: https://issues.apache.org/jira/browse/MCOMPILER-510
             Project: Maven Compiler Plugin
          Issue Type: Bug
    Affects Versions: 3.10.1
            Reporter: Artem K.
         Attachments: poc.zip

h2. Summary

When using {{--patch-module}} compiler argument, there is a 
{{NullPointerException}} due to field {{pathElements}} not being initialized in 
{{{}CompilerMojo{}}}.
h2. Description

The method {{preparePaths}} in {{CompilerMojo}} has two code paths:
{code:java}
@Override
protected void preparePaths( Set<File> sourceFiles )
{
    boolean hasModuleDescriptor = false;
    // ...
    if ( hasModuleDescriptor )
    {
        modulepathElements = new ArrayList<>( compilePath.size() );
        classpathElements = new ArrayList<>( compilePath.size() );
        pathElements = new LinkedHashMap<>( compilePath.size() );
    }
    else
    {
        classpathElements = new ArrayList<>();
        modulepathElements = Collections.emptyList();
        // ...
    }
}{code}
Notice that in the else part, {{pathElements}} is not initialized. This field 
is used when the compiler arguments contain {{--patch-module}} switch. So 
whenever the compiler is used with this switch and there is no module 
descriptor in the project, NPE occurs.
h2. Proof-of-concept

Attached is a simple proof-of-concept to illustrate the issue. The project 
consists of a single class with raises {{NullPointerException}} using a new 
constructor {{{}NullPointerException(Throwable){}}}, which does not exist in 
base JDK.
 * When executing {{javac}}{{{} src\main\java\poc\Main.java{}}}, the 
compilation fails as expected.
 * Using a replacement {{NullPointerException}} class in {{java_base.jar}} with 
the added constructor, the compilation works fine:{{{} javac --patch-module 
java.base=test.jar src\main\java\poc\Main.java{}}}.
 * Attempting to do the same using Maven compiler plugin results in 
NullPointerException: {{mvn -X compile}}

h2. Walkaround

I was able to fix the problem by adding the line
{code:java}
pathElements = Collections.emptyMap(); {code}
to the else part of the {{{}CompilerMojo.preparePaths{}}}.

The compilation started working, it does however produce a warning
{noformat}
[WARNING] Can't locate java_base.jar{noformat}
every single time. I did not investigate much the cause of this message.

Also note that while similar, this issues is not the same as MCOMPILER-311.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to