From 5cfdd0ac873066d7f2df157d5da76c6feb1fa34c Mon Sep 17 00:00:00 2001 From: Dmitri Boulytchev Date: Fri, 18 Feb 2005 19:46:58 +0300 Subject: [PATCH] Iterative Cook Killed --- resources/src/idea/ActionManager.xml | 4 +- .../openapi/impl/TypeCookRefactoringImpl.java | 2 +- .../intellij/refactoring/typeCook/Util.java | 342 +----------------- .../typeCook/deductive/builder/System.java | 1 - .../intellij/refactoring/TypeCookTest.java | 17 +- 5 files changed, 5 insertions(+), 361 deletions(-) diff --git a/resources/src/idea/ActionManager.xml b/resources/src/idea/ActionManager.xml index 329467ffccef..7658d8155752 100644 --- a/resources/src/idea/ActionManager.xml +++ b/resources/src/idea/ActionManager.xml @@ -546,9 +546,7 @@ - - - + diff --git a/source/com/intellij/refactoring/openapi/impl/TypeCookRefactoringImpl.java b/source/com/intellij/refactoring/openapi/impl/TypeCookRefactoringImpl.java index 9803f6dd3657..d4a359282273 100644 --- a/source/com/intellij/refactoring/openapi/impl/TypeCookRefactoringImpl.java +++ b/source/com/intellij/refactoring/openapi/impl/TypeCookRefactoringImpl.java @@ -9,7 +9,7 @@ import com.intellij.psi.PsiElement; import com.intellij.refactoring.RefactoringImpl; import com.intellij.refactoring.TypeCookRefactoring; import com.intellij.refactoring.typeCook.Settings; -import com.intellij.refactoring.typeCook.deductive.TypeCookProcessor; +import com.intellij.refactoring.typeCook.TypeCookProcessor; import java.util.List; diff --git a/source/com/intellij/refactoring/typeCook/Util.java b/source/com/intellij/refactoring/typeCook/Util.java index 47a227d41450..73019a61cd74 100644 --- a/source/com/intellij/refactoring/typeCook/Util.java +++ b/source/com/intellij/refactoring/typeCook/Util.java @@ -25,8 +25,6 @@ import java.util.Set; public class Util { private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.typeCook.Util"); - private static final int HEIGHT_BOUND = 6; - public static int getArrayLevel(PsiType t) { if (t instanceof PsiArrayType) { return 1 + getArrayLevel(((PsiArrayType)t).getComponentType()); @@ -56,41 +54,13 @@ public class Util { else if (type instanceof PsiArrayType) { return resolveType(((PsiArrayType)type).getComponentType()); } - else if (type instanceof SubtypeOf) { - return resolveType(((SubtypeOf)type).getSuperType()); - } - else if (type instanceof SupertypeOf) { - return resolveType(((SupertypeOf)type).getSubType()); - } else { return PsiClassType.ClassResolveResult.EMPTY; } } - public static PsiElement getParentElement(PsiElement element) { - PsiElement parent = element; - - while ((parent = parent.getParent()) instanceof PsiParenthesizedExpression) ; - - return parent; - } - - public static boolean isSonOfReferenceExpression(PsiElement he) { - PsiElement wormlet = he; - - while ((wormlet = wormlet.getParent()) instanceof PsiParenthesizedExpression) ; - - return wormlet instanceof PsiReferenceExpression; - } - public static PsiType normalize(PsiType t, boolean objectBottom) { - if (t instanceof SubtypeOf) { - return normalize(((SubtypeOf)t).getSuperType(), objectBottom); - } - else if (t instanceof SupertypeOf) { - return normalize(((SupertypeOf)t).getSubType(), objectBottom); - } - else if (t instanceof PsiArrayType) { + if (t instanceof PsiArrayType) { PsiType normType = normalize(((PsiArrayType)t).getComponentType(), objectBottom); return normType == null ? null : normType.createArrayType(); @@ -147,45 +117,6 @@ public class Util { } } - public static boolean hasNoParameters(PsiType t) { - if (t instanceof PsiClassType) { - PsiClassType.ClassResolveResult resolveResult = resolveType(t); - - if (resolveResult == null) { - return true; - } - - if (PsiClassType.isRaw(resolveResult)) { - return true; - } - - PsiSubstitutor subst = resolveResult.getSubstitutor(); - PsiClass element = resolveResult.getElement(); - - if (element instanceof PsiTypeParameter) { - return false; - } - - PsiTypeParameter[] parameters = getTypeParametersList(element); - - for (int i = 0; i < parameters.length; i++) { - PsiType actual = subst.substitute(parameters[i]); - if (hasNoParameters(actual)) { - return true; - } - } - } - else if (t instanceof PsiArrayType) { - return hasNoParameters(((PsiArrayType)t).getComponentType()); - } - - return false; - } - - public static boolean isRaw(PsiType t) { - return isRaw(t, true); - } - public static boolean isRaw(PsiType t, boolean arrays) { if (t instanceof PsiClassType) { final PsiClassType.ClassResolveResult resolveResult = resolveType(t); @@ -217,17 +148,6 @@ public class Util { return false; } - public static boolean isTypeParameter(PsiType t) { - - PsiClassType.ClassResolveResult result = resolveType(t); - - if (result == null) { - return false; - } - - return result.getElement() instanceof PsiTypeParameter; - } - public static PsiType banalize(final PsiType t) { if (t instanceof PsiClassType) { final PsiClassType.ClassResolveResult result = resolveType(t); @@ -286,32 +206,6 @@ public class Util { return subst; } - public static boolean bindsTypeParameters(PsiSubstitutor theSubst, HashSet params) { - Collection values = theSubst.getSubstitutionMap().values(); - - for (Iterator i = values.iterator(); i.hasNext();) { - PsiType type = i.next(); - PsiClassType.ClassResolveResult result = Util.resolveType(type); - - if (result == null) { - return false; - } - - PsiClass aClass = result.getElement(); - - if (aClass instanceof PsiTypeParameter) { - return params.contains(aClass); - } - else { - if (bindsTypeParameters(result.getSubstitutor(), params)) { - return true; - } - } - } - - return false; - } - public static boolean bindsTypeParameters(PsiType t, HashSet params) { if (t instanceof PsiWildcardType) { final PsiWildcardType wct = ((PsiWildcardType)t); @@ -350,72 +244,6 @@ public class Util { return false; } - public static boolean bindsTypeParameters(PsiType t) { - return bindsTypeParameters(t, null); - } - - public static PsiSubstitutor createIdentitySubstitutor(PsiTypeParameterList p) { - PsiSubstitutor subst = PsiSubstitutor.EMPTY; - PsiTypeParameter[] parms = p.getTypeParameters(); - - for (int i = 0; i < parms.length; i++) { - PsiTypeParameter pp = parms[i]; - subst = subst.put(pp, pp.getManager().getElementFactory().createType(pp)); - } - - return subst; - } - - public static PsiSubstitutor createIdentitySubstitutor(Set params) { - PsiSubstitutor subst = PsiSubstitutor.EMPTY; - - for (Iterator i = params.iterator(); i.hasNext();) { - PsiTypeParameter p = i.next(); - subst = subst.put(p, p.getManager().getElementFactory().createType(p)); - } - - return subst; - } - - public static PsiType getNCA(PsiClass aClass, PsiClass bClass) { - if (InheritanceUtil.isCorrectDescendant(aClass, bClass, true)) { - return aClass.getManager().getElementFactory() - .createType(bClass); - } - if (InheritanceUtil.isCorrectDescendant(bClass, aClass, true)) { - return aClass.getManager().getElementFactory() - .createType(aClass); - } - ; - - return PsiType.getJavaLangObject(aClass.getManager()); - } - - public static PsiType cloneType(PsiType t, PsiManager manager) { - return manager.getElementFactory().detachType(t); - } - - public static TypeNode killOthers(TypeNode baseNode, - TypeNode objectNode, - PsiClass mainClass, - HashSet boundParameters) { - if (!mainClass.hasTypeParameters()) { - return baseNode; - } - - PsiTypeParameter[] mainParms = getTypeParametersList(mainClass); - - for (int i = 0; i < mainParms.length; i++) { - PsiTypeParameter p = mainParms[i]; - - if (!boundParameters.contains(p)) { - TypeEdge.connectParameter(baseNode, objectNode, p.getIndex()); - } - } - - return baseNode; - } - public static PsiType getType(PsiElement element) { if (element instanceof PsiVariable) { return ((PsiVariable)element).getType(); @@ -430,49 +258,6 @@ public class Util { return null; } - public static boolean equals(PsiClass a, PsiClass b) { - if (a.getManager().areElementsEquivalent(a, b)) { - return getTypeParametersList(a).length == getTypeParametersList(b).length; - } - - return false; - } - - private static boolean clashes(PsiClass a, PsiClass b) { - return - a.getManager().areElementsEquivalent(a, b) && - (getTypeParametersList(a).length != getTypeParametersList(b).length); - } - - public static boolean isDescendant(PsiClass a, PsiClass b) { - boolean semi = InheritanceUtil.isCorrectDescendant(a, b, true); - - if (semi && clashes(a, b)) { - return false; - } - - return semi; - } - - public static int getTypeKind(PsiType t) { - if (t instanceof PsiClassType) return 0; - if (t instanceof PsiArrayType) return 1; - if (t instanceof SupertypeOf) return 2; - if (t instanceof SubtypeOf) return 3; - if (t instanceof Bottom) return 4; - - LOG.error("Class/Array/Super/Sub-type expected in getTypeKind."); - - return 5; // PsiPrimitiveType - } - - public static PsiType balanceSubtype(PsiClass aClass, PsiClass bClass, PsiSubstitutor aSubst) { - PsiSubstitutor subSubst = TypeConversionUtil.getClassSubstitutor(bClass, aClass, PsiSubstitutor.EMPTY); - PsiSubstitutor theSubst = Util.composeSubstitutors(aSubst, subSubst); - - return aClass.getManager().getElementFactory().createType(bClass, theSubst); - } - public static PsiTypeParameter[] getTypeParametersList(PsiClass a) { PsiTypeParameterList list = a.getTypeParameterList(); @@ -484,46 +269,6 @@ public class Util { } } - public static boolean isGeneric(PsiType a) { - if (a instanceof PsiArrayType) { - return isGeneric(((PsiArrayType)a).getDeepComponentType()); - } - - if (a instanceof SubtypeOf) { - return isGeneric(((SubtypeOf)a).getSuperType()); - } - - if (a instanceof SupertypeOf) { - return isGeneric(((SupertypeOf)a).getSubType()); - } - - if (a instanceof PsiClassType) { - PsiClassType.ClassResolveResult result = resolveType(a); - - if (result == null) { - return false; - } - - PsiClass aClass = result.getElement(); - - return aClass != null && aClass.hasTypeParameters(); - } - - return a == Bottom.BOTTOM; - } - - public static PsiType undress(PsiType t) { - if (t instanceof SupertypeOf) { - return undress(((SupertypeOf)t).getSubType()); - } - - if (t instanceof SubtypeOf) { - return undress(((SubtypeOf)t).getSuperType()); - } - - return t; - } - public static PsiType avoidAnonymous(PsiType type) { if (type instanceof PsiClassType) { final PsiClassType.ClassResolveResult result = ((PsiClassType)type).resolveGenerics(); @@ -563,91 +308,6 @@ public class Util { return type; } - private static boolean isValidType(PsiType type, PsiElement context) { - if (type instanceof PsiClassType) { - PsiClassType.ClassResolveResult result = resolveType(type); - - if (result == null) { - return false; - } - - PsiClass aClass = result.getElement(); - PsiSubstitutor aSubst = result.getSubstitutor(); - - if (!PsiUtil.isAccessible(aClass, context, null)) { - return false; - } - - PsiTypeParameter[] aParms = getTypeParametersList(aClass); - - for (int i = 0; i < aParms.length; i++) { - if (!isValidType(aSubst.substitute(aParms[i]), context)) { - return false; - } - } - - return true; - } - - if (type instanceof PsiArrayType) { - return isValidType(((PsiArrayType)type).getComponentType(), context); - } - - return true; - } - - public static boolean isValidTypeInContext(PsiType type, PsiElement context) { - try { - return isValidType(context.getManager().getElementFactory().createTypeFromText(type.getCanonicalText(), context), - context); - } - catch (IncorrectOperationException e) { - LOG.error("Incorrect operation during factory.createTypeFromText"); - } - - return false; - } - - private static int getHeight(PsiType t) { - if (t instanceof PsiClassType) { - PsiClassType.ClassResolveResult result = ((PsiClassType)t).resolveGenerics(); - PsiClass aClass = result.getElement(); - PsiSubstitutor aSubst = result.getSubstitutor(); - - if (aClass == null) { - return 0; - } - - PsiTypeParameter[] parms = getTypeParametersList(aClass); - - int max = 0; - - for (int i = 0; i < parms.length; i++) { - max = Math.max(max, getHeight(aSubst.substitute(parms[i]))); - if (max > HEIGHT_BOUND) { - break; - } - } - - return 1 + max; - } - else if (t instanceof PsiArrayType) { - return getHeight(((PsiArrayType)t).getDeepComponentType()); - } - else if (t instanceof SubtypeOf) { - return getHeight(((SubtypeOf)t).getSuperType()); - } - else if (t instanceof SupertypeOf) { - return getHeight(((SupertypeOf)t).getSubType()); - } - - return 0; - } - - public static boolean prunedType(PsiType t) { - return getHeight(t) > HEIGHT_BOUND; - } - public static PsiType createParameterizedType(final PsiType t, final PsiTypeVariableFactory factory) { if (t == null) { return factory.create(); diff --git a/source/com/intellij/refactoring/typeCook/deductive/builder/System.java b/source/com/intellij/refactoring/typeCook/deductive/builder/System.java index 133202ef8c66..ff9697ba2df7 100644 --- a/source/com/intellij/refactoring/typeCook/deductive/builder/System.java +++ b/source/com/intellij/refactoring/typeCook/deductive/builder/System.java @@ -3,7 +3,6 @@ package com.intellij.refactoring.typeCook.deductive.builder; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.refactoring.typeCook.Util; -import com.intellij.refactoring.typeCook.TypeNode; import com.intellij.refactoring.typeCook.Bottom; import com.intellij.refactoring.typeCook.Settings; import com.intellij.refactoring.typeCook.deductive.PsiTypeVariable; diff --git a/testSource/com/intellij/refactoring/TypeCookTest.java b/testSource/com/intellij/refactoring/TypeCookTest.java index aa6c5109465c..53c1fc24e295 100644 --- a/testSource/com/intellij/refactoring/TypeCookTest.java +++ b/testSource/com/intellij/refactoring/TypeCookTest.java @@ -7,17 +7,15 @@ import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; -import com.intellij.refactoring.typeCook.Kitchen; import com.intellij.refactoring.typeCook.Settings; -import com.intellij.refactoring.typeCook.deductive.builder.*; -import com.intellij.refactoring.typeCook.deductive.resolver.ResolverTree; +import com.intellij.refactoring.typeCook.deductive.builder.SystemBuilder; import com.intellij.refactoring.typeCook.deductive.resolver.Binding; +import com.intellij.refactoring.typeCook.deductive.resolver.ResolverTree; import junit.textui.TestRunner; import java.io.File; import java.io.FileOutputStream; import java.io.PrintWriter; -import java.lang.System; /** * Created by IntelliJ IDEA. @@ -682,14 +680,6 @@ public class TypeCookTest extends MultiFileTestCase { binding = tree.getBestSolution(); } - //System.out.println("" + system); - - //if (system != null) return; - - //Kitchen d = new Kitchen(aClass.getManager()); - - //d.buildGraph(new PsiElement[]{aClass}); - String itemRepr = system != null ? system.dumpString() : commonSystem.dumpString();// d.resultString(); String itemName = className + ".items"; String patternName = PathManagerEx.getTestDataPath() + getTestRoot() + getTestName(true) + "/after/" + itemName; @@ -716,9 +706,6 @@ public class TypeCookTest extends MultiFileTestCase { LocalFileSystem.getInstance().refreshAndFindFileByIoFile(graFile); - //d.analyze(); - //d.relax(); - itemRepr = system != null ? system.dumpResult(binding) : commonSystem.dumpString(); //d.resultString(); itemName = className + ".1.items";