From a65ac28d3267ec2ae1e4d6b804aa3df9a9c33333 Mon Sep 17 00:00:00 2001 From: "Andrey.Cherkasov" Date: Fri, 4 Sep 2020 11:01:48 +0300 Subject: [PATCH] RedundantFileCreationInspection created: IDEA-249165 GitOrigin-RevId: 2d8ea0c12c2320d5f8b0e685b9a047bf3f972852 --- java/java-impl/src/META-INF/JavaPlugin.xml | 6 ++ .../RedundantFileCreationInspection.java | 98 +++++++++++++++++++ .../RedundantFileCreation.html | 8 ++ .../afterTakeStringDirectly.java | 15 +++ .../afterTakeStringFromMethod.java | 19 ++++ .../beforeTakeStringDirectly.java | 15 +++ .../beforeTakeStringFromMethod.java | 19 ++++ .../RedundantFileCreationInspectionTest.java | 22 +++++ .../resources/messages/JavaBundle.properties | 3 + .../src/com/siyeh/ig/psiutils/TypeUtils.java | 10 ++ 10 files changed, 215 insertions(+) create mode 100644 java/java-impl/src/com/intellij/codeInspection/RedundantFileCreationInspection.java create mode 100644 java/java-impl/src/inspectionDescriptions/RedundantFileCreation.html create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/afterTakeStringDirectly.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/afterTakeStringFromMethod.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/beforeTakeStringDirectly.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/beforeTakeStringFromMethod.java create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/RedundantFileCreationInspectionTest.java diff --git a/java/java-impl/src/META-INF/JavaPlugin.xml b/java/java-impl/src/META-INF/JavaPlugin.xml index 7bd1a47ee05b..dc9b5922d13c 100644 --- a/java/java-impl/src/META-INF/JavaPlugin.xml +++ b/java/java-impl/src/META-INF/JavaPlugin.xml @@ -1373,6 +1373,12 @@ bundle="messages.JavaBundle" key="inspection.redundant.explicit.close" implementationClass="com.intellij.codeInspection.RedundantExplicitCloseInspection"/> + + +Redundant File creation in a stream constructor + +

+ + + \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/afterTakeStringDirectly.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/afterTakeStringDirectly.java new file mode 100644 index 000000000000..dc8ab6a205c0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/afterTakeStringDirectly.java @@ -0,0 +1,15 @@ +// "Fix all 'Redundant file creation' problems in file" "true" +import java.io.*; +import java.util.Formatter; + +class Main { + public main(String[] args) { + InputStream is = new FileInputStream("1.txt"); + OutputStream os = new FileOutputStream("2.txt"); + FileReader fr = new FileReader("3.txt"); + FileWriter fw = new FileWriter("4.txt"); + PrintStream ps = new PrintStream("5.txt"); + PrintWriter pw = new PrintWriter("6.txt"); + Formatter f = new Formatter("7.txt"); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/afterTakeStringFromMethod.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/afterTakeStringFromMethod.java new file mode 100644 index 000000000000..fdc60e063ba7 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/afterTakeStringFromMethod.java @@ -0,0 +1,19 @@ +// "Fix all 'Redundant file creation' problems in file" "true" +import java.io.*; +import java.util.Formatter; + +class Main { + private static String getSomePathname() { + return "Some pathname"; + } + + public main(String[] args) { + InputStream is = new FileInputStream(getSomePathname()); + OutputStream os = new FileOutputStream(getSomePathname()); + FileReader fr = new FileReader(getSomePathname()); + FileWriter fw = new FileWriter(getSomePathname()); + PrintStream ps = new PrintStream(getSomePathname()); + PrintWriter pw = new PrintWriter(getSomePathname()); + Formatter f = new Formatter(getSomePathname()); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/beforeTakeStringDirectly.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/beforeTakeStringDirectly.java new file mode 100644 index 000000000000..0fd08bbce202 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/beforeTakeStringDirectly.java @@ -0,0 +1,15 @@ +// "Fix all 'Redundant file creation' problems in file" "true" +import java.io.*; +import java.util.Formatter; + +class Main { + public main(String[] args) { + InputStream is = new FileInputStream(new File("1.txt")); + OutputStream os = new FileOutputStream(new File("2.txt")); + FileReader fr = new FileReader(new File("3.txt")); + FileWriter fw = new FileWriter(new File("4.txt")); + PrintStream ps = new PrintStream(new File("5.txt")); + PrintWriter pw = new PrintWriter(new File("6.txt")); + Formatter f = new Formatter(new File("7.txt")); + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/beforeTakeStringFromMethod.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/beforeTakeStringFromMethod.java new file mode 100644 index 000000000000..c477cbb5dc7f --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation/beforeTakeStringFromMethod.java @@ -0,0 +1,19 @@ +// "Fix all 'Redundant file creation' problems in file" "true" +import java.io.*; +import java.util.Formatter; + +class Main { + private static String getSomePathname() { + return "Some pathname"; + } + + public main(String[] args) { + InputStream is = new FileInputStream(new File(getSomePathname())); + OutputStream os = new FileOutputStream(new File(getSomePathname())); + FileReader fr = new FileReader(new File(getSomePathname())); + FileWriter fw = new FileWriter(new File(getSomePathname())); + PrintStream ps = new PrintStream(new File(getSomePathname())); + PrintWriter pw = new PrintWriter(new File(getSomePathname())); + Formatter f = new Formatter(new File(getSomePathname())); + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/RedundantFileCreationInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/RedundantFileCreationInspectionTest.java new file mode 100644 index 000000000000..a64bbd951607 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/RedundantFileCreationInspectionTest.java @@ -0,0 +1,22 @@ +// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.java.codeInsight.daemon.quickFix; + +import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; +import com.intellij.codeInspection.LocalInspectionTool; +import com.intellij.codeInspection.RedundantFileCreationInspection; +import org.jetbrains.annotations.NotNull; + + +public class RedundantFileCreationInspectionTest extends LightQuickFixParameterizedTestCase { + @Override + protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() { + return new LocalInspectionTool[]{ + new RedundantFileCreationInspection() + }; + } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/redundantFileCreation"; + } +} diff --git a/java/openapi/resources/messages/JavaBundle.properties b/java/openapi/resources/messages/JavaBundle.properties index 7aa20e87322b..34e14691d758 100644 --- a/java/openapi/resources/messages/JavaBundle.properties +++ b/java/openapi/resources/messages/JavaBundle.properties @@ -567,6 +567,9 @@ inspection.redundant.array.creation.display.name=Redundant array creation inspection.redundant.array.creation.for.varargs.call.descriptor=Redundant array creation for calling varargs method inspection.redundant.array.creation.quickfix=Remove explicit array creation inspection.redundant.explicit.close=Redundant 'close()' +inspection.redundant.file.creation.display.name=Redundant file creation +inspection.redundant.file.creation.description=#ref is redundant #loc +inspection.redundant.file.creation.quickfix=Replace with argument inspection.redundant.null.check.always.fail.message=Null-check will always fail: {0} is never null inspection.redundant.null.check.fix.family.name=Remove redundant null-check inspection.redundant.null.check.fix.notnull.family.name=Remove erroneous '!= null' diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java index 1ee7db623943..1c828392bc93 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java @@ -54,6 +54,16 @@ public final class TypeUtils { return targetType != null && targetType.equalsToText(typeName); } + @Contract("null, _ -> false") + public static boolean typeEquals(@Nullable PsiType targetType, @NonNls String @NotNull ... typeNames) { + for (String typeName : typeNames) { + if (typeEquals(typeName, targetType)) { + return true; + } + } + return false; + } + public static PsiClassType getType(@NotNull String fqName, @NotNull PsiElement context) { final Project project = context.getProject(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);