can be diamond: ensure inner class is referenced as simple name, otherwise qualified text with generics outer class would lead to kinda raw type (IDEA-159179)

This commit is contained in:
Anna Kozlova
2016-08-12 09:21:55 +03:00
parent bca244394b
commit a5af84762d
3 changed files with 31 additions and 2 deletions
@@ -17,10 +17,12 @@ package com.intellij.psi.impl;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiTypesUtil;
import com.intellij.psi.util.PsiUtil;
@@ -163,8 +165,11 @@ public class PsiDiamondTypeUtil {
final PsiType typeByParent = PsiTypesUtil.getExpectedTypeByParent(expression);
if (typeByParent != null) {
final String arrayInitializer = "new " + typeByParent.getCanonicalText() + "[]{0}";
final PsiNewExpression newExpr =
(PsiNewExpression)JavaPsiFacade.getInstance(expression.getProject()).getElementFactory().createExpressionFromText(arrayInitializer, expression);
final Project project = expression.getProject();
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
PsiNewExpression newExpr = (PsiNewExpression)elementFactory.createExpressionFromText(arrayInitializer, expression);
//ensure refs to inner classes are collapsed to avoid raw types (container type would be raw in qualified text)
newExpr = (PsiNewExpression)JavaCodeStyleManager.getInstance(project).shortenClassReferences(newExpr);
final PsiArrayInitializerExpression initializer = newExpr.getArrayInitializer();
LOG.assertTrue(initializer != null);
copy = initializer.getInitializers()[0].replace(expression);
@@ -0,0 +1,12 @@
// "Replace with <>" "true"
import java.util.ArrayList;
import java.util.List;
class Util<T> {
private class WithPosition {}
public void addNewlyAddedChildrenOf() {
List<WithPosition> addings = new ArrayList<>();
}
}
@@ -0,0 +1,12 @@
// "Replace with <>" "true"
import java.util.ArrayList;
import java.util.List;
class Util<T> {
private class WithPosition {}
public void addNewlyAddedChildrenOf() {
List<WithPosition> addings = new ArrayList<With<caret>Position>();
}
}