IOStreamConstructorInspection: cr fixes (IJ-CR-20017):

1. report for arbitrary expressions that expect OutputStream / InputStream
2. report for arbitrary file creation arguments
3. check recursively if file creation argument is effectively final
4. reuse effectively final path variables if possible
5. use Paths.get() instead of Path.of() since 'of' method appeared only in java 11
6. info level for java 10 and higher

GitOrigin-RevId: 0a778328fe938faa0fe7eb2f199064f90b7a6d1b
This commit is contained in:
Artemiy Sartakov
2022-02-02 14:59:36 +07:00
committed by intellij-monorepo-bot
parent 4c619e8728
commit c699fe0e24
24 changed files with 230 additions and 106 deletions

View File

@@ -0,0 +1,14 @@
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
import java.nio.file.Files;
class Foo {
void test(boolean b, File f) throws IOException {
Path p;
if (b) {
p = f.toPath();
try (InputStream is = Files.newInputStream(p)) {
}
}
}
}

View File

@@ -1,11 +1,11 @@
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
class Foo {
void test(File file) {
try (InputStream is = Files.newInputStream(Path.of("foo"))) {
try (InputStream is = Files.newInputStream(Paths.get("foo"))) {
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@@ -0,0 +1,12 @@
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
import java.nio.file.Files;
class Foo {
void test(File file) {
try (InputStream is = Files.newInputStream(new File("foo").toPath())) {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,11 +1,11 @@
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
class Foo {
void test(String str) {
try (InputStream is = Files.newInputStream(Path.of(str))) {
try (InputStream is = Files.newInputStream(Paths.get(str))) {
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@@ -0,0 +1,12 @@
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
import java.nio.file.Files;
class Foo {
void test(File file, boolean b) {
try (InputStream is = Files.newInputStream((b ? new File("foo") : new File("bar")).toPath())) {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,15 @@
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
class Foo {
void test(String path, boolean b, boolean b1) {
Path name = Paths.get(b ? b1 ? "foo" : "bar" : "baz");
path = "bar";
try (InputStream is = Files.newInputStream(name)) {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,15 @@
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
class Foo {
void test(String path, boolean b, boolean b1) {
Path name = Paths.get(b ? b1 ? "foo" : path : "baz");
path = "bar";
try (InputStream is = Files.newInputStream(Paths.get(b ? b1 ? "foo" : path : "baz"))) {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,8 +1,8 @@
// "Fix all '`InputStream' and 'OutputStream' can be constructed using 'Files' methods' problems in file" "true"
// "Fix all ''InputStream' and 'OutputStream' can be constructed using 'Files' methods' problems in file" "true"
import java.io.*;
import java.nio.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
class Foo {
void test(File file, String str) {
@@ -17,7 +17,7 @@ class Foo {
throw new RuntimeException(e);
}
Path strPath = Path.of(str);
Path strPath = Paths.get(str);
try (InputStream is = Files.newInputStream(strPath)) {
} catch (IOException e) {
throw new RuntimeException(e);

View File

@@ -1,7 +1,8 @@
// "Fix all '`InputStream' and 'OutputStream' can be constructed using 'Files' methods' problems in file" "true"
// "Fix all ''InputStream' and 'OutputStream' can be constructed using 'Files' methods' problems in file" "true"
import java.io.*;
import java.nio.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
class Foo {
void test(File file, String str) {
@@ -15,12 +16,12 @@ class Foo {
throw new RuntimeException(e);
}
try (InputStream is = Files.newInputStream(Path.of(str))) {
try (InputStream is = Files.newInputStream(Paths.get(str))) {
} catch (IOException e) {
throw new RuntimeException(e);
}
try (InputStream is = Files.newInputStream(Path.of(str))) {
try (InputStream is = Files.newInputStream(Paths.get(str))) {
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@@ -1,8 +1,8 @@
// "Fix all '`InputStream' and 'OutputStream' can be constructed using 'Files' methods' problems in file" "true"
// "Fix all ''InputStream' and 'OutputStream' can be constructed using 'Files' methods' problems in file" "true"
import java.io.*;
import java.nio.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
class Foo {
void test(File file, String str) {
@@ -12,21 +12,21 @@ class Foo {
throw new RuntimeException(e);
}
filePath = Path.of("other");
filePath = Paths.get("other");
try (InputStream is = Files.newInputStream(file.toPath())) {
} catch (IOException e) {
throw new RuntimeException(e);
}
Path strPath = Path.of(str);
Path strPath = Paths.get(str);
for (int i = 0; i < 10; i++) {
try (InputStream is = Files.newInputStream(Path.of(str))){
try (InputStream is = Files.newInputStream(Paths.get(str))){
} catch(IOException e){
throw new RuntimeException(e);
}
strPath = Path.of("foo");
strPath = Paths.get("foo");
try (InputStream is = Files.newInputStream(strPath)) {
}
catch (IOException e) {

View File

@@ -1,11 +1,12 @@
// "Replace with 'Files.newOutputStream'" "true"
import java.io.*;
import java.nio.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
class Foo {
void test(String str, boolean b) {
try (OutputStream os = (b ? (Files.newOutputStream(Path.of("foo"))) : (Files.newOutputStream(Path.of(str))))) {
try (OutputStream os = (b ? (Files.newOutputStream(Paths.get("foo"))) : (Files.newOutputStream(Paths.get(str))))) {
} catch (IOException e) {}
}
}

View File

@@ -0,0 +1,13 @@
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
class Foo {
void test(boolean b, File f) throws IOException {
Path p;
if (b) {
p = f.toPath();
try (InputStream is = new FileI<caret>nputStream(f)) {
}
}
}
}

View File

@@ -1,4 +1,4 @@
// "Replace with 'Files.newInputStream'" "false"
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
class Foo {

View File

@@ -0,0 +1,11 @@
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
class Foo {
void test(File file, boolean b) {
try (InputStream is = new FileInpu<caret>tStream(b ? new File("foo") : new File("bar"))) {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,14 @@
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
import java.nio.file.Paths;
class Foo {
void test(String path, boolean b, boolean b1) {
Path name = Paths.get(b ? b1 ? "foo" : "bar" : "baz");
path = "bar";
try (InputStream is = new FileInputStre<caret>am(b ? b1 ? "foo" : "bar" : "baz")) {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,14 @@
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
import java.nio.file.Paths;
class Foo {
void test(String path, boolean b, boolean b1) {
Path name = Paths.get(b ? b1 ? "foo" : path : "baz");
path = "bar";
try (InputStream is = new FileInputStre<caret>am(b ? b1 ? "foo" : path : "baz")) {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,4 +1,4 @@
// "Fix all '`InputStream' and 'OutputStream' can be constructed using 'Files' methods' problems in file" "true"
// "Fix all ''InputStream' and 'OutputStream' can be constructed using 'Files' methods' problems in file" "true"
import java.io.*;
import java.nio.*;
@@ -15,7 +15,7 @@ class Foo {
throw new RuntimeException(e);
}
Path strPath = Path.of(str);
Path strPath = Paths.get(str);
try (InputStream is = new FileInp<caret>utStream(str)) {
} catch (IOException e) {
throw new RuntimeException(e);

View File

@@ -1,5 +1,6 @@
// "Fix all '`InputStream' and 'OutputStream' can be constructed using 'Files' methods' problems in file" "true"
// "Fix all ''InputStream' and 'OutputStream' can be constructed using 'Files' methods' problems in file" "true"
import java.io.*;
import java.nio.*;
class Foo {
void test(File file, String str) {
@@ -18,7 +19,7 @@ class Foo {
throw new RuntimeException(e);
}
try (InputStream is = Files.newInputStream(Path.of(str))) {
try (InputStream is = Files.newInputStream(Paths.get(str))) {
} catch (IOException e) {
throw new RuntimeException(e);
}

View File

@@ -1,4 +1,4 @@
// "Fix all '`InputStream' and 'OutputStream' can be constructed using 'Files' methods' problems in file" "true"
// "Fix all ''InputStream' and 'OutputStream' can be constructed using 'Files' methods' problems in file" "true"
import java.io.*;
import java.nio.*;
@@ -10,21 +10,21 @@ class Foo {
throw new RuntimeException(e);
}
filePath = Path.of("other");
filePath = Paths.get("other");
try (InputStream is = new FileInputStream(file)) {
} catch (IOException e) {
throw new RuntimeException(e);
}
Path strPath = Path.of(str);
Path strPath = Paths.get(str);
for (int i = 0; i < 10; i++) {
try (InputStream is = new FileInp<caret>utStream(str)){
} catch(IOException e){
throw new RuntimeException(e);
}
strPath = Path.of("foo");
strPath = Paths.get("foo");
try (InputStream is = Files.newInputStream(strPath)) {
}
catch (IOException e) {

View File

@@ -1,9 +1,10 @@
// "Replace with 'Files.newOutputStream'" "true"
import java.io.*;
import java.nio.*;
class Foo {
void test(String str, boolean b) {
try (OutputStream os = (b ? (Files.newOutputStream(Path.of("foo"))) : (new FileO<caret>utputStream(str)))) {
try (OutputStream os = (b ? (Files.newOutputStream(Paths.get("foo"))) : (new FileO<caret>utputStream(str)))) {
} catch (IOException e) {}
}
}