diff options
author | Liam Miller-Cushon <cushon@google.com> | 2020-12-08 17:25:06 -0800 |
---|---|---|
committer | Javac Team <javac-team+copybara@google.com> | 2020-12-08 17:25:32 -0800 |
commit | dc43e5c4d6aa1905a037cff7f77e09e6fc339e95 (patch) | |
tree | d0966d562d49dfbd85ecb66fa3ca48acdd161e1d /java | |
parent | 7db5cb2f1914f3547a206547d87cdc1252967114 (diff) | |
download | turbine-dc43e5c4d6aa1905a037cff7f77e09e6fc339e95.tar.gz |
Clean up `-source` parsing
use `SourceVersion.valueOf` to support the latest version supported by
the current runtime, without having to compile turbine against the
corresponding version (i.e. to support `-source 11` without referencing
`RELEASE_11` statically.)
PiperOrigin-RevId: 346445262
Diffstat (limited to 'java')
-rw-r--r-- | java/com/google/turbine/binder/Processing.java | 53 |
1 files changed, 32 insertions, 21 deletions
diff --git a/java/com/google/turbine/binder/Processing.java b/java/com/google/turbine/binder/Processing.java index 7b19575..aad88a4 100644 --- a/java/com/google/turbine/binder/Processing.java +++ b/java/com/google/turbine/binder/Processing.java @@ -28,6 +28,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.Sets; +import com.google.common.primitives.Ints; import com.google.turbine.binder.Binder.BindingResult; import com.google.turbine.binder.Binder.Statistics; import com.google.turbine.binder.bound.SourceTypeBoundClass; @@ -439,28 +440,10 @@ public class Processing { String option = it.next(); switch (option) { case "-source": - if (it.hasNext()) { - String value = it.next(); - switch (value) { - case "5": - case "1.5": - sourceVersion = SourceVersion.RELEASE_5; - break; - case "6": - case "1.6": - sourceVersion = SourceVersion.RELEASE_6; - break; - case "7": - case "1.7": - sourceVersion = SourceVersion.RELEASE_7; - break; - case "8": - sourceVersion = SourceVersion.RELEASE_8; - break; - default: - break; - } + if (!it.hasNext()) { + throw new IllegalArgumentException("-source requires an argument"); } + sourceVersion = parseSourceVersion(it.next()); break; default: break; @@ -469,6 +452,34 @@ public class Processing { return sourceVersion; } + private static SourceVersion parseSourceVersion(String value) { + boolean hasPrefix = value.startsWith("1."); + Integer version = Ints.tryParse(hasPrefix ? value.substring("1.".length()) : value); + if (!isValidSourceVersion(version, hasPrefix)) { + throw new IllegalArgumentException("invalid -source version: " + value); + } + try { + return SourceVersion.valueOf("RELEASE_" + version); + } catch (IllegalArgumentException unused) { + throw new IllegalArgumentException("invalid -source version: " + value); + } + } + + private static boolean isValidSourceVersion(Integer version, boolean hasPrefix) { + if (version == null) { + return false; + } + if (version < 5) { + // the earliest source version supported by JDK 8 is Java 5 + return false; + } + if (hasPrefix && version > 10) { + // javac supports legacy `1.*` version numbers for source versions up to Java 10 + return false; + } + return true; + } + private static URL[] toUrls(ImmutableList<String> processorPath) throws MalformedURLException { URL[] urls = new URL[processorPath.size()]; int i = 0; |