From 374682e40459b8a170d0e3256705b6009aa55d26 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Tue, 28 Jan 2014 14:44:46 +0400 Subject: [PATCH] Extract utility methods from KotlinSafeDeleteProcessor --- .../psi/search/searches/annotations.xml | 12 ++ .../jet/lang/psi/psiUtil/jetPsiUtil.kt | 10 +- .../refactoring/JetRefactoringUtil.java | 74 ++++++++ .../KotlinSafeDeleteOverrideAnnotation.kt | 2 +- .../safeDelete/KotlinSafeDeleteProcessor.java | 163 ++---------------- .../SafeDeleteTypeArgumentListUsageInfo.kt | 5 +- .../plugin/refactoring/safeDelete/utils.kt | 53 ++++++ 7 files changed, 163 insertions(+), 156 deletions(-) create mode 100644 idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/utils.kt diff --git a/annotations/com/intellij/psi/search/searches/annotations.xml b/annotations/com/intellij/psi/search/searches/annotations.xml index 5f62b69d8aa..72806268982 100644 --- a/annotations/com/intellij/psi/search/searches/annotations.xml +++ b/annotations/com/intellij/psi/search/searches/annotations.xml @@ -7,4 +7,16 @@ name='com.intellij.psi.search.searches.DirectClassInheritorsSearch com.intellij.util.Query<com.intellij.psi.PsiClass> search(com.intellij.psi.PsiClass, com.intellij.psi.search.SearchScope, boolean, boolean)'> + + + + + + + + + \ No newline at end of file diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/psiUtil/jetPsiUtil.kt b/compiler/frontend/src/org/jetbrains/jet/lang/psi/psiUtil/jetPsiUtil.kt index d634e2e3e03..db79e061520 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/psiUtil/jetPsiUtil.kt +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/psiUtil/jetPsiUtil.kt @@ -189,4 +189,12 @@ fun PsiElement.isExtensionDeclaration(): Boolean { return callable?.getReceiverTypeRef() != null } -fun PsiElement.isObjectLiteral(): Boolean = this is JetObjectDeclaration && isObjectLiteral() \ No newline at end of file +fun PsiElement.isObjectLiteral(): Boolean = this is JetObjectDeclaration && isObjectLiteral() + +fun PsiElement.deleteElementAndCleanParent() { + val parent = getParent() + + JetPsiUtil.deleteElementWithDelimiters(this) + [suppress("UNCHECKED_CAST")] + JetPsiUtil.deleteChildlessElement(parent, this.getClass() as Class) +} \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/refactoring/JetRefactoringUtil.java b/idea/src/org/jetbrains/jet/plugin/refactoring/JetRefactoringUtil.java index f6148fb3fe5..2bf0e3e9253 100644 --- a/idea/src/org/jetbrains/jet/plugin/refactoring/JetRefactoringUtil.java +++ b/idea/src/org/jetbrains/jet/plugin/refactoring/JetRefactoringUtil.java @@ -18,6 +18,7 @@ package org.jetbrains.jet.plugin.refactoring; import com.intellij.codeInsight.unwrap.ScopeHighlighter; import com.intellij.ide.IdeBundle; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; @@ -27,6 +28,7 @@ import com.intellij.openapi.ui.popup.LightweightWindowEvent; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; +import com.intellij.psi.search.searches.OverridingMethodsSearch; import com.intellij.psi.util.PsiFormatUtil; import com.intellij.psi.util.PsiFormatUtilBase; import com.intellij.ui.components.JBList; @@ -37,6 +39,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.impl.LocalVariableDescriptor; import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContextUtils; import org.jetbrains.jet.lang.resolve.OverridingUtil; @@ -283,6 +286,77 @@ public class JetRefactoringUtil { return "class " + classOrObject.getName(); } + @Nullable + public static Collection checkParametersInMethodHierarchy(@NotNull PsiParameter parameter) { + PsiMethod method = (PsiMethod)parameter.getDeclarationScope(); + int parameterIndex = method.getParameterList().getParameterIndex(parameter); + + Set parametersToDelete = collectParametersHierarchy(method, parameterIndex); + if (parametersToDelete.size() > 1) { + if (ApplicationManager.getApplication().isUnitTestMode()) { + return parametersToDelete; + } + + String message = + JetBundle.message("delete.param.in.method.hierarchy", formatJavaOrLightMethod(method)); + int exitCode = Messages.showOkCancelDialog( + parameter.getProject(), message, IdeBundle.message("title.warning"), Messages.getQuestionIcon() + ); + if (exitCode == Messages.OK) { + return parametersToDelete; + } + else { + return null; + } + } + + return parametersToDelete; + } + + // TODO: generalize breadth-first search + @NotNull + private static Set collectParametersHierarchy(@NotNull PsiMethod method, int parameterIndex) { + Deque queue = new ArrayDeque(); + Set visited = new HashSet(); + Set parametersToDelete = new HashSet(); + + queue.add(method); + while (!queue.isEmpty()) { + PsiMethod currentMethod = queue.poll(); + + visited.add(currentMethod); + addParameter(currentMethod, parametersToDelete, parameterIndex); + + for (PsiMethod superMethod : currentMethod.findSuperMethods(true)) { + if (!visited.contains(superMethod)) { + queue.offer(superMethod); + } + } + for (PsiMethod overrider : OverridingMethodsSearch.search(currentMethod)) { + if (!visited.contains(overrider)) { + queue.offer(overrider); + } + } + } + return parametersToDelete; + } + + private static void addParameter(@NotNull PsiMethod method, @NotNull Set result, int parameterIndex) { + if (method instanceof KotlinLightMethod) { + JetDeclaration declaration = ((KotlinLightMethod) method).getOrigin(); + int jetParameterIndex = PsiUtilPackage.isExtensionDeclaration(declaration) ? parameterIndex - 1 : parameterIndex; + if (declaration instanceof JetNamedFunction) { + result.add(((JetNamedFunction) declaration).getValueParameters().get(jetParameterIndex)); + } + else if (declaration instanceof JetClass) { + result.add(((JetClass) declaration).getPrimaryConstructorParameters().get(jetParameterIndex)); + } + } + else { + result.add(method.getParameterList().getParameters()[parameterIndex]); + } + } + public interface SelectExpressionCallback { void run(@Nullable JetExpression expression); } diff --git a/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteOverrideAnnotation.kt b/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteOverrideAnnotation.kt index 79f465cc30b..98bbc226995 100644 --- a/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteOverrideAnnotation.kt +++ b/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteOverrideAnnotation.kt @@ -25,6 +25,6 @@ public class KotlinSafeDeleteOverrideAnnotation( element: PsiElement, referencedElement: PsiElement ) : SafeDeleteUsageInfo(element, referencedElement), SafeDeleteCustomUsageInfo { public override fun performRefactoring() { - getElement()?.let { element -> KotlinSafeDeleteProcessor.removeOverrideModifier(element) } + getElement()?.removeOverrideModifier() } } diff --git a/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteProcessor.java b/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteProcessor.java index 22ca070e2d2..b470565aa7e 100644 --- a/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteProcessor.java +++ b/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteProcessor.java @@ -16,15 +16,15 @@ package org.jetbrains.jet.plugin.refactoring.safeDelete; -import com.intellij.ide.IdeBundle; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; -import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.Conditions; -import com.intellij.psi.*; -import com.intellij.psi.search.searches.OverridingMethodsSearch; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiParameter; +import com.intellij.psi.PsiReference; import com.intellij.psi.search.searches.ReferencesSearch; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.refactoring.safeDelete.JavaSafeDeleteProcessor; @@ -400,49 +400,10 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return result.toArray(new UsageInfo[result.size()]); } - public static void removeOverrideModifier(@NotNull PsiElement element) { - if (element instanceof JetNamedFunction || element instanceof JetProperty) { - JetModifierList modifierList = ((JetModifierListOwner) element).getModifierList(); - if (modifierList == null) return; - - PsiElement overrideModifier = modifierList.getModifier(JetTokens.OVERRIDE_KEYWORD); - if (overrideModifier != null) { - overrideModifier.delete(); - } - } - else if (element instanceof PsiMethod) { - PsiMethod method = (PsiMethod) element; - - PsiAnnotation overrideAnnotation = null; - for (PsiAnnotation annotation : method.getModifierList().getAnnotations()) { - if ("java.lang.Override".equals(annotation.getQualifiedName())) { - overrideAnnotation = annotation; - break; - } - } - - if (overrideAnnotation != null) { - overrideAnnotation.delete(); - } - } - } - - @Nullable - private static PsiParameter getPsiParameter(@NotNull JetParameter parameter) { - JetNamedFunction function = PsiTreeUtil.getParentOfType(parameter, JetNamedFunction.class); - if (function == null || parameter.getParent() != function.getValueParameterList()) return null; - - PsiMethod lightMethod = LightClassUtil.getLightClassMethod(function); - if (lightMethod == null) return null; - - int parameterIndex = function.getValueParameters().indexOf(parameter); - return lightMethod.getParameterList().getParameters()[parameterIndex]; - } - @Override public void prepareForDeletion(@NotNull PsiElement element) throws IncorrectOperationException { if (element instanceof PsiMethod) { - cleanUpOverrides((PsiMethod) element); + SafeDeletePackage.cleanUpOverrides((PsiMethod) element); } else if (element instanceof JetNamedFunction) { PsiMethod lightMethod = LightClassUtil.getLightClassMethod((JetNamedFunction) element); @@ -450,7 +411,7 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return; } - cleanUpOverrides(lightMethod); + SafeDeletePackage.cleanUpOverrides(lightMethod); } else if (element instanceof JetProperty) { LightClassUtil.PropertyAccessorsPsiMethods propertyMethods = @@ -459,65 +420,32 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { PsiMethod setter = propertyMethods.getSetter(); if (getter != null) { - cleanUpOverrides(getter); + SafeDeletePackage.cleanUpOverrides(getter); } if (setter != null) { - cleanUpOverrides(setter); + SafeDeletePackage.cleanUpOverrides(setter); } } else if (element instanceof JetTypeParameter) { - deleteElementAndCleanParent(element); + PsiUtilPackage.deleteElementAndCleanParent(element); } else if (element instanceof JetParameter) { JetPsiUtil.deleteElementWithDelimiters(element); } } - public static void deleteElementAndCleanParent(@NotNull PsiElement element) { - PsiElement parent = element.getParent(); - JetPsiUtil.deleteElementWithDelimiters(element); - JetPsiUtil.deleteChildlessElement(parent, element.getClass()); - } - - private static boolean checkPsiMethodEquality(@NotNull PsiMethod method1, @NotNull PsiMethod method2) { - if (method1 instanceof KotlinLightMethod && method2 instanceof KotlinLightMethod) { - return ((KotlinLightMethod) method1).getOrigin().equals(((KotlinLightMethod) method2).getOrigin()); - } - return method1.equals(method2); - } - - public static void cleanUpOverrides(@NotNull PsiMethod method) { - Collection superMethods = Arrays.asList(method.findSuperMethods(true)); - Collection overridingMethods = OverridingMethodsSearch.search(method, true).findAll(); - overrideLoop: - for (PsiMethod overridingMethod : overridingMethods) { - PsiElement overridingElement = overridingMethod instanceof KotlinLightMethod - ? ((KotlinLightMethod) overridingMethod).getOrigin() - : overridingMethod; - - Collection currentSuperMethods = new ArrayList(); - ContainerUtil.addAll(currentSuperMethods, overridingMethod.findSuperMethods(true)); - currentSuperMethods.addAll(superMethods); - for (PsiMethod superMethod : currentSuperMethods) { - if (!checkPsiMethodEquality(superMethod, method)) continue overrideLoop; - } - - removeOverrideModifier(overridingElement); - } - } - @Nullable @Override public Collection getElementsToSearch( @NotNull PsiElement element, @Nullable Module module, @NotNull Collection allElementsToDelete ) { if (element instanceof JetParameter) { - PsiParameter psiParameter = getPsiParameter((JetParameter) element); - if (psiParameter != null) return checkParametersInMethodHierarchy(psiParameter); + PsiParameter psiParameter = AsJavaPackage.toPsiParameter((JetParameter) element); + if (psiParameter != null) return JetRefactoringUtil.checkParametersInMethodHierarchy(psiParameter); } if (element instanceof PsiParameter) { - return checkParametersInMethodHierarchy((PsiParameter) element); + return JetRefactoringUtil.checkParametersInMethodHierarchy((PsiParameter) element); } if (ApplicationManager.getApplication().isUnitTestMode()) { @@ -532,71 +460,4 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return super.getElementsToSearch(element, module, allElementsToDelete); } - - @Nullable - private static Collection checkParametersInMethodHierarchy(@NotNull PsiParameter parameter) { - PsiMethod method = (PsiMethod)parameter.getDeclarationScope(); - int parameterIndex = method.getParameterList().getParameterIndex(parameter); - - Set parametersToDelete = collectParametersToDelete(method, parameterIndex); - if (parametersToDelete.size() > 1) { - if (ApplicationManager.getApplication().isUnitTestMode()) { - return parametersToDelete; - } - - String message = - JetBundle.message("delete.param.in.method.hierarchy", JetRefactoringUtil.formatJavaOrLightMethod(method)); - int exitCode = Messages.showOkCancelDialog( - parameter.getProject(), message, IdeBundle.message("title.warning"), Messages.getQuestionIcon() - ); - if (exitCode == Messages.OK) { - return parametersToDelete; - } - else { - return null; - } - } - - return parametersToDelete; - } - - // TODO: generalize breadth-first search - @NotNull - private static Set collectParametersToDelete(@NotNull PsiMethod method, int parameterIndex) { - Deque queue = new ArrayDeque(); - Set visited = new HashSet(); - Set parametersToDelete = new HashSet(); - - queue.add(method); - while (!queue.isEmpty()) { - PsiMethod currentMethod = queue.poll(); - - visited.add(currentMethod); - addParameter(currentMethod, parametersToDelete, parameterIndex); - - for (PsiMethod superMethod : currentMethod.findSuperMethods(true)) { - if (!visited.contains(superMethod)) { - queue.offer(superMethod); - } - } - for (PsiMethod overrider : OverridingMethodsSearch.search(currentMethod)) { - if (!visited.contains(overrider)) { - queue.offer(overrider); - } - } - } - return parametersToDelete; - } - - private static void addParameter(@NotNull PsiMethod method, @NotNull Set result, int parameterIndex) { - if (method instanceof KotlinLightMethod) { - JetDeclaration declaration = ((KotlinLightMethod) method).getOrigin(); - if (declaration instanceof JetNamedFunction) { - result.add(((JetNamedFunction) declaration).getValueParameters().get(parameterIndex)); - } - } - else { - result.add(method.getParameterList().getParameters()[parameterIndex]); - } - } } diff --git a/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/SafeDeleteTypeArgumentListUsageInfo.kt b/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/SafeDeleteTypeArgumentListUsageInfo.kt index 623beac6c7e..4dd8633b4c5 100644 --- a/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/SafeDeleteTypeArgumentListUsageInfo.kt +++ b/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/SafeDeleteTypeArgumentListUsageInfo.kt @@ -16,15 +16,14 @@ package org.jetbrains.jet.plugin.refactoring.safeDelete -import com.intellij.psi.PsiElement import com.intellij.refactoring.safeDelete.usageInfo.SafeDeleteReferenceSimpleDeleteUsageInfo -import com.intellij.util.IncorrectOperationException import org.jetbrains.jet.lang.psi.* +import org.jetbrains.jet.lang.psi.psiUtil.deleteElementAndCleanParent public class SafeDeleteTypeArgumentListUsageInfo( typeProjection: JetTypeProjection, parameter: JetTypeParameter ) : SafeDeleteReferenceSimpleDeleteUsageInfo(typeProjection, parameter, true) { public override fun deleteElement() { - getElement()?.let { element -> KotlinSafeDeleteProcessor.deleteElementAndCleanParent(element) } + getElement()?.deleteElementAndCleanParent() } } diff --git a/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/utils.kt b/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/utils.kt new file mode 100644 index 00000000000..2836c0b3292 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/utils.kt @@ -0,0 +1,53 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.refactoring.safeDelete + +import org.jetbrains.jet.lang.psi.JetNamedFunction +import com.intellij.psi.PsiMethod +import org.jetbrains.jet.lang.psi.JetModifierListOwner +import org.jetbrains.jet.lang.psi.JetProperty +import com.intellij.psi.PsiElement +import org.jetbrains.jet.lexer.JetTokens +import org.jetbrains.jet.lang.psi.JetPsiUtil +import com.intellij.psi.search.searches.OverridingMethodsSearch +import org.jetbrains.jet.asJava.unwrapped +import java.util.ArrayList + +fun PsiElement.removeOverrideModifier() { + val modifier = when (this) { + is JetNamedFunction, is JetProperty -> { + (this as JetModifierListOwner).getModifierList()?.getModifier(JetTokens.OVERRIDE_KEYWORD) + } + is PsiMethod -> { + getModifierList().getAnnotations().find { + annotation -> annotation.getQualifiedName() == "java.lang.Override" + } + } + else -> null + } + modifier?.let { modifier -> modifier.delete() } +} + +fun PsiMethod.cleanUpOverrides() { + val superMethods = findSuperMethods(true) + for (overridingMethod in OverridingMethodsSearch.search(this, true).findAll()) { + val currentSuperMethods = overridingMethod.findSuperMethods(true).iterator() + superMethods.iterator() + if (currentSuperMethods.all { superMethod -> superMethod.unwrapped == unwrapped }) { + overridingMethod.unwrapped?.removeOverrideModifier() + } + } +} \ No newline at end of file