Skip to content

Commit

Permalink
Create a check for calls to FileSystems.newFileSystem that will be …
Browse files Browse the repository at this point in the history
…ambiguous in JDK 13

PiperOrigin-RevId: 494250886
  • Loading branch information
cushon authored and Error Prone Team committed Dec 9, 2022
1 parent ba7ce2e commit 5aa39dc
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
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) "));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
import com.google.errorprone.bugpatterns.NarrowingCompoundAssignment;
import com.google.errorprone.bugpatterns.NegativeCharLiteral;
import com.google.errorprone.bugpatterns.NestedInstanceOfConditions;
import com.google.errorprone.bugpatterns.NewFileSystem;
import com.google.errorprone.bugpatterns.NoAllocationChecker;
import com.google.errorprone.bugpatterns.NonAtomicVolatileUpdate;
import com.google.errorprone.bugpatterns.NonCanonicalStaticImport;
Expand Down Expand Up @@ -933,6 +934,7 @@ public static ScannerSupplier errorChecks() {
NarrowingCompoundAssignment.class,
NegativeCharLiteral.class,
NestedInstanceOfConditions.class,
NewFileSystem.class,
NonAtomicVolatileUpdate.class,
NonCanonicalType.class,
NonOverridingEquals.class,
Expand Down
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();
}
}
22 changes: 22 additions & 0 deletions docs/bugpattern/NewFileSystem.md
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);
```

0 comments on commit 5aa39dc

Please sign in to comment.