mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-238645 Test fixes and some refactoring
GitOrigin-RevId: 5a3ac649c4639a0a2981608c1f503fbea72ce3be
This commit is contained in:
committed by
intellij-monorepo-bot
parent
fcf688ffa3
commit
6100adc63f
+26
@@ -0,0 +1,26 @@
|
||||
// "Fix all 'Redundant String operation' problems in file" "true"
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
class Main {
|
||||
|
||||
private static ByteArrayOutputStream foo() {
|
||||
return new ByteArrayOutputStream();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
|
||||
String s1 = out1.toString();
|
||||
|
||||
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
|
||||
String s2 = out2.toString();
|
||||
|
||||
ByteArrayOutputStream out3 = new ByteArrayOutputStream();
|
||||
String s3 = out3.toString();
|
||||
|
||||
ByteArrayOutputStream out4 = new ByteArrayOutputStream();
|
||||
String s4 = out4.toString();
|
||||
|
||||
String s5 = foo().toString();
|
||||
String s6 = foo().toString();
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
// "Fix all 'Redundant String operation' problems in file" "true"
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
class Main {
|
||||
|
||||
private static ByteArrayOutputStream foo() {
|
||||
return new ByteArrayOutputStream();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
|
||||
byte[] result1 = out1.toByteArray();
|
||||
String s1 = new String(result1);
|
||||
|
||||
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
|
||||
byte[] result2 = out2.toByteArray();
|
||||
String s2 = new String((result2));
|
||||
|
||||
ByteArrayOutputStream out3 = new ByteArrayOutputStream();
|
||||
String s3 = new String(out3.toByteArray());
|
||||
|
||||
ByteArrayOutputStream out4 = new ByteArrayOutputStream();
|
||||
String s4 = new String((out4.toByteArray()));
|
||||
|
||||
String s5 = new String(foo().toByteArray());
|
||||
String s6 = new String((foo().toByteArray()<caret>));
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,7 @@ public interface CommonClassNames {
|
||||
|
||||
String JAVA_UTIL_OPTIONAL = "java.util.Optional";
|
||||
|
||||
String JAVA_IO_BYTE_ARRAY_OUTPUT_STREAM = "java.io.ByteArrayOutputStream";
|
||||
String JAVA_IO_SERIALIZABLE = "java.io.Serializable";
|
||||
String JAVA_IO_EXTERNALIZABLE = "java.io.Externalizable";
|
||||
String JAVA_IO_SERIAL = "java.io.Serial";
|
||||
|
||||
+3
-1
@@ -2441,4 +2441,6 @@ junit5.converter.fixes.presentation.text=Convert Assertions
|
||||
convert.junit3.test.fix.conflict.semantics=Method call {0} may change semantics when {1} is converted to JUnit 4
|
||||
convert.junit3.test.fix.conflict.compile=Method call {0} will not compile when {1} is converted to JUnit 4
|
||||
convert.junit3.test.fix.conflict.compile.2=Reference {0} will not compile when {1} is converted to JUnit 4
|
||||
junit5.convert.fix.conflict.inheritor=Class {0} can''t be converted to JUnit 5, cause there are incompatible inheritor(s): {1}
|
||||
junit5.convert.fix.conflict.inheritor=Class {0} can''t be converted to JUnit 5, cause there are incompatible inheritor(s): {1}
|
||||
inspection.byte.array.output.stream.to.string.message=Inefficient conversion from ByteArrayOutputStream
|
||||
inspection.byte.array.output.stream.to.string.fix.name=Replace with ByteArrayOutputStream.toString()
|
||||
+79
@@ -12,6 +12,7 @@ import com.intellij.openapi.util.NlsSafe;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PsiLiteralUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
@@ -41,6 +42,8 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
|
||||
REPLACE_WITH_ARGUMENTS
|
||||
}
|
||||
|
||||
private static final CallMatcher BYTE_ARRAY_OUTPUT_STREAM_INTO_STRING =
|
||||
exactInstanceCall(JAVA_IO_BYTE_ARRAY_OUTPUT_STREAM, "toByteArray").parameterCount(0);
|
||||
private static final CallMatcher STRING_TO_STRING = exactInstanceCall(JAVA_LANG_STRING, "toString").parameterCount(0);
|
||||
private static final CallMatcher STRING_INTERN = exactInstanceCall(JAVA_LANG_STRING, "intern").parameterCount(0);
|
||||
private static final CallMatcher STRING_LENGTH = exactInstanceCall(JAVA_LANG_STRING, "length").parameterCount(0);
|
||||
@@ -158,11 +161,49 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
|
||||
return myManager.createProblemDescriptor(expression, range,
|
||||
InspectionGadgetsBundle.message("inspection.redundant.string.constructor.message"),
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL, myIsOnTheFly, fixes);
|
||||
} else if (TypeUtils.typeEquals("byte[]", arg.getType())) {
|
||||
arg = PsiUtil.skipParenthesizedExprDown(arg);
|
||||
|
||||
final ProblemDescriptor descriptor = getByteArrayOutputStreamToStringProblem(expression, arg, null);
|
||||
if (descriptor != null) return descriptor;
|
||||
|
||||
if (!(arg instanceof PsiReferenceExpression)) return null;
|
||||
|
||||
final PsiElement intermediateArrayCandidate = ((PsiReferenceExpression)arg).resolve();
|
||||
if (intermediateArrayCandidate == null) return null;
|
||||
|
||||
if (ReferencesSearch.search(intermediateArrayCandidate).findAll().size() != 1) return null;
|
||||
|
||||
if (intermediateArrayCandidate instanceof PsiLocalVariable) {
|
||||
final PsiExpression initializer = ((PsiLocalVariable) intermediateArrayCandidate).getInitializer();
|
||||
return getByteArrayOutputStreamToStringProblem(expression, initializer, intermediateArrayCandidate);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private ProblemDescriptor getByteArrayOutputStreamToStringProblem(
|
||||
PsiNewExpression expression,
|
||||
PsiExpression methodCallCandidate,
|
||||
PsiElement intermediateArray
|
||||
) {
|
||||
if (methodCallCandidate instanceof PsiMethodCallExpression &&
|
||||
BYTE_ARRAY_OUTPUT_STREAM_INTO_STRING.test((PsiMethodCallExpression)methodCallCandidate)) {
|
||||
|
||||
final TextRange range = new TextRange(0, expression.getTextLength());
|
||||
|
||||
final PsiElement qualifier = ((PsiMethodCallExpression) methodCallCandidate).getMethodExpression().getQualifier();
|
||||
if (qualifier == null) return null;
|
||||
|
||||
final LocalQuickFix fix = new ByteArrayOutputStreamToStringFix(qualifier.getText(), intermediateArray);
|
||||
return myManager.createProblemDescriptor(expression, range,
|
||||
InspectionGadgetsBundle.message("inspection.byte.array.output.stream.to.string.message"),
|
||||
ProblemHighlightType.WARNING, myIsOnTheFly, fix);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private ProblemDescriptor getRedundantCaseEqualsProblem(PsiMethodCallExpression call) {
|
||||
|
||||
PsiExpression equalTo = PsiUtil.skipParenthesizedExprDown(call.getArgumentList().getExpressions()[0]);
|
||||
@@ -859,4 +900,42 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
|
||||
PsiReplacementUtil.replaceExpression(expression, argText, commentTracker);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class ByteArrayOutputStreamToStringFix extends InspectionGadgetsFix {
|
||||
private final String elementToString;
|
||||
private final SmartPsiElementPointer<PsiElement> intermediateArrayPointer;
|
||||
|
||||
private ByteArrayOutputStreamToStringFix(String s, PsiElement intermediateArray) {
|
||||
this.elementToString = s;
|
||||
if (intermediateArray == null) {
|
||||
intermediateArrayPointer = null;
|
||||
} else {
|
||||
final SmartPointerManager pointerManager = SmartPointerManager.getInstance(intermediateArray.getProject());
|
||||
intermediateArrayPointer = pointerManager.createSmartPsiElementPointer(intermediateArray);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return InspectionGadgetsBundle.message("inspection.byte.array.output.stream.to.string.fix.name");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return CommonQuickFixBundle.message("fix.simplify");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFix(Project project, ProblemDescriptor descriptor) {
|
||||
if (elementToString == null) return;
|
||||
final PsiNewExpression expression = (PsiNewExpression)descriptor.getPsiElement();
|
||||
PsiReplacementUtil.replaceExpression(expression, elementToString + ".toString()", new CommentTracker());
|
||||
if (intermediateArrayPointer == null) return;
|
||||
final PsiElement intermediateArray = intermediateArrayPointer.getElement();
|
||||
if (intermediateArray == null) return;
|
||||
deleteElement(intermediateArray);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user