reuse utility method; ensure unknown functional type reduced to Object before create new psi

EA-75452 - IOE: PsiJavaParserFacadeImpl.createTypeElementFromText
This commit is contained in:
Anna.Kozlova
2016-04-22 16:15:16 +02:00
parent 1a2b94105e
commit b1645e3ae5
7 changed files with 28 additions and 11 deletions

View File

@@ -15,7 +15,10 @@
*/
package com.intellij.refactoring.util;
import com.intellij.psi.*;
import com.intellij.psi.LambdaUtil;
import com.intellij.psi.PsiType;
import com.intellij.psi.PsiVariable;
import com.intellij.psi.SmartTypePointerManager;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -32,7 +35,7 @@ public class VariableData extends AbstractVariableData {
public VariableData(@Nullable PsiVariable var, PsiType type) {
variable = var;
if (var != null) {
if (type instanceof PsiLambdaParameterType || type instanceof PsiLambdaExpressionType || type instanceof PsiMethodReferenceType) {
if (LambdaUtil.notInferredType(type)) {
type = PsiType.getJavaLangObject(var.getManager(), GlobalSearchScope.allScope(var.getProject()));
}
this.type = SmartTypePointerManager.getInstance(var.getProject()).createSmartTypePointer(type).getType();

View File

@@ -261,10 +261,7 @@ public class CreateFromUsageUtils {
names = new String[]{"p" + i};
}
if (argType == null || PsiType.NULL.equals(argType) ||
argType instanceof PsiLambdaExpressionType ||
argType instanceof PsiLambdaParameterType ||
argType instanceof PsiMethodReferenceType) {
if (argType == null || PsiType.NULL.equals(argType) || LambdaUtil.notInferredType(argType)) {
argType = PsiType.getJavaLangObject(psiManager, resolveScope);
} else if (argType instanceof PsiDisjunctionType) {
argType = ((PsiDisjunctionType)argType).getLeastUpperBound();

View File

@@ -78,6 +78,9 @@ public class CreateLocalFromUsageFix extends CreateVarFromUsageFix {
final SmartTypePointer defaultType = SmartTypePointerManager.getInstance(project).createSmartTypePointer(expectedTypes[0]);
final PsiType preferredType = TypeSelectorManagerImpl.getPreferredType(expectedTypes, expectedTypes[0]);
PsiType type = preferredType != null ? preferredType : expectedTypes[0];
if (LambdaUtil.notInferredType(type)) {
type = PsiType.getJavaLangObject(myReferenceExpression.getManager(), targetClass.getResolveScope());
}
String varName = myReferenceExpression.getReferenceName();
PsiExpression initializer = null;

View File

@@ -385,7 +385,7 @@ public class RefactoringUtil {
public static PsiType getTypeByExpressionWithExpectedType(PsiExpression expr) {
PsiElementFactory factory = JavaPsiFacade.getInstance(expr.getProject()).getElementFactory();
PsiType type = getTypeByExpression(expr, factory);
final boolean isFunctionalType = type instanceof PsiLambdaExpressionType || type instanceof PsiMethodReferenceType || type instanceof PsiLambdaParameterType;
final boolean isFunctionalType = LambdaUtil.notInferredType(type);
final boolean isDenotable = PsiTypesUtil.isDenotableType(expr.getType());
if (type != null && !isFunctionalType && isDenotable) {
return type;
@@ -404,9 +404,7 @@ public class RefactoringUtil {
public static PsiType getTypeByExpression(PsiExpression expr) {
PsiElementFactory factory = JavaPsiFacade.getInstance(expr.getProject()).getElementFactory();
PsiType type = getTypeByExpression(expr, factory);
if (type instanceof PsiLambdaParameterType ||
type instanceof PsiLambdaExpressionType ||
type instanceof PsiMethodReferenceType) {
if (LambdaUtil.notInferredType(type)) {
type = factory.createTypeByFQClassName(CommonClassNames.JAVA_LANG_OBJECT, expr.getResolveScope());
}
return type;

View File

@@ -42,7 +42,7 @@ class TypeCorrector extends PsiTypeMapper {
@Override
public PsiType visitType(PsiType type) {
if (type instanceof PsiLambdaParameterType || type instanceof PsiLambdaExpressionType || type instanceof PsiMethodReferenceType) {
if (LambdaUtil.notInferredType(type)) {
return type;
}
return super.visitType(type);

View File

@@ -0,0 +1,8 @@
// "Create local variable 'a'" "true"
class C {
void foo() {
(s) -> {
Object a = s;
};
}
}

View File

@@ -0,0 +1,8 @@
// "Create local variable 'a'" "true"
class C {
void foo() {
(s) -> {
<caret>a = s;
};
}
}