From 41c98508b62d3f092cbdd212de15d5c24c609820 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Thu, 1 Aug 2013 15:52:08 +0400 Subject: [PATCH] SafeDeleteProcessor: Add nullability annotations Conflicts: idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteProcessor.java Conflicts: idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteProcessor.java Conflicts: idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteProcessor.java --- .../safeDelete/KotlinSafeDeleteProcessor.java | 215 +++++++++++------- 1 file changed, 136 insertions(+), 79 deletions(-) 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 918d5564e7a..7bfad45842d 100644 --- a/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteProcessor.java +++ b/idea/src/org/jetbrains/jet/plugin/refactoring/safeDelete/KotlinSafeDeleteProcessor.java @@ -60,7 +60,7 @@ import org.jetbrains.jet.renderer.DescriptorRenderer; import java.util.*; public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { - public static boolean canDeleteElement(PsiElement element) { + public static boolean canDeleteElement(@NotNull PsiElement element) { return element instanceof JetClassOrObject || element instanceof JetObjectDeclarationName || element instanceof JetNamedFunction @@ -71,10 +71,11 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } @Override - public boolean handlesElement(PsiElement element) { + public boolean handlesElement(@NotNull PsiElement element) { return canDeleteElement(element); } + @NotNull protected static NonCodeUsageSearchInfo getSearchInfo( @NotNull PsiElement element, @NotNull final Collection ignoredElements ) { @@ -91,13 +92,16 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { ); } + @NotNull protected static NonCodeUsageSearchInfo getSearchInfo(@NotNull PsiElement element, @NotNull PsiElement[] ignoredElements) { return getSearchInfo(element, Arrays.asList(ignoredElements)); } @Nullable @Override - public NonCodeUsageSearchInfo findUsages(PsiElement element, PsiElement[] allElementsToDelete, List result) { + public NonCodeUsageSearchInfo findUsages( + @NotNull PsiElement element, @NotNull PsiElement[] allElementsToDelete, @NotNull List result + ) { if (element instanceof JetClassOrObject) { return findClassOrObjectUsages(element, (JetClassOrObject) element, allElementsToDelete, result); } @@ -136,19 +140,20 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return getSearchInfo(element, allElementsToDelete); } - private static PsiElement getObjectDeclarationOrFail(PsiElement element) { + @NotNull + private static PsiElement getObjectDeclarationOrFail(@NotNull PsiElement element) { PsiElement parent = element.getParent(); assert parent instanceof JetObjectDeclaration; return parent; } @SuppressWarnings("MethodOverridesPrivateMethodOfSuperclass") - protected static boolean isInside(PsiElement place, PsiElement[] ancestors) { + protected static boolean isInside(@NotNull PsiElement place, @NotNull PsiElement[] ancestors) { return isInside(place, Arrays.asList(ancestors)); } @SuppressWarnings("MethodOverridesPrivateMethodOfSuperclass") - protected static boolean isInside(PsiElement place, Collection ancestors) { + protected static boolean isInside(@NotNull PsiElement place, @NotNull Collection ancestors) { for (PsiElement element : ancestors) { if (isInside(place, element)) return true; } @@ -156,18 +161,19 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } @SuppressWarnings("MethodOverridesStaticMethodOfSuperclass") - public static boolean isInside(PsiElement place, PsiElement ancestor) { + public static boolean isInside(@NotNull PsiElement place, @NotNull PsiElement ancestor) { if (ancestor instanceof JetClsMethod) { ancestor = ((JetClsMethod) ancestor).getOrigin(); } return JavaSafeDeleteProcessor.isInside(place, ancestor); } + @NotNull protected static NonCodeUsageSearchInfo findClassOrObjectUsages( - PsiElement referencedElement, - final JetClassOrObject classOrObject, - final PsiElement[] allElementsToDelete, - final List result + @NotNull PsiElement referencedElement, + @NotNull final JetClassOrObject classOrObject, + @NotNull final PsiElement[] allElementsToDelete, + @NotNull final List result ) { ReferencesSearch.search(referencedElement).forEach(new Processor() { @Override @@ -190,10 +196,11 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return getSearchInfo(referencedElement, allElementsToDelete); } + @Nullable protected NonCodeUsageSearchInfo findPsiMethodUsages( - PsiMethod method, - PsiElement[] allElementsToDelete, - List result + @NotNull PsiMethod method, + @NotNull PsiElement[] allElementsToDelete, + @NotNull List result ) { List javaUsages = new ArrayList(); NonCodeUsageSearchInfo searchInfo = super.findUsages(method, allElementsToDelete, javaUsages); @@ -217,18 +224,19 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return searchInfo; } - private static List difference(Collection from, T[] a) { + @NotNull + private static List difference(@NotNull Collection from, @NotNull T[] a) { List list = new ArrayList(from); list.removeAll(Arrays.asList(a)); return list; } private static void processDeclarationUsages( - JetDeclaration declaration, - PsiElement[] allElementsToDelete, - List result, - Collection references, - List overridingDeclarations + @NotNull JetDeclaration declaration, + @NotNull PsiElement[] allElementsToDelete, + @NotNull List result, + @NotNull Collection references, + @NotNull List overridingDeclarations ) { for (PsiReference reference : references) { PsiElement element = reference.getElement(); @@ -244,10 +252,11 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } } + @NotNull protected static NonCodeUsageSearchInfo findFunctionUsages( - JetNamedFunction function, - PsiElement[] allElementsToDelete, - List result + @NotNull JetNamedFunction function, + @NotNull PsiElement[] allElementsToDelete, + @NotNull List result ) { PsiMethod lightMethod = LightClassUtil.getLightClassMethod(function); if (lightMethod == null) { @@ -268,10 +277,11 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return getSearchInfo(function, ignoredElements); } + @NotNull protected static NonCodeUsageSearchInfo findPropertyUsages( - JetProperty property, - PsiElement[] allElementsToDelete, - List result + @NotNull JetProperty property, + @NotNull PsiElement[] allElementsToDelete, + @NotNull List result ) { LightClassUtil.PropertyAccessorsPsiMethods propertyMethods = LightClassUtil.getLightClassPropertyMethods(property); PsiMethod getter = propertyMethods.getGetter(); @@ -293,9 +303,13 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { Map> methodToReferences = getOverridingUsagesMap(overridingMethods); Set safeGetterOverriding = - filterSafeOverridingMethods(getter, references, getterOverriding, methodToReferences, result, allElementsToDelete); + getter != null + ? filterSafeOverridingMethods(getter, references, getterOverriding, methodToReferences, result, allElementsToDelete) + : Collections.emptySet(); Set safeSetterOverriding = - filterSafeOverridingMethods(setter, references, setterOverriding, methodToReferences, result, allElementsToDelete); + setter != null + ? filterSafeOverridingMethods(setter, references, setterOverriding, methodToReferences, result, allElementsToDelete) + : Collections.emptySet(); List ignoredElements = new ArrayList(safeGetterOverriding); ignoredElements.addAll(safeSetterOverriding); @@ -303,7 +317,8 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return getSearchInfo(property, ignoredElements); } - private static Map> getOverridingUsagesMap(List overridingMethods) { + @NotNull + private static Map> getOverridingUsagesMap(@NotNull List overridingMethods) { Map> methodToReferences = new HashMap>(); for (PsiMethod overridingMethod : overridingMethods) { Collection overridingReferences = @@ -315,10 +330,11 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return methodToReferences; } + @NotNull protected static NonCodeUsageSearchInfo findLocalVariableUsages( - final JetProperty property, - final PsiElement[] allElementsToDelete, - final List result + @NotNull final JetProperty property, + @NotNull final PsiElement[] allElementsToDelete, + @NotNull final List result ) { ReferencesSearch.search(property, property.getUseScope()).forEach(new Processor() { @Override @@ -334,10 +350,11 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return getSearchInfo(property, allElementsToDelete); } + @NotNull protected static NonCodeUsageSearchInfo findParameterUsages( - final JetParameter parameter, - final PsiElement[] allElementsToDelete, - final List result + @NotNull final JetParameter parameter, + @NotNull final PsiElement[] allElementsToDelete, + @NotNull final List result ) { NonCodeUsageSearchInfo searchInfo = getSearchInfo(parameter, allElementsToDelete); @@ -371,11 +388,11 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } static void processParameterUsageInCall( - PsiReference reference, - PsiElement originalDeclaration, + @NotNull PsiReference reference, + @NotNull PsiElement originalDeclaration, int parameterIndex, - List result, - PsiElement parameter + @NotNull List result, + @NotNull PsiElement parameter ) { PsiElement element = reference.getElement(); @@ -412,10 +429,11 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } } + @NotNull protected static NonCodeUsageSearchInfo findTypeParameterUsages( - final JetTypeParameter parameter, - final PsiElement[] allElementsToDelete, - final List result + @NotNull final JetTypeParameter parameter, + @NotNull final PsiElement[] allElementsToDelete, + @NotNull final List result ) { NonCodeUsageSearchInfo searchInfo = getSearchInfo(parameter, allElementsToDelete); @@ -459,11 +477,11 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } private static void processJavaTypeArgumentListCandidate( - PsiJavaCodeReferenceElement reference, + @NotNull PsiJavaCodeReferenceElement reference, int parameterIndex, int parameterCount, - List result, - PsiElement parameter + @NotNull List result, + @NotNull PsiElement parameter ) { PsiReferenceParameterList parameterList = reference.getParameterList(); if (parameterList != null) { @@ -479,10 +497,10 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } private static void processKotlinTypeArgumentListCandidate( - PsiReference reference, + @NotNull PsiReference reference, int parameterIndex, - List result, - JetTypeParameter parameter + @NotNull List result, + @NotNull JetTypeParameter parameter ) { PsiElement referencedElement = reference.getElement(); @@ -513,11 +531,12 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { * (simplified and implemented proper treatment of light methods) */ private static Set filterSafeOverridingMethods( - PsiMethod originalMethod, Collection originalReferences, - Collection overridingMethods, - Map> methodToReferences, - List usages, - PsiElement[] allElementsToDelete + @NotNull PsiMethod originalMethod, + @NotNull Collection originalReferences, + @NotNull Collection overridingMethods, + @NotNull Map> methodToReferences, + @NotNull List usages, + @NotNull PsiElement[] allElementsToDelete ) { Set validOverriding = new LinkedHashSet(overridingMethods); boolean anyNewBadRefs; @@ -565,7 +584,9 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } @SuppressWarnings("MethodOverridesPrivateMethodOfSuperclass") - private static boolean isMultipleInterfacesImplementation(PsiMethod method, PsiMethod originalMethod, PsiElement[] ignore) { + private static boolean isMultipleInterfacesImplementation( + @NotNull PsiMethod method, @NotNull PsiMethod originalMethod, @NotNull PsiElement[] ignore + ) { PsiMethod[] methods = method.findSuperMethods(); for (PsiMethod superMethod : methods) { PsiElement relevantElement = superMethod instanceof JetClsMethod ? ((JetClsMethod) superMethod).getOrigin() : superMethod; @@ -577,11 +598,17 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return false; } - private static String wrapOrSkip(String s, boolean inCode) { + @NotNull + private static String wrapOrSkip(@NotNull String s, boolean inCode) { return inCode ? "" + s + "" : s; } - private static String formatClass(DeclarationDescriptor classDescriptor, BindingContext bindingContext, boolean inCode) { + @NotNull + private static String formatClass( + @NotNull DeclarationDescriptor classDescriptor, + @NotNull BindingContext bindingContext, + boolean inCode + ) { PsiElement element = BindingContextUtils.descriptorToDeclaration(bindingContext, classDescriptor); if (element instanceof PsiClass) { return formatPsiClass((PsiClass) element, false, inCode); @@ -590,7 +617,12 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return wrapOrSkip(formatClassDescriptor(classDescriptor), inCode); } - private static String formatFunction(DeclarationDescriptor functionDescriptor, BindingContext bindingContext, boolean inCode) { + @NotNull + private static String formatFunction( + @NotNull DeclarationDescriptor functionDescriptor, + @NotNull BindingContext bindingContext, + boolean inCode + ) { PsiElement element = BindingContextUtils.descriptorToDeclaration(bindingContext, functionDescriptor); if (element instanceof PsiMethod) { return formatPsiMethod((PsiMethod) element, false, inCode); @@ -599,15 +631,22 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return wrapOrSkip(formatFunctionDescriptor(functionDescriptor), inCode); } - private static String formatClassDescriptor(DeclarationDescriptor classDescriptor) { + @NotNull + private static String formatClassDescriptor(@NotNull DeclarationDescriptor classDescriptor) { return DescriptorRenderer.SOURCE_CODE_SHORT_NAMES_IN_TYPES.render(classDescriptor); } - private static String formatFunctionDescriptor(DeclarationDescriptor functionDescriptor) { + @NotNull + private static String formatFunctionDescriptor(@NotNull DeclarationDescriptor functionDescriptor) { return DescriptorRenderer.COMPACT.render(functionDescriptor); } - public static String formatPsiClass(PsiClass psiClass, boolean markAsJava, boolean inCode) { + @NotNull + public static String formatPsiClass( + @NotNull PsiClass psiClass, + boolean markAsJava, + boolean inCode + ) { String description; String kind = psiClass.isInterface() ? "interface " : "class "; @@ -623,7 +662,11 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return markAsJava ? "[Java] " + description : description; } - public static String formatPsiMethod(PsiMethod psiMethod, boolean showContainingClass, boolean inCode) { + @NotNull + public static String formatPsiMethod( + @NotNull PsiMethod psiMethod, + boolean showContainingClass, + boolean inCode) { int options = PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS | PsiFormatUtilBase.SHOW_TYPE; if (showContainingClass) { //noinspection ConstantConditions @@ -637,7 +680,8 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } @Override - public Collection findConflicts(PsiElement element, PsiElement[] allElementsToDelete) { + @Nullable + public Collection findConflicts(@NotNull PsiElement element, @NotNull PsiElement[] allElementsToDelete) { if (element instanceof JetNamedFunction || element instanceof JetProperty) { JetClass jetClass = PsiTreeUtil.getParentOfType(element, JetClass.class); if (jetClass == null || jetClass.getBody() != element.getParent()) return null; @@ -678,7 +722,7 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { */ @Nullable @Override - public UsageInfo[] preprocessUsages(Project project, UsageInfo[] usages) { + public UsageInfo[] preprocessUsages(@NotNull Project project, @NotNull UsageInfo[] usages) { ArrayList result = new ArrayList(); ArrayList overridingMethodUsages = new ArrayList(); @@ -733,7 +777,8 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } } - private static PsiParameter getPsiParameter(JetParameter parameter) { + @Nullable + private static PsiParameter getPsiParameter(@NotNull JetParameter parameter) { JetNamedFunction function = PsiTreeUtil.getParentOfType(parameter, JetNamedFunction.class); if (function == null || parameter.getParent() != function.getValueParameterList()) return null; @@ -745,7 +790,7 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } @Override - public void prepareForDeletion(PsiElement element) throws IncorrectOperationException { + public void prepareForDeletion(@NotNull PsiElement element) throws IncorrectOperationException { if (element instanceof PsiMethod) { cleanUpOverrides((PsiMethod) element); } @@ -778,20 +823,20 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } } - public static void deleteElementAndCleanParent(PsiElement element) { + public static void deleteElementAndCleanParent(@NotNull PsiElement element) { PsiElement parent = element.getParent(); JetPsiUtil.deleteElementWithDelimiters(element); JetPsiUtil.deleteChildlessElement(parent, element.getClass()); } - private static boolean checkPsiMethodEquality(PsiMethod method1, PsiMethod method2) { + private static boolean checkPsiMethodEquality(@NotNull PsiMethod method1, @NotNull PsiMethod method2) { if (method1 instanceof JetClsMethod && method2 instanceof JetClsMethod) { return ((JetClsMethod) method1).getOrigin().equals(((JetClsMethod) method2).getOrigin()); } return method1.equals(method2); } - public static void cleanUpOverrides(PsiMethod method) { + public static void cleanUpOverrides(@NotNull PsiMethod method) { Collection superMethods = Arrays.asList(method.findSuperMethods(true)); Collection overridingMethods = OverridingMethodsSearch.search(method, true).findAll(); overrideLoop: @@ -843,11 +888,12 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return askUserForMethodsToSearch(declaration, callableDescriptor, superMethods, superClasses); } + @NotNull private static Collection askUserForMethodsToSearch( - JetDeclaration declaration, - CallableMemberDescriptor callableDescriptor, - Collection superMethods, - List superClasses + @NotNull JetDeclaration declaration, + @NotNull CallableMemberDescriptor callableDescriptor, + @NotNull Collection superMethods, + @NotNull List superClasses ) { String superClassesStr = "\n" + StringUtil.join(superClasses, ""); String message = JetBundle.message( @@ -870,7 +916,10 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } } - private static List getClassDescriptions(final BindingContext bindingContext, Collection superMethods) { + @NotNull + private static List getClassDescriptions( + @NotNull final BindingContext bindingContext, @NotNull Collection superMethods + ) { return ContainerUtil.map( superMethods, new Function() { @@ -906,13 +955,17 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { @Nullable @Override public Collection getElementsToSearch( - PsiElement element, @Nullable Module module, Collection allElementsToDelete + @NotNull PsiElement element, @Nullable Module module, @NotNull Collection allElementsToDelete ) { if (element instanceof JetParameter) { PsiParameter psiParameter = getPsiParameter((JetParameter) element); if (psiParameter != null) return checkParametersInMethodHierarchy(psiParameter); } + if (element instanceof PsiParameter) { + return checkParametersInMethodHierarchy((PsiParameter) element); + } + if (ApplicationManager.getApplication().isUnitTestMode()) { return Collections.singletonList(element); } @@ -924,7 +977,8 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return super.getElementsToSearch(element, module, allElementsToDelete); } - private static Collection checkParametersInMethodHierarchy(PsiParameter parameter) { + @Nullable + private static Collection checkParametersInMethodHierarchy(@NotNull PsiParameter parameter) { PsiMethod method = (PsiMethod)parameter.getDeclarationScope(); int parameterIndex = method.getParameterList().getParameterIndex(parameter); @@ -951,7 +1005,8 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } // TODO: generalize breadth-first search - private static Set collectParametersToDelete(PsiMethod method, int parameterIndex) { + @NotNull + private static Set collectParametersToDelete(@NotNull PsiMethod method, int parameterIndex) { Deque queue = new ArrayDeque(); Set visited = new HashSet(); Set parametersToDelete = new HashSet(); @@ -977,7 +1032,8 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return parametersToDelete; } - private static String formatJavaOrLightMethod(PsiMethod method) { + @NotNull + private static String formatJavaOrLightMethod(@NotNull PsiMethod method) { if (method instanceof JetClsMethod) { JetDeclaration declaration = ((JetClsMethod) method).getOrigin(); BindingContext bindingContext = @@ -989,7 +1045,7 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { return formatPsiMethod(method, false, false); } - private static void addParameter(PsiMethod method, Set result, int parameterIndex) { + private static void addParameter(@NotNull PsiMethod method, @NotNull Set result, int parameterIndex) { if (method instanceof JetClsMethod) { JetDeclaration declaration = ((JetClsMethod) method).getOrigin(); if (declaration instanceof JetNamedFunction) { @@ -1001,9 +1057,10 @@ public class KotlinSafeDeleteProcessor extends JavaSafeDeleteProcessor { } } + @Nullable @Override public Collection getAdditionalElementsToDelete( - PsiElement element, Collection allElementsToDelete, boolean askUser + @NotNull PsiElement element, @NotNull Collection allElementsToDelete, boolean askUser ) { if (element instanceof JetObjectDeclarationName) { return Arrays.asList(getObjectDeclarationOrFail(element));