diff options
-rw-r--r-- | java/com/google/turbine/binder/Processing.java | 53 | ||||
-rw-r--r-- | javatests/com/google/turbine/binder/ProcessingTest.java | 45 |
2 files changed, 77 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; diff --git a/javatests/com/google/turbine/binder/ProcessingTest.java b/javatests/com/google/turbine/binder/ProcessingTest.java index 75203e9..b7091e8 100644 --- a/javatests/com/google/turbine/binder/ProcessingTest.java +++ b/javatests/com/google/turbine/binder/ProcessingTest.java @@ -17,6 +17,7 @@ package com.google.turbine.binder; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableList; import javax.lang.model.SourceVersion; @@ -35,4 +36,48 @@ public class ProcessingTest { assertThat(Processing.parseSourceVersion(ImmutableList.of("-source", "8", "-source", "7"))) .isEqualTo(SourceVersion.RELEASE_7); } + + @Test + public void withPrefix() { + assertThat(Processing.parseSourceVersion(ImmutableList.of("-source", "1.7"))) + .isEqualTo(SourceVersion.RELEASE_7); + assertThat(Processing.parseSourceVersion(ImmutableList.of("-source", "1.8"))) + .isEqualTo(SourceVersion.RELEASE_8); + } + + @Test + public void invalidPrefix() { + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> Processing.parseSourceVersion(ImmutableList.of("-source", "1.11"))); + assertThat(expected).hasMessageThat().contains("invalid -source version: 1.11"); + } + + @Test + public void latestSupported() { + String latest = SourceVersion.latestSupported().toString(); + assertThat(latest).startsWith("RELEASE_"); + latest = latest.substring("RELEASE_".length()); + assertThat(Processing.parseSourceVersion(ImmutableList.of("-source", latest))) + .isEqualTo(SourceVersion.latestSupported()); + } + + @Test + public void missingArgument() { + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> Processing.parseSourceVersion(ImmutableList.of("-source"))); + assertThat(expected).hasMessageThat().contains("-source requires an argument"); + } + + @Test + public void invalidSourceVersion() { + IllegalArgumentException expected = + assertThrows( + IllegalArgumentException.class, + () -> Processing.parseSourceVersion(ImmutableList.of("-source", "NOSUCH"))); + assertThat(expected).hasMessageThat().contains("invalid -source version: NOSUCH"); + } } |