mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
variable array fix: don't create fix if it can't be applied when there is no variable in the context
EA-54266 - ISE: VariableArrayTypeFix.getText
This commit is contained in:
+1
-1
@@ -1807,7 +1807,7 @@ public class HighlightUtil extends HighlightUtilBase {
|
||||
|
||||
if (!arrayTypeFixChecked) {
|
||||
final PsiType checkResult = JavaHighlightUtil.sameType(initializers);
|
||||
fix = checkResult != null ? new VariableArrayTypeFix(arrayInitializer, checkResult) : null;
|
||||
fix = checkResult != null ? VariableArrayTypeFix.createFix(arrayInitializer, checkResult) : null;
|
||||
arrayTypeFixChecked = true;
|
||||
}
|
||||
if (fix != null) {
|
||||
|
||||
+18
-12
@@ -36,24 +36,30 @@ public class VariableArrayTypeFix extends LocalQuickFixOnPsiElement {
|
||||
private final String myName;
|
||||
private final String myFamilyName;
|
||||
|
||||
public VariableArrayTypeFix(@NotNull PsiArrayInitializerExpression initializer, @NotNull PsiType componentType) {
|
||||
super(getInitializer(initializer));
|
||||
private VariableArrayTypeFix(@NotNull PsiArrayInitializerExpression initializer,
|
||||
@NotNull PsiArrayType arrayType,
|
||||
@NotNull PsiVariable variable) {
|
||||
super(initializer);
|
||||
myTargetType = arrayType;
|
||||
PsiExpression myNewExpression = getNewExpressionLocal(initializer);
|
||||
myName = myTargetType.equals(variable.getType()) && myNewExpression != null
|
||||
? QuickFixBundle.message("change.new.operator.type.text", getNewText(myNewExpression,initializer), myTargetType.getCanonicalText(), "")
|
||||
: QuickFixBundle.message("fix.variable.type.text", formatType(variable), variable.getName(), myTargetType.getCanonicalText());
|
||||
myFamilyName = QuickFixBundle.message(myTargetType.equals(variable.getType()) && myNewExpression != null ? "change.new.operator.type.family"
|
||||
: "fix.variable.type.family");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static VariableArrayTypeFix createFix(PsiArrayInitializerExpression initializer, @NotNull PsiType componentType) {
|
||||
PsiArrayType arrayType = new PsiArrayType(componentType);
|
||||
PsiArrayInitializerExpression arrayInitializer = initializer;
|
||||
while (arrayInitializer.getParent() instanceof PsiArrayInitializerExpression) {
|
||||
arrayInitializer = (PsiArrayInitializerExpression)arrayInitializer.getParent();
|
||||
arrayType = new PsiArrayType(arrayType);
|
||||
}
|
||||
myTargetType = arrayType;
|
||||
|
||||
PsiExpression myNewExpression = getNewExpressionLocal(arrayInitializer);
|
||||
PsiVariable myVariable = getVariableLocal(arrayInitializer);
|
||||
myName = myVariable == null ? null : myTargetType.equals(myVariable.getType()) && myNewExpression != null ?
|
||||
QuickFixBundle.message("change.new.operator.type.text", getNewText(myNewExpression,arrayInitializer), myTargetType.getCanonicalText(), "") :
|
||||
QuickFixBundle.message("fix.variable.type.text", formatType(myVariable), myVariable.getName(), myTargetType.getCanonicalText());
|
||||
myFamilyName = myVariable == null ? null : myTargetType.equals(myVariable.getType()) && myNewExpression != null ?
|
||||
QuickFixBundle.message("change.new.operator.type.family") :
|
||||
QuickFixBundle.message("fix.variable.type.family");
|
||||
PsiVariable variable = getVariableLocal(arrayInitializer);
|
||||
if (variable == null) return null;
|
||||
return new VariableArrayTypeFix(arrayInitializer, arrayType, variable);
|
||||
}
|
||||
|
||||
private static String formatType(@NotNull PsiVariable variable) {
|
||||
|
||||
+1
-1
@@ -349,7 +349,7 @@ public class UncheckedWarningLocalInspectionBase extends BaseJavaBatchLocalInspe
|
||||
JavaHighlightUtil.formatType(componentType));
|
||||
if (!arrayTypeFixChecked) {
|
||||
final PsiType checkResult = JavaHighlightUtil.sameType(initializers);
|
||||
fix = checkResult != null ? new VariableArrayTypeFix(arrayInitializer, checkResult) : null;
|
||||
fix = checkResult != null ? VariableArrayTypeFix.createFix(arrayInitializer, checkResult) : null;
|
||||
arrayTypeFixChecked = true;
|
||||
}
|
||||
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import java.util.*;
|
||||
|
||||
class FooClass {
|
||||
|
||||
{
|
||||
Set[][] a = null;
|
||||
a[0] = new Set[]{ <error descr="Incompatible types. Found: 'java.util.List', required: 'java.util.Set'">fooBar()</error>, <error descr="Incompatible types. Found: 'java.util.List', required: 'java.util.Set'">fooBar()</error>};
|
||||
}
|
||||
|
||||
private List fooBar() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+2
@@ -182,6 +182,8 @@ public class LightAdvHighlightingJdk7Test extends LightDaemonAnalyzerTestCase {
|
||||
public void testTryWithResourcesWithMultipleCloseInterfaces() { doTest(false, false);}
|
||||
public void testIDEA138978() { doTest(false, false); }
|
||||
|
||||
public void testArrayInitializerTypeCheckVariableType() { doTest(false, false);}
|
||||
|
||||
public void testJavaUtilCollections_NoVerify() throws Exception {
|
||||
PsiClass collectionsClass = getJavaFacade().findClass("java.util.Collections", GlobalSearchScope.moduleWithLibrariesScope(getModule()));
|
||||
assertNotNull(collectionsClass);
|
||||
|
||||
Reference in New Issue
Block a user