mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-inspections] IDEA-294920 "Standard charset can be used" breaks code when UEE is thrown from resource list
GitOrigin-RevId: 5959d27181c8a5b7e7f87b3da8001b8ba397a0b5
This commit is contained in:
committed by
intellij-monorepo-bot
parent
5f9f8a2665
commit
b5bda1d28f
+8
-9
@@ -25,10 +25,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.intellij.psi.CommonClassNames.*;
|
||||
import static java.util.Map.entry;
|
||||
@@ -298,19 +297,19 @@ public class CharsetObjectCanBeUsedInspection extends AbstractBaseJavaLocalInspe
|
||||
PsiTreeUtil.getParentOfType(anchor, PsiTryStatement.class, true, PsiMember.class, PsiLambdaExpression.class);
|
||||
if (tryStatement == null) break;
|
||||
PsiCodeBlock tryBlock = tryStatement.getTryBlock();
|
||||
if (PsiTreeUtil.isAncestor(tryBlock, anchor, true)) {
|
||||
boolean inTry = PsiTreeUtil.isAncestor(tryBlock, anchor, true);
|
||||
PsiResourceList resourceList = tryStatement.getResourceList();
|
||||
boolean inResource = PsiTreeUtil.isAncestor(resourceList, anchor, true);
|
||||
if (inTry || inResource) {
|
||||
for (PsiParameter parameter : tryStatement.getCatchBlockParameters()) {
|
||||
List<PsiTypeElement> typeElements = PsiUtil.getParameterTypeElements(parameter);
|
||||
for (PsiTypeElement element : typeElements) {
|
||||
PsiType type = element.getType();
|
||||
if (type.equalsToText("java.io.UnsupportedEncodingException") ||
|
||||
type.equalsToText("java.io.IOException")) {
|
||||
Collection<PsiClassType> unhandledExceptions = ExceptionUtil.collectUnhandledExceptions(tryBlock, tryBlock);
|
||||
PsiResourceList resourceList = tryStatement.getResourceList();
|
||||
if (resourceList != null) {
|
||||
Collection<PsiClassType> resourceExceptions = ExceptionUtil.collectUnhandledExceptions(resourceList, resourceList);
|
||||
unhandledExceptions = StreamEx.of(unhandledExceptions, resourceExceptions).toFlatList(Function.identity());
|
||||
}
|
||||
Set<PsiClassType> unhandledExceptions = StreamEx.of(tryBlock, resourceList).nonNull()
|
||||
.flatCollection(block -> ExceptionUtil.collectUnhandledExceptions(block, block))
|
||||
.toSet();
|
||||
if (!ContainerUtil.exists(unhandledExceptions, ue -> ue.isAssignableFrom(type) || type.isAssignableFrom(ue))) {
|
||||
if (parameter.getType() instanceof PsiDisjunctionType) {
|
||||
DeleteMultiCatchFix.deleteCaughtExceptionType(element);
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Replace with 'StandardCharsets.UTF_8'" "true"
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Demo {
|
||||
private static void test(InputStream is) {
|
||||
try (Stream<String> stream = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)).lines()) {
|
||||
stream.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// "Replace with 'StandardCharsets.UTF_8'" "true"
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Demo {
|
||||
private static void test(InputStream is) {
|
||||
try (Stream<String> stream = new BufferedReader(new InputStreamReader(is, "<caret>UTF-8")).lines()) {
|
||||
stream.forEach(System.out::println);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user