mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-05 01:50:56 +07:00
IOStreamConstructorInspection: do not suggest fix in cases when FileNotFoundException is already handled (IDEA-288738)
GitOrigin-RevId: f55d0d899196a9db2fc9dfc2ab01fc7c5edb51bf
This commit is contained in:
committed by
intellij-monorepo-bot
parent
0d90827381
commit
a028df8220
@@ -21,6 +21,7 @@ import com.siyeh.ig.PsiReplacementUtil;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ig.psiutils.ExpectedTypeUtils;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import com.siyeh.ig.psiutils.TypeUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -44,6 +45,7 @@ public class IOStreamConstructorInspection extends AbstractBaseJavaLocalInspecti
|
||||
StreamType streamType = constructorModel.myStreamType;
|
||||
PsiType expectedType = ExpectedTypeUtils.findExpectedType(newExpression, false);
|
||||
if (expectedType == null) return;
|
||||
if (isFileNotFoundHandled(newExpression)) return;
|
||||
boolean canUseBaseType = TypeConversionUtil.isAssignable(expectedType, streamType.baseType(newExpression));
|
||||
if (!canUseBaseType) return;
|
||||
boolean isInfoLevel = PsiUtil.isLanguageLevel10OrHigher(holder.getFile());
|
||||
@@ -55,6 +57,23 @@ public class IOStreamConstructorInspection extends AbstractBaseJavaLocalInspecti
|
||||
};
|
||||
}
|
||||
|
||||
private static boolean isFileNotFoundHandled(@NotNull PsiExpression context) {
|
||||
PsiClassType fileNotFoundType = TypeUtils.getType("java.io.FileNotFoundException", context);
|
||||
PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(context, PsiTryStatement.class, true, PsiMethod.class, PsiLambdaExpression.class);
|
||||
while (tryStatement != null) {
|
||||
PsiCatchSection[] catchSections = tryStatement.getCatchSections();
|
||||
for (PsiCatchSection catchSection : catchSections) {
|
||||
PsiParameter catchParameter = catchSection.getParameter();
|
||||
if (catchParameter != null && TypeConversionUtil.isAssignable(catchParameter.getType(), fileNotFoundType)) {
|
||||
PsiClassType ioException = TypeUtils.getType("java.io.IOException", context);
|
||||
return !TypeConversionUtil.isAssignable(catchParameter.getType(), ioException);
|
||||
}
|
||||
}
|
||||
tryStatement = PsiTreeUtil.getParentOfType(tryStatement, PsiTryStatement.class, true, PsiMethod.class, PsiLambdaExpression.class);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static @Nullable PsiExpression getOnlyArgument(@NotNull PsiCallExpression expression) {
|
||||
PsiExpressionList argumentList = expression.getArgumentList();
|
||||
if (argumentList == null) return null;
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
// "Replace with 'Files.newInputStream'" "true"
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
class Foo {
|
||||
void test(String str) {
|
||||
try {
|
||||
if (str.length() < 3) throw new FileNotFoundException("e");
|
||||
try (InputStream in = Files.newInputStream(Paths.get(str))) {
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.out.println("Don't know what happened");
|
||||
}
|
||||
}
|
||||
catch (FileNotFoundException e) {
|
||||
System.out.println("file not found exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Replace with 'Files.newInputStream'" "false"
|
||||
import java.io.*;
|
||||
|
||||
class Foo {
|
||||
void test(String str) {
|
||||
try(InputStream in = new FileInputStream(s<caret>tr)) {
|
||||
}
|
||||
catch (FileNotFoundException e) {
|
||||
System.out.println("file not found exception");
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.out.println("Don't know what happened");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Replace with 'Files.newInputStream'" "false"
|
||||
import java.io.*;
|
||||
|
||||
class Foo {
|
||||
void test(String str) {
|
||||
try(InputStream in = new FileInputStream(s<caret>tr)) {
|
||||
}
|
||||
catch (FileNotFoundException | RuntimeException e) {
|
||||
System.out.println("file not found exception");
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.out.println("Don't know what happened");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// "Replace with 'Files.newInputStream'" "true"
|
||||
import java.io.*;
|
||||
|
||||
class Foo {
|
||||
void test(String str) {
|
||||
try {
|
||||
if (str.length() < 3) throw new FileNotFoundException("e");
|
||||
try (InputStream in = new FileInputStream(st<caret>r)) {
|
||||
}
|
||||
catch (IOException e) {
|
||||
System.out.println("Don't know what happened");
|
||||
}
|
||||
}
|
||||
catch (FileNotFoundException e) {
|
||||
System.out.println("file not found exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user