safe delete: delete annotations if type is deleted (IDEA-154181)

This commit is contained in:
Anna.Kozlova
2016-05-12 16:17:23 +02:00
parent 78b56192d2
commit f4ea28d8bf
5 changed files with 61 additions and 1 deletions
@@ -330,6 +330,9 @@ public class JavaSafeDeleteProcessor extends SafeDeleteProcessorDelegateBase {
else if (usage instanceof SafeDeleteParameterCallHierarchyUsageInfo) {
delegatingParams.add((SafeDeleteParameterCallHierarchyUsageInfo)usage);
}
else if (usage instanceof SafeDeleteAnnotation) {
result.add(new SafeDeleteAnnotation((PsiAnnotation)usage.getElement(), ((SafeDeleteAnnotation)usage).getReferencedElement(), true));
}
else {
result.add(usage);
}
@@ -518,6 +521,8 @@ public class JavaSafeDeleteProcessor extends SafeDeleteProcessorDelegateBase {
private static void findClassUsages(final PsiClass psiClass, final PsiElement[] allElementsToDelete, final List<UsageInfo> usages) {
final boolean justPrivates = containsOnlyPrivates(psiClass);
final String qualifiedName = psiClass.getQualifiedName();
final boolean annotationType = psiClass.isAnnotationType() && qualifiedName != null;
ReferencesSearch.search(psiClass).forEach(new Processor<PsiReference>() {
public boolean process(final PsiReference reference) {
@@ -547,7 +552,14 @@ public class JavaSafeDeleteProcessor extends SafeDeleteProcessorDelegateBase {
classes[0] == psiClass &&
element.getContainingFile() == containingFile;
}
usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(element, psiClass, sameFileWithSingleClass || isInNonStaticImport(element)));
final boolean safeDelete = sameFileWithSingleClass || isInNonStaticImport(element);
if (annotationType && parent instanceof PsiAnnotation) {
usages.add(new SafeDeleteAnnotation((PsiAnnotation)parent, psiClass, safeDelete));
}
else {
usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(element, psiClass, safeDelete));
}
}
return true;
}
@@ -0,0 +1,34 @@
/*
* Copyright 2000-2016 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 com.intellij.refactoring.safeDelete.usageInfo;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
public class SafeDeleteAnnotation extends SafeDeleteReferenceUsageInfo {
public SafeDeleteAnnotation(PsiAnnotation element, PsiElement referencedElement, boolean safeDelete) {
super(element, referencedElement, safeDelete);
}
@Override
public void deleteElement() throws IncorrectOperationException {
final PsiElement element = getElement();
if (element != null) {
element.delete();
}
}
}
@@ -0,0 +1,3 @@
@interface <caret>A {}
@A
class Test {}
@@ -305,6 +305,16 @@ public class SafeDeleteTest extends MultiFileTestCase {
doSingleFileTest();
}
public void testShowConflictsButRemoveAnnotationsIfAnnotationTypeIsDeleted() throws Exception {
try {
BaseRefactoringProcessor.ConflictsInTestsException.setTestIgnore(true);
doSingleFileTest();
}
finally {
BaseRefactoringProcessor.ConflictsInTestsException.setTestIgnore(false);
}
}
private void doTest(@NonNls final String qClassName) throws Exception {
doTest((rootDir, rootAfter) -> {
SafeDeleteTest.this.performAction(qClassName);