RedundantFileCreationInspection created: IDEA-249165

GitOrigin-RevId: 2d8ea0c12c2320d5f8b0e685b9a047bf3f972852
This commit is contained in:
Andrey.Cherkasov
2020-09-04 09:42:42 +00:00
committed by intellij-monorepo-bot
parent 0dad544331
commit a65ac28d32
10 changed files with 215 additions and 0 deletions
@@ -1373,6 +1373,12 @@
bundle="messages.JavaBundle"
key="inspection.redundant.explicit.close"
implementationClass="com.intellij.codeInspection.RedundantExplicitCloseInspection"/>
<localInspection groupPath="Java" language="JAVA" shortName="RedundantFileCreation"
groupBundle="messages.InspectionsBundle"
groupKey="group.names.declaration.redundancy" enabledByDefault="true" level="WARNING"
bundle="messages.JavaBundle"
key="inspection.redundant.file.creation.display.name"
implementationClass="com.intellij.codeInspection.RedundantFileCreationInspection"/>
<localInspection groupPathKey="group.path.names.java.language.level.specific.issues.and.migration.aids" language="JAVA" shortName="TextBlockMigration"
groupBundle="messages.InspectionsBundle"
groupKey="group.names.language.level.specific.issues.and.migration.aids15" enabledByDefault="true" level="WARNING"
@@ -0,0 +1,98 @@
// 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.codeInspection;
import com.intellij.java.JavaBundle;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
public class RedundantFileCreationInspection extends AbstractBaseJavaLocalInspectionTool {
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new JavaElementVisitor() {
@Override
public void visitNewExpression(PsiNewExpression newExpression) {
super.visitNewExpression(newExpression);
final String[] targetTypes = new String[] {
"java.io.FileInputStream", "java.io.FileOutputStream", "java.io.FileReader","java.io.FileWriter",
"java.io.PrintStream", "java.io.PrintWriter", "java.util.Formatter"
};
final PsiType type = newExpression.getType();
if (!TypeUtils.typeEquals(type, targetTypes)) {
return;
}
final PsiMethod streamConstructor = newExpression.resolveConstructor();
if (streamConstructor == null) return;
final PsiParameter[] streamParams = streamConstructor.getParameterList().getParameters();
if (streamParams.length != 1) return;
if (!TypeUtils.typeEquals("java.io.File", streamParams[0].getType())) return;
final PsiExpressionList streamArgList = newExpression.getArgumentList();
if (streamArgList == null) return;
final PsiExpression[] streamArgs = streamArgList.getExpressions();
if (streamArgs.length != 1) return;
PsiExpression streamArg = streamArgs[0];
if (!(streamArg instanceof PsiNewExpression)) return;
final PsiMethod fileConstructor = ((PsiNewExpression)streamArg).resolveConstructor();
if (fileConstructor == null) return;
final PsiParameter[] fileParams = fileConstructor.getParameterList().getParameters();
if (fileParams.length != 1) return;
if (!TypeUtils.isJavaLangString(fileParams[0].getType())) return;
PsiExpressionList fileArgList = ((PsiNewExpression)streamArg).getArgumentList();
if (fileArgList == null) return;
holder.registerProblem(streamArg,
JavaBundle.message("inspection.redundant.file.creation.description"),
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
new TextRange(0, fileArgList.getStartOffsetInParent()),
new DeleteRedundantFileCreationFix());
}
};
}
private static class DeleteRedundantFileCreationFix implements LocalQuickFix {
@Nls
@NotNull
@Override
public String getFamilyName() {
return JavaBundle.message("inspection.redundant.file.creation.quickfix");
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
assert element instanceof PsiNewExpression;
final PsiNewExpression newExpression = (PsiNewExpression)descriptor.getPsiElement();
final PsiExpressionList argList = newExpression.getArgumentList();
assert argList != null;
final PsiExpression[] args = argList.getExpressions();
assert args.length == 1;
CommentTracker commentTracker = new CommentTracker();
final String argText = commentTracker.text(args[0]);
PsiReplacementUtil.replaceExpression(newExpression, argText, commentTracker);
}
}
}
@@ -0,0 +1,8 @@
<html>
<body>
Redundant File creation in a stream constructor
<!-- tooltip end -->
<p>
</body>
</html>
@@ -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");
}
}
@@ -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());
}
}
@@ -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 Fi<caret>le("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"));
}
}
@@ -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 Fi<caret>le(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()));
}
}
@@ -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";
}
}
@@ -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=<code>#ref</code> 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'
@@ -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);