Skip to content

Commit

Permalink
Handle --release in compilesWithFix
Browse files Browse the repository at this point in the history
The java compiler does not allow `-source` and `-target` to be specified
explicitly when `--release` is [1], but does add them in response to
passing `--release` [2]. As such `SuggestedFixes#compilesWithFix` must
compensate for this by removing `-source` and `-target` before
triggering another compilation round.

Fixes #1265.

[1] https://hg.openjdk.java.net/jdk/jdk11/file/1ddf9a99e4ad/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java#l297

Fixes #1473

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=311235043
  • Loading branch information
Stephan202 authored and kluever committed May 13, 2020
1 parent e408026 commit cd83ca9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,11 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
// recompiles to avoid infinite recursion.
continue;
}
if ((key.equals("-source") || key.equals("-target")) && originalOptions.isSet("--release")) {
// javac does not allow -source and -target to be specified explicitly when --release is,
// but does add them in response to passing --release. Here we invert that operation.
continue;
}
options.put(key, value);
}
JavacTask newTask =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static com.google.errorprone.matchers.Matchers.isSameType;
import static com.google.errorprone.matchers.Matchers.staticMethod;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.junit.Assume.assumeTrue;

import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
Expand All @@ -44,6 +45,7 @@
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.util.ASTHelpers;
import com.google.errorprone.util.RuntimeVersion;
import com.sun.source.doctree.LinkTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ExpressionTree;
Expand Down Expand Up @@ -1212,6 +1214,31 @@ public void compilesWithFixTest() {
.doTest();
}

@Test
public void compilesWithFix_releaseFlag() {
assumeTrue(RuntimeVersion.isAtLeast9());
BugCheckerRefactoringTestHelper.newInstance(new CompilesWithFixChecker(), getClass())
.setArgs("--release", "9")
.addInputLines(
"in/Test.java",
"class Test {",
" void f() {",
" int x = 0;",
" int y = 1;",
" System.err.println(y);",
" }",
"}")
.addOutputLines(
"out/Test.java",
"class Test {",
" void f() {",
" int y = 1;",
" System.err.println(y);",
" }",
"}")
.doTest();
}

/** A test bugchecker that deletes an exception from throws. */
@BugPattern(name = "RemovesExceptionChecker", summary = "", severity = ERROR)
public static class RemovesExceptionsChecker extends BugChecker implements MethodTreeMatcher {
Expand Down

0 comments on commit cd83ca9

Please sign in to comment.