private void addModuleOptions()

in src/main/java/org/apache/maven/plugin/compiler/ToolExecutorForTest.java [246:316]


    private void addModuleOptions(final Options configuration) throws IOException {
        if (addedModuleOptions) {
            return;
        }
        addedModuleOptions = true;
        if (!hasModuleDeclaration || dependencyResolution == null) {
            return;
        }
        if (SUPPORT_LEGACY && useModulePath && hasTestModuleInfo) {
            /*
             * Do not add any `--add-reads` parameters. The developers should put
             * everything needed in the `module-info`, including test dependencies.
             */
            return;
        }
        final var done = new HashSet<String>(); // Added modules and their dependencies.
        final var addModules = new StringJoiner(",");
        StringJoiner addReads = null;
        boolean hasUnnamed = false;
        for (Map.Entry<Dependency, Path> entry :
                dependencyResolution.getDependencies().entrySet()) {
            boolean compile = false;
            switch (entry.getKey().getScope()) {
                case TEST:
                case TEST_ONLY:
                    compile = true;
                    // Fall through
                case TEST_RUNTIME:
                    if (compile) {
                        // Needs to be initialized even if `name` is null.
                        if (addReads == null) {
                            addReads = new StringJoiner(",");
                        }
                    }
                    Path path = entry.getValue();
                    String name = dependencyResolution.getModuleName(path).orElse(null);
                    if (name == null) {
                        hasUnnamed = true;
                    } else if (done.add(name)) {
                        addModules.add(name);
                        if (compile) {
                            addReads.add(name);
                        }
                        /*
                         * For making the options simpler, we do not add `--add-modules` or `--add-reads`
                         * options for modules that are required by a module that we already added. This
                         * simplification is not necessary, but makes the command-line easier to read.
                         */
                        dependencyResolution.getModuleDescriptor(path).ifPresent((descriptor) -> {
                            for (ModuleDescriptor.Requires r : descriptor.requires()) {
                                done.add(r.name());
                            }
                        });
                    }
                    break;
            }
        }
        if (!done.isEmpty()) {
            configuration.addIfNonBlank("--add-modules", addModules.toString());
        }
        if (addReads != null) {
            if (hasUnnamed) {
                addReads.add("ALL-UNNAMED");
            }
            String reads = addReads.toString();
            addReads(configuration, getMainModuleName(), reads);
            for (SourceDirectory root : sourceDirectories) {
                addReads(configuration, root.moduleName, reads);
            }
        }
    }