mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
change new type & collapse generics for java 7 (IDEA-73517)
This commit is contained in:
@@ -24,6 +24,8 @@ import com.intellij.openapi.editor.ScrollType;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.PsiDiamondTypeUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.psi.util.PsiExpressionTrimRenderer;
|
||||
@@ -84,6 +86,12 @@ public class ChangeNewOperatorTypeFix implements IntentionAction {
|
||||
else {
|
||||
final PsiAnonymousClass anonymousClass = originalExpression.getAnonymousClass();
|
||||
newExpression = (PsiNewExpression)factory.createExpressionFromText("new " + toType.getCanonicalText() + "()" + (anonymousClass != null ? "{}" : ""), originalExpression);
|
||||
if (anonymousClass == null) { //just to prevent useless inference
|
||||
if (PsiDiamondTypeUtil.canCollapseToDiamond(newExpression, originalExpression, false)) {
|
||||
final PsiElement paramList = PsiDiamondTypeUtil.replaceExplicitWithDiamond(newExpression.getClassOrAnonymousClassReference().getParameterList());
|
||||
newExpression = PsiTreeUtil.getParentOfType(paramList, PsiNewExpression.class);
|
||||
}
|
||||
}
|
||||
PsiExpressionList argumentList = originalExpression.getArgumentList();
|
||||
if (argumentList == null) return;
|
||||
newExpression.getArgumentList().replace(argumentList);
|
||||
|
||||
@@ -35,6 +35,12 @@ public class PsiDiamondTypeUtil {
|
||||
}
|
||||
|
||||
public static boolean canCollapseToDiamond(PsiNewExpression expression, PsiExpression context) {
|
||||
return canCollapseToDiamond(expression, context, true);
|
||||
}
|
||||
|
||||
public static boolean canCollapseToDiamond(PsiNewExpression expression,
|
||||
PsiExpression context,
|
||||
boolean checkAssignable) {
|
||||
if (PsiUtil.getLanguageLevel(context).isAtLeast(LanguageLevel.JDK_1_7)) {
|
||||
final PsiJavaCodeReferenceElement classReference = expression.getClassOrAnonymousClassReference();
|
||||
if (classReference != null) {
|
||||
@@ -45,6 +51,7 @@ public class PsiDiamondTypeUtil {
|
||||
if (typeElements.length == 1 && typeElements[0].getType() instanceof PsiDiamondType) return false;
|
||||
final PsiDiamondType.DiamondInferenceResult inferenceResult = PsiDiamondType.resolveInferredTypes(expression);
|
||||
if (inferenceResult.getErrorMessage() == null) {
|
||||
if (!checkAssignable) return true;
|
||||
final List<PsiType> types = inferenceResult.getInferredTypes();
|
||||
final PsiType[] typeArguments = parameterList.getTypeArguments();
|
||||
if (types.size() == typeArguments.length) {
|
||||
|
||||
@@ -3,6 +3,6 @@ import java.util.*;
|
||||
|
||||
class RRR {
|
||||
void f() {
|
||||
List<String> l = new ArrayList<String>(<caret>);
|
||||
List<String> l = new ArrayList<>(<caret>);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,6 @@ import java.util.*;
|
||||
|
||||
class RRR {
|
||||
void f() {
|
||||
List<String> l = new ArrayList<String>(2<caret>);
|
||||
List<String> l = new ArrayList<>(2<caret>);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user