-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a check for calls to
FileSystems.newFileSystem
that will be …
…ambiguous in JDK 13 PiperOrigin-RevId: 494250886
- Loading branch information
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
core/src/main/java/com/google/errorprone/bugpatterns/NewFileSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2022 The Error Prone Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.errorprone.bugpatterns; | ||
|
||
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; | ||
import static com.google.errorprone.matchers.Description.NO_MATCH; | ||
|
||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.fixes.SuggestedFix; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.matchers.method.MethodMatchers; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import com.sun.source.tree.Tree; | ||
|
||
/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */ | ||
@BugPattern( | ||
summary = "Starting in JDK 13, this call is ambiguous with FileSystem.newFileSystem(Path,Map)", | ||
severity = WARNING) | ||
public class NewFileSystem extends BugChecker implements BugChecker.MethodInvocationTreeMatcher { | ||
|
||
private static final Matcher<ExpressionTree> MATCHER = | ||
MethodMatchers.staticMethod() | ||
.onClass("java.nio.file.FileSystems") | ||
.named("newFileSystem") | ||
.withParameters("java.nio.file.Path", "java.lang.ClassLoader"); | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
if (!MATCHER.matches(tree, state)) { | ||
return NO_MATCH; | ||
} | ||
ExpressionTree expressionTree = tree.getArguments().get(1); | ||
if (!expressionTree.getKind().equals(Tree.Kind.NULL_LITERAL)) { | ||
return NO_MATCH; | ||
} | ||
return describeMatch(tree, SuggestedFix.prefixWith(expressionTree, "(ClassLoader) ")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
core/src/test/java/com/google/errorprone/bugpatterns/NewFileSystemTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2022 The Error Prone Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.errorprone.bugpatterns; | ||
|
||
import static org.junit.Assume.assumeFalse; | ||
|
||
import com.google.errorprone.BugCheckerRefactoringTestHelper; | ||
import com.google.errorprone.CompilationTestHelper; | ||
import com.google.errorprone.util.RuntimeVersion; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class NewFileSystemTest { | ||
|
||
private final CompilationTestHelper testHelper = | ||
CompilationTestHelper.newInstance(NewFileSystem.class, getClass()); | ||
|
||
private final BugCheckerRefactoringTestHelper refactoringTestHelper = | ||
BugCheckerRefactoringTestHelper.newInstance(NewFileSystem.class, getClass()); | ||
|
||
@Test | ||
public void refactoring() { | ||
assumeFalse(RuntimeVersion.isAtLeast13()); | ||
refactoringTestHelper | ||
.addInputLines( | ||
"Test.java", | ||
"import java.nio.file.FileSystems;", | ||
"import java.nio.file.Paths;", | ||
"import java.io.IOException;", | ||
"class Test {", | ||
" void f() throws IOException {", | ||
" FileSystems.newFileSystem(Paths.get(\".\"), null);", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"Test.java", | ||
"import java.nio.file.FileSystems;", | ||
"import java.nio.file.Paths;", | ||
"import java.io.IOException;", | ||
"class Test {", | ||
" void f() throws IOException {", | ||
" FileSystems.newFileSystem(Paths.get(\".\"), (ClassLoader) null);", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void negative() { | ||
testHelper | ||
.addSourceLines( | ||
"Test.java", | ||
"import java.nio.file.FileSystems;", | ||
"import java.nio.file.Paths;", | ||
"import java.io.IOException;", | ||
"class Test {", | ||
" void f() throws IOException {", | ||
" FileSystems.newFileSystem(Paths.get(\".\"), (ClassLoader) null);", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Starting in JDK 13, calls to `FileSystem.newFileSystem(path, null)` are | ||
ambiguous. | ||
|
||
The calls match both: | ||
|
||
* [`FileSystem.newFileSystem(Path, ClassLoader)`](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/nio/file/FileSystems.html#newFileSystem\(java.nio.file.Path,java.lang.ClassLoader\)) | ||
* [`FileSystem.newFileSystem(Path, Map<?, ?>)`](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/nio/file/FileSystems.html#newFileSystem\(java.nio.file.Path,java.util.Map\)) | ||
|
||
To disambiguate, add a cast to the desired type, to preserve the pre-JDK 13 | ||
behaviour. | ||
|
||
That is, prefer this: | ||
|
||
```java | ||
FileSystem.newFileSystem(path, (ClassLoader) null); | ||
``` | ||
|
||
Instead of this: | ||
|
||
```java | ||
FileSystem.newFileSystem(path, null); | ||
``` |