Skip to content

Commit

Permalink
Normalize --add-exports= and --add-opens= flags
Browse files Browse the repository at this point in the history
This is similar to the existing handling of `-source`, `-target`,
and `--release`, where (unlike javac) we allow all the options
to be mixed and matched, and let the last one win. With the
module-related flags, they are accumulated and added to the
command line only if the target level is 9 or higher, which
corresponds to modules being enabled.

PiperOrigin-RevId: 367471359
  • Loading branch information
cushon authored and copybara-github committed Apr 8, 2021
1 parent e35aedf commit c177d88
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.auto.value.AutoValue;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Ints;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
Expand Down Expand Up @@ -230,6 +231,7 @@ public static class ReleaseOptionNormalizer implements JavacOptionNormalizer {
private String source;
private String target;
private String release;
private final List<String> modular = new ArrayList<>();

@Override
public boolean processOption(String option, Iterator<String> remaining) {
Expand All @@ -253,6 +255,13 @@ public boolean processOption(String option, Iterator<String> remaining) {
target = null;
}
return true;
case "--add-exports":
case "--add-opens":
if (remaining.hasNext()) {
modular.add(option);
modular.add(remaining.next());
}
return true;
default: // fall out
}
if (option.startsWith("--release=")) {
Expand All @@ -261,11 +270,35 @@ public boolean processOption(String option, Iterator<String> remaining) {
target = null;
return true;
}
if (option.startsWith("--add-exports=") || option.startsWith("--add-opens=")) {
modular.add(option);
return true;
}
return false;
}

private void addModular(List<String> normalized) {
String value;
if (release != null) {
value = release;
} else if (target != null) {
value = target;
} else {
return;
}
boolean hasPrefix = value.startsWith("1.");
Integer version = Ints.tryParse(hasPrefix ? value.substring("1.".length()) : value);
if (version == null) {
return;
}
if (version > 8) {
normalized.addAll(modular);
}
}

@Override
public void normalize(List<String> normalized) {
addModular(normalized);
if (release != null) {
normalized.add("--release");
normalized.add(release);
Expand Down

0 comments on commit c177d88

Please sign in to comment.