Uploaded image for project: 'Maven Compiler Plugin'
  1. Maven Compiler Plugin
  2. MCOMPILER-510

NullPointerException when using --patch-module compiler argument

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • 3.10.1
    • None
    • None

    Description

      Summary

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

      Description

      The method preparePaths in CompilerMojo has two code paths:

      @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();
              // ...
          }
      }

      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.

      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=java_base.jar src\main\java\poc\Main.java.
      • Attempting to do the same using Maven compiler plugin results in NullPointerException: mvn -X compile

      Walkaround

      I was able to fix the problem by adding the line

      pathElements = Collections.emptyMap(); 

      to the else part of the CompilerMojo.preparePaths.

      The compilation started working, it does however produce a warning

      [WARNING] Can't locate java_base.jar

      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.

      Attachments

        1. poc.zip
          2 kB
          Artem K.

        Issue Links

          Activity

            People

              Unassigned Unassigned
              artemkh Artem K.
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: