mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 13:20:53 +07:00
IDEA-180862 'Wrap using Collections.singletonList' intention doesn't work with subtypes of return type
Also anonymous classes supported
This commit is contained in:
@@ -26,7 +26,9 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.infos.CandidateInfo;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import one.util.streamex.StreamEx;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -62,12 +64,20 @@ public class WrapWithAdapterMethodCallFix extends LocalQuickFixAndIntentionActio
|
||||
return false;
|
||||
}
|
||||
PsiType variableType = GenericsUtil.getVariableTypeByExpressionType(inType);
|
||||
String typeText = variableType.getCanonicalText();
|
||||
if(!variableType.equalsToText(typeText)) {
|
||||
// Probably some incorrect type like PsiLambdaParameterType
|
||||
if(variableType instanceof PsiLambdaExpressionType || variableType instanceof PsiMethodReferenceType
|
||||
|| variableType instanceof PsiLambdaParameterType) {
|
||||
return false;
|
||||
}
|
||||
PsiType resultType = createReplacement(context, "((" + typeText + ")null)").getType();
|
||||
|
||||
String typeText = variableType.getCanonicalText();
|
||||
PsiExpression replacement = createReplacement(context, "((" + typeText + ")null)");
|
||||
PsiDeclarationStatement declaration =
|
||||
JavaPsiFacade.getElementFactory(context.getProject()).createVariableDeclarationStatement("x", outType, replacement, context);
|
||||
PsiVariable var = ObjectUtils.tryCast(ArrayUtil.getFirstElement(declaration.getDeclaredElements()), PsiVariable.class);
|
||||
if (var == null) return false;
|
||||
PsiExpression initializer = var.getInitializer();
|
||||
if (initializer == null) return false;
|
||||
PsiType resultType = initializer.getType();
|
||||
return resultType != null && outType.isAssignableFrom(resultType);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Wrap using 'Collections.singletonList()'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Test {
|
||||
|
||||
void m() {
|
||||
List<Object> list = Collections.singletonList(new Object() {
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Wrap using 'Collections.singletonList()'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Test {
|
||||
|
||||
void m(String s) {
|
||||
List<CharSequence> list = Collections.singletonList(s);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Wrap using 'Collections.singletonList()'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Test {
|
||||
|
||||
void m() {
|
||||
List<Object> list = new Obj<caret>ect() {};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Wrap using 'Collections.singletonList()'" "false"
|
||||
import java.util.*;
|
||||
|
||||
class Test {
|
||||
|
||||
void m(CharSequence s) {
|
||||
List<String> list = <caret>s;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Wrap using 'Collections.singletonList()'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Test {
|
||||
|
||||
void m(String s) {
|
||||
List<CharSequence> list = <caret>s;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user