From b12c2b59edac44488fc50c0213353460fc41f730 Mon Sep 17 00:00:00 2001 From: Eugene Vigdorchik Date: Sun, 20 Feb 2005 13:26:37 +0300 Subject: [PATCH] (no message) --- .../src/com/intellij/psi/GenericsUtil.java | 67 ++++++++++++++++++- .../daemon/impl/quickfix/VariableTypeFix.java | 16 +---- .../refactoring/util/RefactoringUtil.java | 62 +---------------- 3 files changed, 68 insertions(+), 77 deletions(-) diff --git a/openapi/src/com/intellij/psi/GenericsUtil.java b/openapi/src/com/intellij/psi/GenericsUtil.java index 0776a7e1164c..55be1abdc338 100644 --- a/openapi/src/com/intellij/psi/GenericsUtil.java +++ b/openapi/src/com/intellij/psi/GenericsUtil.java @@ -6,11 +6,10 @@ package com.intellij.psi; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.util.Pair; +import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.util.InheritanceUtil; import com.intellij.psi.util.PsiUtil; import com.intellij.psi.util.TypeConversionUtil; -import com.intellij.psi.search.GlobalSearchScope; -import com.intellij.psi.search.PsiSearchHelper; import java.util.Iterator; import java.util.LinkedHashSet; @@ -172,4 +171,68 @@ public class GenericsUtil { return type.getInternalCanonicalText().equals(type.getCanonicalText()); } + public static PsiType getVariableTypeByExpressionType(PsiType type) { + type = type.accept(new PsiTypeVisitor() { + public PsiType visitArrayType(PsiArrayType arrayType) { + PsiType componentType = arrayType.getComponentType(); + PsiType type = componentType.accept(this); + if (type == componentType) return arrayType; + return type.createArrayType(); + } + + public PsiType visitType(PsiType type) { + return type; + } + + public PsiType visitWildcardType(final PsiWildcardType wildcardType) { + final PsiType bound = wildcardType.getBound(); + PsiManager manager = wildcardType.getManager(); + if (bound != null) { + final PsiType acceptedBound = bound.accept(this); + if (acceptedBound instanceof PsiWildcardType) { + if (((PsiWildcardType)acceptedBound).isExtends() != wildcardType.isExtends()) return PsiWildcardType.createUnbounded(manager); + return acceptedBound; + } + if (acceptedBound.equals(bound)) return wildcardType; + return wildcardType.isExtends() ? PsiWildcardType.createExtends(manager, acceptedBound) : + PsiWildcardType.createSuper(manager, acceptedBound); + } + return wildcardType; + } + + public PsiType visitCapturedWildcardType(PsiCapturedWildcardType capturedWildcardType) { + return capturedWildcardType.getWildcard().accept(this); + } + + public PsiType visitClassType(PsiClassType classType) { + PsiClassType.ClassResolveResult resolveResult = classType.resolveGenerics(); + PsiClass aClass = resolveResult.getElement(); + if (aClass == null) return classType; + boolean toExtend = false; + Iterator iterator = PsiUtil.typeParametersIterator(aClass); + PsiSubstitutor substitutor = PsiSubstitutor.EMPTY; + while (iterator.hasNext()) { + PsiTypeParameter typeParameter = iterator.next(); + PsiType typeArgument = resolveResult.getSubstitutor().substitute(typeParameter); + if (typeArgument instanceof PsiCapturedWildcardType) toExtend = true; + substitutor = substitutor.put(typeParameter, typeArgument == null ? null : typeArgument.accept(this)); + } + + PsiManager manager = aClass.getManager(); + PsiType result = manager.getElementFactory().createType(aClass, substitutor); + if (toExtend) result = PsiWildcardType.createExtends(manager, result); + return result; + } + }); + + PsiType componentType = type.getDeepComponentType(); + if (componentType instanceof PsiWildcardType) { + componentType = ((PsiWildcardType)componentType).getExtendsBound(); + int dims = type.getArrayDimensions(); + for (int i = 0; i < dims; i++) componentType = componentType.createArrayType(); + return componentType; + } + + return type; + } } diff --git a/source/com/intellij/codeInsight/daemon/impl/quickfix/VariableTypeFix.java b/source/com/intellij/codeInsight/daemon/impl/quickfix/VariableTypeFix.java index 3b8b05f6bf0e..3e85e9d6e0e8 100644 --- a/source/com/intellij/codeInsight/daemon/impl/quickfix/VariableTypeFix.java +++ b/source/com/intellij/codeInsight/daemon/impl/quickfix/VariableTypeFix.java @@ -23,18 +23,7 @@ public class VariableTypeFix implements IntentionAction { public VariableTypeFix(PsiVariable variable, PsiType toReturn) { myVariable = variable; - myReturnType = toReturn; - } - public VariableTypeFix(PsiVariable variable, String toReturn) { - myVariable = variable; - PsiType type; - try { - type = variable.getManager().getElementFactory().createTypeFromText(toReturn, variable); - } - catch (Exception e) { - type = null; - } - myReturnType = type; + myReturnType = GenericsUtil.getVariableTypeByExpressionType(toReturn); } public String getText() { @@ -57,8 +46,7 @@ public class VariableTypeFix implements IntentionAction { && myReturnType != null && myReturnType.isValid() && !TypeConversionUtil.isNullType(myReturnType) - && !TypeConversionUtil.isVoidType(myReturnType) - && GenericsUtil.isFromExternalTypeLanguage(myReturnType); + && !TypeConversionUtil.isVoidType(myReturnType); } public void invoke(Project project, Editor editor, PsiFile file) { diff --git a/source/com/intellij/refactoring/util/RefactoringUtil.java b/source/com/intellij/refactoring/util/RefactoringUtil.java index 1b832a305ef6..1adabe8f7608 100644 --- a/source/com/intellij/refactoring/util/RefactoringUtil.java +++ b/source/com/intellij/refactoring/util/RefactoringUtil.java @@ -579,67 +579,7 @@ public class RefactoringUtil { } } - type = type.accept(new PsiTypeVisitor() { - public PsiType visitArrayType(PsiArrayType arrayType) { - PsiType componentType = arrayType.getComponentType(); - PsiType type = componentType.accept(this); - if (type == componentType) return arrayType; - return type.createArrayType(); - } - - public PsiType visitType(PsiType type) { - return type; - } - - public PsiType visitWildcardType(final PsiWildcardType wildcardType) { - final PsiType bound = wildcardType.getBound(); - PsiManager manager = wildcardType.getManager(); - if (bound != null) { - final PsiType acceptedBound = bound.accept(this); - if (acceptedBound instanceof PsiWildcardType) { - if (((PsiWildcardType)acceptedBound).isExtends() != wildcardType.isExtends()) return PsiWildcardType.createUnbounded(manager); - return acceptedBound; - } - if (acceptedBound.equals(bound)) return wildcardType; - return wildcardType.isExtends() ? PsiWildcardType.createExtends(manager, acceptedBound) : - PsiWildcardType.createSuper(manager, acceptedBound); - } - return wildcardType; - } - - public PsiType visitCapturedWildcardType(PsiCapturedWildcardType capturedWildcardType) { - return capturedWildcardType.getWildcard().accept(this); - } - - public PsiType visitClassType(PsiClassType classType) { - PsiClassType.ClassResolveResult resolveResult = classType.resolveGenerics(); - PsiClass aClass = resolveResult.getElement(); - if (aClass == null) return classType; - boolean toExtend = false; - Iterator iterator = PsiUtil.typeParametersIterator(aClass); - PsiSubstitutor substitutor = PsiSubstitutor.EMPTY; - while(iterator.hasNext()) { - PsiTypeParameter typeParameter = iterator.next(); - PsiType typeArgument = resolveResult.getSubstitutor().substitute(typeParameter); - if (typeArgument instanceof PsiCapturedWildcardType) toExtend = true; - substitutor = substitutor.put(typeParameter, typeArgument == null ? null : typeArgument.accept(this)); - } - - PsiType result = factory.createType(aClass, substitutor); - if (toExtend) result = PsiWildcardType.createExtends(aClass.getManager(), result); - return result; - } - }); - - PsiType componentType = type.getDeepComponentType(); - if (componentType instanceof PsiWildcardType) { - componentType = ((PsiWildcardType)componentType).getExtendsBound(); - int dims = type.getArrayDimensions(); - for (int i = 0; i < dims; i++) componentType = componentType.createArrayType(); - return componentType; - } - - return type; + return GenericsUtil.getVariableTypeByExpressionType(type); } public static boolean isAssignmentLHS(PsiElement element) {