(no message)

This commit is contained in:
Eugene Vigdorchik
2005-02-20 13:26:37 +03:00
parent 844fe248c1
commit b12c2b59ed
3 changed files with 68 additions and 77 deletions
+65 -2
View File
@@ -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<PsiType>() {
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<PsiTypeParameter> 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;
}
}
@@ -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) {
@@ -579,67 +579,7 @@ public class RefactoringUtil {
}
}
type = type.accept(new PsiTypeVisitor<PsiType>() {
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<PsiTypeParameter> 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) {