mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-06 20:39:40 +07:00
ExplicitArrayFillingInspection: cast filled value if needed (IDEA-223102)
GitOrigin-RevId: b5e3b4ace9840968853f95ad80339d7084308d7a
This commit is contained in:
committed by
intellij-monorepo-bot
parent
0947108630
commit
0a9b74a5d5
@@ -16,6 +16,7 @@ import com.intellij.psi.controlFlow.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.siyeh.ig.psiutils.*;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -251,8 +252,9 @@ public class ExplicitArrayFillingInspection extends AbstractBaseJavaLocalInspect
|
||||
CommentTracker ct = new CommentTracker();
|
||||
PsiElement result;
|
||||
if (myIsRhsConstant) {
|
||||
String cast = getCast(project, container.getElementType(), rValue.getType());
|
||||
String replacement = CommonClassNames.JAVA_UTIL_ARRAYS + ".fill(" +
|
||||
ct.text(container.getQualifier()) + ", " + ct.text(rValue) + ");";
|
||||
ct.text(container.getQualifier()) + ", " + cast + ct.text(rValue) + ");";
|
||||
result = ct.replaceAndRestoreComments(statement, replacement);
|
||||
}
|
||||
else {
|
||||
@@ -264,5 +266,13 @@ public class ExplicitArrayFillingInspection extends AbstractBaseJavaLocalInspect
|
||||
result = JavaCodeStyleManager.getInstance(project).shortenClassReferences(result);
|
||||
CodeStyleManager.getInstance(project).reformat(result);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String getCast(@NotNull Project project, @Nullable PsiType elementType, @Nullable PsiType rType) {
|
||||
if (elementType == null || rType == null) return "";
|
||||
PsiType assignTo = tryCast(elementType, PsiPrimitiveType.class);
|
||||
if (assignTo == null) assignTo = JavaPsiFacade.getElementFactory(project).createTypeByFQClassName(CommonClassNames.JAVA_LANG_OBJECT);
|
||||
return TypeConversionUtil.isAssignable(assignTo, rType) ? "" : "(" + elementType.getCanonicalText() + ")";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Replace loop with 'Arrays.fill()' method call" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
class Test {
|
||||
|
||||
void fillByteArray() {
|
||||
byte[] plaintext = {1,2,3,4,5};
|
||||
Arrays.fill(plaintext, (byte) 0);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
// "Replace loop with 'Arrays.fill()' method call" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.*;
|
||||
|
||||
class Test {
|
||||
|
||||
private void testLambdas() {
|
||||
Supplier[] arr = new Supplier[10];
|
||||
Arrays.fill(arr, () -> new int[10]);
|
||||
Arrays.fill(arr, (Supplier) () -> new int[10]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Replace loop with 'Arrays.fill()' method call" "true"
|
||||
|
||||
class Test {
|
||||
|
||||
void fillByteArray() {
|
||||
byte[] plaintext = {1,2,3,4,5};
|
||||
for (<caret>int counter = 0; counter < plaintext.length; counter++) {
|
||||
plaintext[counter] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
// "Replace loop with 'Arrays.fill()' method call" "true"
|
||||
import java.util.function.*;
|
||||
|
||||
class Test {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user