From 5f23dacb8a95f82dc005ff9aa5af0484bc5be344 Mon Sep 17 00:00:00 2001 From: Eugene Vigdorchik Date: Wed, 9 Mar 2005 17:24:21 +0300 Subject: [PATCH] (no message) --- .../intellij/refactoring/typeCook/Util.java | 122 +++++++++--------- .../deductive/builder/SystemBuilder.java | 92 ++++++------- 2 files changed, 105 insertions(+), 109 deletions(-) diff --git a/source/com/intellij/refactoring/typeCook/Util.java b/source/com/intellij/refactoring/typeCook/Util.java index 348f0e1de09d..a82aef9c1991 100644 --- a/source/com/intellij/refactoring/typeCook/Util.java +++ b/source/com/intellij/refactoring/typeCook/Util.java @@ -2,6 +2,7 @@ package com.intellij.refactoring.typeCook; import com.intellij.openapi.diagnostic.Logger; import com.intellij.psi.*; +import com.intellij.psi.util.PsiUtil; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.refactoring.typeCook.deductive.PsiTypeIntersection; import com.intellij.refactoring.typeCook.deductive.PsiTypeVariable; @@ -22,14 +23,6 @@ import java.util.Set; public class Util { private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.typeCook.Util"); - public static int getArrayLevel(PsiType t) { - if (t instanceof PsiArrayType) { - return 1 + getArrayLevel(((PsiArrayType)t).getComponentType()); - } - - return 0; - } - public static PsiType createArrayType(PsiType theType, int level) { while (level-- > 0) { theType = theType.createArrayType(); @@ -39,21 +32,13 @@ public class Util { } public static PsiClassType.ClassResolveResult resolveType(PsiType type) { - type = avoidAnonymous(type); - - if (type == null) { - return PsiClassType.ClassResolveResult.EMPTY; - } - - if (type instanceof PsiClassType) { - return ((PsiClassType)type).resolveGenerics(); - } - else if (type instanceof PsiArrayType) { - return resolveType(((PsiArrayType)type).getComponentType()); - } - else { - return PsiClassType.ClassResolveResult.EMPTY; + final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(type); + final PsiClass aClass = resolveResult.getElement(); + if (aClass instanceof PsiAnonymousClass) { + final PsiClassType baseClassType = ((PsiAnonymousClass)aClass).getBaseClassType(); + return resolveType(resolveResult.getSubstitutor().substitute(baseClassType)); } + return resolveResult; } public static PsiType normalize(PsiType t, boolean objectBottom) { @@ -276,45 +261,6 @@ public class Util { } } - public static PsiType avoidAnonymous(PsiType type) { - if (type instanceof PsiClassType) { - final PsiClassType.ClassResolveResult result = ((PsiClassType)type).resolveGenerics(); - final PsiClass aClass = result.getElement(); - - if (result.getElement() == null) { - return null; - } - - if (aClass instanceof PsiAnonymousClass) { - return aClass.getSuperTypes()[0]; - } - - final PsiSubstitutor aSubst = result.getSubstitutor(); - - PsiSubstitutor theSubst = PsiSubstitutor.EMPTY; - - for (Iterator i = aSubst.getSubstitutionMap().keySet().iterator(); i.hasNext();) { - final PsiTypeParameter p = i.next(); - PsiType t = aSubst.substitute(p); - - if (t != null) { - t = avoidAnonymous(t); - } - - theSubst = theSubst.put(p, t); - } - - return aClass.getManager().getElementFactory().createType(aClass, theSubst); - } - else if (type instanceof PsiArrayType) { - PsiType avoided = avoidAnonymous(((PsiArrayType)type).getComponentType()); - - return avoided == null ? null : avoided.createArrayType(); - } - - return type; - } - public static PsiType createParameterizedType(final PsiType t, final PsiTypeVariableFactory factory, final PsiElement context) { return createParameterizedType(t, factory, true, context); } @@ -364,6 +310,57 @@ public class Util { return t; } + public static PsiType substituteType(final PsiType type, final PsiSubstitutor subst) { + if (type == null) return null; + if (type instanceof PsiWildcardType) { + final PsiWildcardType wcType = ((PsiWildcardType)type); + final PsiType bound = wcType.getBound(); + + if (bound != null) { + final PsiClass aClass = resolveType(bound).getElement(); + + if (aClass != null) { + final PsiManager manager = aClass.getManager(); + + return wcType.isExtends() + ? PsiWildcardType.createExtends(manager, substituteType(bound, subst)) + : PsiWildcardType.createSuper(manager, substituteType(bound, subst)); + } + } + + return type; + } + else { + final int level = type.getArrayDimensions(); + final PsiClassType.ClassResolveResult result = resolveType(type); + final PsiClass aClass = result.getElement(); + + if (aClass != null) { + final PsiSubstitutor aSubst = result.getSubstitutor(); + final PsiManager manager = aClass.getManager(); + + if (aClass instanceof PsiTypeParameter) { + final PsiType sType = subst.substitute(((PsiTypeParameter)aClass)); + + return createArrayType(sType == null ? PsiType.getJavaLangObject(manager, aClass.getResolveScope()) : sType, level); + } + + final PsiTypeParameter[] aParms = getTypeParametersList(aClass); + PsiSubstitutor theSubst = PsiSubstitutor.EMPTY; + + for (int i = 0; i < aParms.length; i++) { + PsiTypeParameter aParm = aParms[i]; + + theSubst = theSubst.put(aParm, substituteType(aSubst.substitute(aParm), subst)); + } + + return createArrayType(aClass.getManager().getElementFactory().createType(aClass, theSubst), level); + } + + return type; + } + } + public static boolean bindsTypeVariables(final PsiType t) { if (t == null) { return false; @@ -472,5 +469,4 @@ public class Util { LOG.error("Incorrect Operation Exception thrown in CastRole.\n"); } } -} - +} \ No newline at end of file diff --git a/source/com/intellij/refactoring/typeCook/deductive/builder/SystemBuilder.java b/source/com/intellij/refactoring/typeCook/deductive/builder/SystemBuilder.java index ab85f79f24b6..74867b395c53 100644 --- a/source/com/intellij/refactoring/typeCook/deductive/builder/SystemBuilder.java +++ b/source/com/intellij/refactoring/typeCook/deductive/builder/SystemBuilder.java @@ -269,10 +269,10 @@ public class SystemBuilder { if (!qualifierClass.equals(aClass)) { supertypeSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(aClass, qualifierClass, PsiSubstitutor.EMPTY); - aType = supertypeSubstitutor.substitute(aType); + aType = Util.substituteType(aType, supertypeSubstitutor); } - aType = qualifierSubstitutor.substitute(aType); + aType = Util.substituteType(aType, qualifierSubstitutor); } } @@ -298,7 +298,7 @@ public class SystemBuilder { system.addSubtypeConstraint(argumenType, parmType); } else { - parmType = qualifierSubstitutor.substitute(supertypeSubstitutor.substitute(parameters[i].getType())); + parmType = Util.substituteType(Util.substituteType(parameters[i].getType(), supertypeSubstitutor), qualifierSubstitutor); if (!Util.bindsTypeVariables(parmType) && !Util.bindsTypeParameters(parmType, typeParameters)) { parmType = Util.banalize(parmType); @@ -309,7 +309,7 @@ public class SystemBuilder { PsiType introduceAdditionalTypeVariables(final PsiType type, final PsiSubstitutor qualifier, final PsiSubstitutor supertype) { - final int level = Util.getArrayLevel(type); + final int level = type.getArrayDimensions(); final PsiClassType.ClassResolveResult result = Util.resolveType(type); final PsiClass aClass = result.getElement(); @@ -327,47 +327,47 @@ public class SystemBuilder { for (int j = 0; j < extypes.length; j++) { final PsiClassType ext = extypes[j]; - final PsiType extype = qualifier.substitute(new Object() { - public PsiType substitute(final PsiType ext) { - final PsiClassType.ClassResolveResult result = - Util.resolveType(ext); - final PsiClass aClass = result.getElement(); + final PsiType extype = Util.substituteType(new Object() { + public PsiType substitute(final PsiType ext) { + final PsiClassType.ClassResolveResult result = + Util.resolveType(ext); + final PsiClass aClass = result.getElement(); - if (aClass != null) { - if (aClass instanceof PsiTypeParameter) { - final PsiType type = mapping.get(aClass); + if (aClass != null) { + if (aClass instanceof PsiTypeParameter) { + final PsiType type = mapping.get(aClass); - if (type != null) { - return type; - } - - return ext; + if (type != null) { + return type; } - final PsiSubstitutor aSubst = result.getSubstitutor(); - PsiSubstitutor theSubst = PsiSubstitutor.EMPTY; - - for (final Iterator p = - aSubst.getSubstitutionMap().keySet().iterator(); - p.hasNext();) { - final PsiTypeParameter parm = p.next(); - - PsiType type = aSubst.substitute(parm); - - if (type != null) { - type = substitute(type); - } - - theSubst = theSubst.put(parm, type); - } - - return aClass.getManager().getElementFactory() - .createType(aClass, theSubst); + return ext; } - return ext; + final PsiSubstitutor aSubst = result.getSubstitutor(); + PsiSubstitutor theSubst = PsiSubstitutor.EMPTY; + + for (final Iterator p = + aSubst.getSubstitutionMap().keySet().iterator(); + p.hasNext();) { + final PsiTypeParameter parm = p.next(); + + PsiType type = aSubst.substitute(parm); + + if (type != null) { + type = substitute(type); + } + + theSubst = theSubst.put(parm, type); + } + + return aClass.getManager().getElementFactory() + .createType(aClass, theSubst); } - }.substitute(ext)); + + return ext; + } + }.substitute(ext), qualifier); system.addSubtypeConstraint(pv, extype); } @@ -387,7 +387,7 @@ public class SystemBuilder { final PsiType theBound = wildcard.getBound(); if (theBound != null) { - final PsiType bound = qualifier.substitute(supertype.substitute(theBound)); + final PsiType bound = Util.substituteType(Util.substituteType(theBound, supertype), qualifier); if (Util.bindsTypeVariables(bound)) { final PsiType var = myTypeVariableFactory.create(); @@ -418,7 +418,7 @@ public class SystemBuilder { subst = subst.put(aTypeParm, parmVar); } - final PsiType bnd = subst.substitute(bound); + final PsiType bnd = Util.substituteType(bound, subst); if (wildcard.isExtends()) { system.addSubtypeConstraint(bnd, var); @@ -498,7 +498,7 @@ public class SystemBuilder { theSubst = theSubst.put(parm, type); } - return theSubst.substitute(aType); + return Util.substituteType(aType, theSubst); } } else if (expr instanceof PsiParenthesizedExpression) { @@ -551,10 +551,10 @@ public class SystemBuilder { if (!aClass.equals(superClass) && field.isPhysical()) { aType = - TypeConversionUtil.getSuperClassSubstitutor(superClass, aClass, PsiSubstitutor.EMPTY).substitute(aType); + Util.substituteType(aType, TypeConversionUtil.getSuperClassSubstitutor(superClass, aClass, PsiSubstitutor.EMPTY)); } - return aSubst.substitute(aType); + return Util.substituteType(aType, aSubst); } } else if (element != null) { @@ -789,10 +789,10 @@ public class SystemBuilder { if (bound != null) { if (wildcard.isExtends()) { - system.addSubtypeConstraint(Util.banalize(definedSubst.substitute(replaceWildCards(bound))), var); + system.addSubtypeConstraint(Util.banalize(Util.substituteType(replaceWildCards(bound), definedSubst)), var); } else { - system.addSubtypeConstraint(var, Util.banalize(definedSubst.substitute(replaceWildCards(bound)))); + system.addSubtypeConstraint(var, Util.banalize(Util.substituteType(replaceWildCards(bound), definedSubst))); } } @@ -820,7 +820,7 @@ public class SystemBuilder { } }.replaceWildCards(extendsList[j]); - system.addSubtypeConstraint(definedType, Util.banalize(definedSubst.substitute(extendsType))); + system.addSubtypeConstraint(definedType, Util.banalize(Util.substituteType(extendsType, definedSubst))); } } else {