mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Support the case when then array initializer is taken into parentheses; more attention to comments in fix: IDEA-238645
GitOrigin-RevId: d4557daa9b37e9f103b6453d9e0ad3a18df0a870
This commit is contained in:
committed by
intellij-monorepo-bot
parent
a7668df48f
commit
50af51d8d1
+1
-1
@@ -13,7 +13,7 @@ class Main {
|
||||
String s1 = new String(result1);
|
||||
|
||||
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
|
||||
byte[] result2 = out2.toByteArray();
|
||||
byte[] result2 = (((out2.toByteArray()));
|
||||
String s2 = new String((result2));
|
||||
|
||||
ByteArrayOutputStream out3 = new ByteArrayOutputStream();
|
||||
|
||||
+2
-2
@@ -13,11 +13,11 @@ class Main {
|
||||
Charset charset = Charset.defaultCharset();
|
||||
|
||||
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
|
||||
byte[] result1 = out1.toByteArray();
|
||||
byte[] result1 = (out1.toByteArray());
|
||||
String s1 = new String(result1, charset);
|
||||
|
||||
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
|
||||
byte[] result2 = out2.toByteArray();
|
||||
byte[] result2 = (((out2.toByteArray())));
|
||||
String s2 = new String((result2), (charset));
|
||||
|
||||
ByteArrayOutputStream out3 = new ByteArrayOutputStream();
|
||||
|
||||
+2
-2
@@ -12,11 +12,11 @@ class Main {
|
||||
String csn = "ISO-8859-1";
|
||||
|
||||
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
|
||||
byte[] result1 = out1.toByteArray();
|
||||
byte[] result1 = (out1.toByteArray());
|
||||
String s1 = new String(result1, csn);
|
||||
|
||||
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
|
||||
byte[] result2 = out2.toByteArray();
|
||||
byte[] result2 = (((out2.toByteArray())));
|
||||
String s2 = new String((result2), (csn));
|
||||
|
||||
ByteArrayOutputStream out3 = new ByteArrayOutputStream();
|
||||
|
||||
+17
-6
@@ -151,8 +151,7 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
|
||||
final PsiExpression[] params = args.getExpressions();
|
||||
|
||||
if (isNewStringFromByteArrayParams(params)) {
|
||||
PsiMethodCallExpression methodCall = tryCast(ExpressionUtils.resolveExpression(params[0]), PsiMethodCallExpression.class);
|
||||
if (methodCall == null) return null;
|
||||
PsiMethodCallExpression methodCall = getMethodCallExpression(params[0]);
|
||||
|
||||
if (BYTE_ARRAY_OUTPUT_STREAM_INTO_BYTE_ARRAY.test(methodCall)) {
|
||||
final TextRange range = new TextRange(0, expression.getTextLength());
|
||||
@@ -897,6 +896,12 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiMethodCallExpression getMethodCallExpression(PsiExpression expression) {
|
||||
PsiExpression resolvedExpression = PsiUtil.skipParenthesizedExprDown(ExpressionUtils.resolveExpression(expression));
|
||||
return tryCast(resolvedExpression, PsiMethodCallExpression.class);
|
||||
}
|
||||
|
||||
private static final class ByteArrayOutputStreamToStringFix extends InspectionGadgetsFix {
|
||||
private final String myText;
|
||||
|
||||
@@ -927,13 +932,19 @@ public class RedundantStringOperationInspection extends AbstractBaseJavaLocalIns
|
||||
final PsiExpression[] params = args.getExpressions();
|
||||
if (!(params.length == 1 || params.length == 2)) return;
|
||||
|
||||
PsiMethodCallExpression resolvedExpression = tryCast(ExpressionUtils.resolveExpression(params[0]), PsiMethodCallExpression.class);
|
||||
PsiMethodCallExpression resolvedExpression = getMethodCallExpression(params[0]);
|
||||
if (resolvedExpression == null) return;
|
||||
|
||||
PsiElement parent = tryCast(resolvedExpression.getParent(), PsiLocalVariable.class);
|
||||
if (parent != null) deleteElement(parent);
|
||||
final PsiElement qualifier = resolvedExpression.getMethodExpression().getQualifier();
|
||||
if (qualifier == null) return;
|
||||
|
||||
PsiReplacementUtil.replaceExpression(expression, myText, new CommentTracker());
|
||||
CommentTracker ct = new CommentTracker();
|
||||
String newText = ct.text(qualifier) + ".toString(" + (params.length == 2 ? ct.text(params[1]) : "") + ")";
|
||||
|
||||
PsiElement parent = tryCast(PsiUtil.skipParenthesizedExprUp(resolvedExpression.getParent()), PsiLocalVariable.class);
|
||||
if (parent != null) ct.delete(parent);
|
||||
|
||||
ct.replaceAndRestoreComments(expression, newText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user