Skip to content

Commit

Permalink
Initial support for --add-reads,-opens,-exports (java9-modularity#74)
Browse files Browse the repository at this point in the history
* Initial support for --add-reads,-opens,-exports

* Fix broken build
  • Loading branch information
aalmiray authored and paulbakker committed Apr 1, 2019
1 parent 6b3d091 commit ca86e2d
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 22 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,53 @@ Next, when Gradle invokes the Java compiler, the compiler is set up with the cor
When using the module system the compiler checks dependencies and encapsulation based on the `requires`, `exports` and `opens` keywords in `module-info-java`.
These are related, but clearly two different steps.

MonkeyPatching the module
===
There are times when explicit modular settings may be needed on compile, test, and run tasks. You have the option to specify these settings using
a `moduleOptions` extension on the target task, for example

```
compileJava {
moduleOptions {
addModules = ['com.acme.foo']
}
}
```

The following options are supported by the `moduleOptions` extension:

* addModules: Maps to `--add-modules`. Value is of type `List<String>`, e.g, `['com.acme.foo']`.
* addReads: Maps to `--add-reads`. Value is of type `Map<String, String>`, e.g, `['module1': 'module2']`.
* addOpens: Maps to `--add-opens`. Value is of type `Map<String, String>`, e.g, `['module1/package': 'module2']`.
* addExports: Maps to `--add-exports`. Value is of type `Map<String, String>`, e.g, `['module1/package': 'module2']`.

Note that multiple entries matching the same left hand side may be added to `addReads`, `addOpens`, and `addExports` but
no value accumulation is performed, the last entry overrides the previous one. If you need to combine multiple values then
you must do so explicitly. The following block resolves to `--add-reads module1=module3`

```
compileJava {
moduleOptions {
addReads = [
'module1': 'module2',
'module1': 'module3'
]
}
}
```

Where as the following block resolves to `--add-reads module1=module2,module3`

```
compileJava {
moduleOptions {
addReads = [
'module1': 'module2,module3'
]
}
}
```

Whitebox testing
===

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ buildScan {
}

group 'org.javamodularity'
version '1.4.1'
version '1.4.2-SNAPSHOT'

sourceCompatibility = 11
targetCompatibility = 11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ static void mutateJavaCompileTask(Project project, JavaCompile compileJava) {
.filter(patchModuleExtension::isUnpatched)
.getAsPath()));

if (!moduleOptions.getAddModules().isEmpty()) {
String addModules = String.join(",", moduleOptions.getAddModules());
compilerArgs.add("--add-modules");
compilerArgs.add(addModules);
}
String moduleName = (String) project.getExtensions().findByName("moduleName");
moduleOptions.mutateArgs(moduleName, compilerArgs);

compilerArgs.addAll(patchModuleExtension.configure(compileJava.getClasspath()));
compileJava.getOptions().setCompilerArgs(compilerArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ public void accept(TestEngine testEngine) {
});

ModuleOptions moduleOptions = compileTestJava.getExtensions().getByType(ModuleOptions.class);
if (!moduleOptions.getAddModules().isEmpty()) {
String addModules = String.join(",", moduleOptions.getAddModules());
args.add("--add-modules");
args.add(addModules);
}
moduleOptions.mutateArgs(moduleName, args);

args.addAll(patchModuleExtension.configure(compileTestJava.getClasspath()));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package org.javamodularity.moduleplugin.tasks;

import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class ModuleOptions {
private static final Logger LOGGER = Logging.getLogger(ModuleOptions.class);

private List<String> addModules = new ArrayList<>();
private Map<String, String> addReads = new LinkedHashMap<>();
private Map<String, String> addExports = new LinkedHashMap<>();
private Map<String, String> addOpens = new LinkedHashMap<>();

public ModuleOptions(Project project) {}
public ModuleOptions(Project project) {
}

public List<String> getAddModules() {
return addModules;
Expand All @@ -17,4 +27,55 @@ public List<String> getAddModules() {
public void setAddModules(List<String> addModules) {
this.addModules = addModules;
}

public Map<String, String> getAddReads() {
return addReads;
}

public void setAddReads(Map<String, String> addReads) {
this.addReads = addReads;
}

public Map<String, String> getAddExports() {
return addExports;
}

public void setAddExports(Map<String, String> addExports) {
this.addExports = addExports;
}

public Map<String, String> getAddOpens() {
return addOpens;
}

public void setAddOpens(Map<String, String> addOpens) {
this.addOpens = addOpens;
}

void mutateArgs(String moduleName, List<String> args) {
if (!getAddModules().isEmpty()) {
String addModules = String.join(",", getAddModules());
args.add("--add-modules");
args.add(addModules);
LOGGER.debug("Adding modules '{}' to patch module {}...", addModules, moduleName);
}

mutateArgs(moduleName, args, getAddReads(), "--add-reads");
mutateArgs(moduleName, args, getAddExports(), "--add-exports");
mutateArgs(moduleName, args, getAddOpens(), "--add-opens");
}

private void mutateArgs(String moduleName, List<String> args, Map<String, String> src, String flag) {
if (!src.isEmpty()) {
LOGGER.debug("Updating module '{}' with...", moduleName);
src.entrySet().stream()
.map(e -> e.getKey() + "=" + e.getValue())
.forEach(e -> {
LOGGER.debug(" {}", flag);
LOGGER.debug(" {}", e);
args.add(flag);
args.add(e);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ public void execute(final Task task) {
var jvmArgs = new ArrayList<String>();

ModuleOptions moduleOptions = execTask.getExtensions().getByType(ModuleOptions.class);
if (!moduleOptions.getAddModules().isEmpty()) {
String addModules = String.join(",", moduleOptions.getAddModules());
jvmArgs.add("--add-modules");
jvmArgs.add(addModules);
}
moduleOptions.mutateArgs(moduleName, jvmArgs);

patchModuleExtension.getConfig().forEach(patch -> {
String[] split = patch.split("=");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ public void execute(Task task) {
"--add-modules", "ALL-MODULE-PATH"
));

if (!testModuleOptions.getAddModules().isEmpty()) {
String addModules = String.join(",", testModuleOptions.getAddModules());
args.add("--add-modules");
args.add(addModules);
}
testModuleOptions.mutateArgs(moduleName, args);

args.addAll(patchModuleExtension.configure(testJava.getClasspath()));

Expand Down

0 comments on commit ca86e2d

Please sign in to comment.