From 6218c0e804c71f0b35d9e3bfb8315bdd837550e2 Mon Sep 17 00:00:00 2001 From: UNV Date: Fri, 23 May 2025 11:10:39 +0300 Subject: [PATCH] Refactoring of BaseRefactoringProcessor descendants (part 4). --- ...eplaceConstructorWithFactoryProcessor.java | 63 +-- .../safeDelete/JavaSafeDeleteProcessor.java | 212 +++++---- .../TurnRefsToSuperProcessor.java | 62 ++- .../TurnRefsToSuperProcessorBase.java | 424 ++++++++---------- .../FixableUsagesRefactoringProcessor.java | 10 +- 5 files changed, 378 insertions(+), 393 deletions(-) diff --git a/plugin/src/main/java/com/intellij/java/impl/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryProcessor.java b/plugin/src/main/java/com/intellij/java/impl/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryProcessor.java index 54931c19c5..706c648c43 100644 --- a/plugin/src/main/java/com/intellij/java/impl/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryProcessor.java +++ b/plugin/src/main/java/com/intellij/java/impl/refactoring/replaceConstructorWithFactory/ReplaceConstructorWithFactoryProcessor.java @@ -19,6 +19,8 @@ import com.intellij.java.language.psi.*; import com.intellij.java.language.psi.util.PsiUtil; import com.intellij.java.language.util.VisibilityUtil; +import consulo.annotation.access.RequiredReadAction; +import consulo.annotation.access.RequiredWriteAction; import consulo.language.codeStyle.CodeStyleManager; import consulo.language.editor.refactoring.BaseRefactoringProcessor; import consulo.language.editor.refactoring.localize.RefactoringLocalize; @@ -40,12 +42,8 @@ import consulo.util.collection.MultiMap; import consulo.util.lang.ref.SimpleReference; import jakarta.annotation.Nonnull; -import org.jetbrains.annotations.NonNls; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; +import java.util.*; /** * @author dsl @@ -65,7 +63,7 @@ public ReplaceConstructorWithFactoryProcessor( PsiMethod originalConstructor, PsiClass originalClass, PsiClass targetClass, - @NonNls String factoryName + String factoryName ) { super(project); myOriginalClass = originalClass; @@ -79,7 +77,7 @@ public ReplaceConstructorWithFactoryProcessor( } private boolean isInner(PsiClass originalClass) { - final boolean result = PsiUtil.isInnerClass(originalClass); + boolean result = PsiUtil.isInnerClass(originalClass); if (result) { LOG.assertTrue(PsiTreeUtil.isAncestor(myTargetClass, originalClass, false)); } @@ -87,7 +85,8 @@ private boolean isInner(PsiClass originalClass) { } @Nonnull - protected UsageViewDescriptor createUsageViewDescriptor(UsageInfo[] usages) { + @Override + protected UsageViewDescriptor createUsageViewDescriptor(@Nonnull UsageInfo[] usages) { if (myConstructor != null) { return new ReplaceConstructorWithFactoryViewDescriptor(myConstructor); } @@ -99,11 +98,13 @@ protected UsageViewDescriptor createUsageViewDescriptor(UsageInfo[] usages) { private List myNonNewConstructorUsages; @Nonnull + @Override + @RequiredReadAction protected UsageInfo[] findUsages() { GlobalSearchScope projectScope = GlobalSearchScope.projectScope(myProject); - ArrayList usages = new ArrayList(); - myNonNewConstructorUsages = new ArrayList(); + ArrayList usages = new ArrayList<>(); + myNonNewConstructorUsages = new ArrayList<>(); for (PsiReference reference : ReferencesSearch.search( myConstructor == null ? myOriginalClass : myConstructor, @@ -112,13 +113,13 @@ protected UsageInfo[] findUsages() { )) { PsiElement element = reference.getElement(); - if (element.getParent() instanceof PsiNewExpression) { - usages.add(new UsageInfo(element.getParent())); + if (element.getParent() instanceof PsiNewExpression newExpr) { + usages.add(new UsageInfo(newExpr)); } else if ("super".equals(element.getText()) || "this".equals(element.getText())) { myNonNewConstructorUsages.add(element); } - else if (element instanceof PsiMethod && ((PsiMethod)element).isConstructor()) { + else if (element instanceof PsiMethod method && method.isConstructor()) { myNonNewConstructorUsages.add(element); } else if (element instanceof PsiClass) { @@ -146,9 +147,9 @@ else if (element instanceof PsiClass) { protected boolean preprocessUsages(@Nonnull SimpleReference refUsages) { UsageInfo[] usages = refUsages.get(); - MultiMap conflicts = new MultiMap(); - final PsiResolveHelper helper = JavaPsiFacade.getInstance(myProject).getResolveHelper(); - final PsiClass constructorContainingClass = getConstructorContainingClass(); + MultiMap conflicts = new MultiMap<>(); + PsiResolveHelper helper = JavaPsiFacade.getInstance(myProject).getResolveHelper(); + PsiClass constructorContainingClass = getConstructorContainingClass(); if (!helper.isAccessible(constructorContainingClass, myTargetClass, null)) { LocalizeValue message = RefactoringLocalize.class0IsNotAccessibleFromTarget1( RefactoringUIUtil.getDescription(constructorContainingClass, true), @@ -157,10 +158,10 @@ protected boolean preprocessUsages(@Nonnull SimpleReference refUsag conflicts.putValue(constructorContainingClass, message.get()); } - HashSet reportedContainers = new HashSet(); - final String targetClassDescription = RefactoringUIUtil.getDescription(myTargetClass, true); + Set reportedContainers = new HashSet<>(); + String targetClassDescription = RefactoringUIUtil.getDescription(myTargetClass, true); for (UsageInfo usage : usages) { - final PsiElement container = ConflictsUtil.getContainer(usage.getElement()); + PsiElement container = ConflictsUtil.getContainer(usage.getElement()); if (!reportedContainers.contains(container)) { reportedContainers.add(container); if (!helper.isAccessible(myTargetClass, usage.getElement(), null)) { @@ -175,9 +176,9 @@ protected boolean preprocessUsages(@Nonnull SimpleReference refUsag if (myIsInner) { for (UsageInfo usage : usages) { - final PsiField field = PsiTreeUtil.getParentOfType(usage.getElement(), PsiField.class); + PsiField field = PsiTreeUtil.getParentOfType(usage.getElement(), PsiField.class); if (field != null) { - final PsiClass containingClass = field.getContainingClass(); + PsiClass containingClass = field.getContainingClass(); if (PsiTreeUtil.isAncestor(containingClass, myTargetClass, true)) { LocalizeValue message = RefactoringLocalize.constructorBeingRefactoredIsUsedInInitializerOf0( @@ -203,8 +204,9 @@ private PsiClass getConstructorContainingClass() { } } - protected void performRefactoring(UsageInfo[] usages) { - + @Override + @RequiredWriteAction + protected void performRefactoring(@Nonnull UsageInfo[] usages) { try { PsiReferenceExpression classReferenceExpression = myFactory.createReferenceExpression(myTargetClass); @@ -263,16 +265,17 @@ protected void performRefactoring(UsageInfo[] usages) { } } + @RequiredReadAction private PsiMethod createFactoryMethod() throws IncorrectOperationException { - final PsiClass containingClass = getConstructorContainingClass(); + PsiClass containingClass = getConstructorContainingClass(); PsiClassType type = myFactory.createType(containingClass, PsiSubstitutor.EMPTY); - final PsiMethod factoryMethod = myFactory.createMethod(myFactoryName, type); + PsiMethod factoryMethod = myFactory.createMethod(myFactoryName, type); if (myConstructor != null) { factoryMethod.getParameterList().replace(myConstructor.getParameterList()); factoryMethod.getThrowsList().replace(myConstructor.getThrowsList()); } - Collection names = new HashSet(); + Collection names = new HashSet<>(); for (PsiTypeParameter typeParameter : PsiUtil.typeParametersIterable(myConstructor != null ? myConstructor : containingClass)) { if (!names.contains(typeParameter.getName())) { //Otherwise type parameter is hidden in the constructor names.add(typeParameter.getName()); @@ -285,7 +288,7 @@ private PsiMethod createFactoryMethod() throws IncorrectOperationException { PsiNewExpression newExpression = (PsiNewExpression)returnStatement.getReturnValue(); PsiJavaCodeReferenceElement classRef = myFactory.createReferenceElementByType(type); newExpression.getClassReference().replace(classRef); - final PsiExpressionList argumentList = newExpression.getArgumentList(); + PsiExpressionList argumentList = newExpression.getArgumentList(); PsiParameter[] params = factoryMethod.getParameterList().getParameters(); @@ -306,7 +309,7 @@ private PsiMethod createFactoryMethod() throws IncorrectOperationException { @PsiModifier.ModifierConstant private String getDefaultFactoryVisibility() { - final PsiModifierList modifierList; + PsiModifierList modifierList; if (myConstructor != null) { modifierList = myConstructor.getModifierList(); } @@ -316,7 +319,9 @@ private String getDefaultFactoryVisibility() { return VisibilityUtil.getVisibilityModifier(modifierList); } - + @Nonnull + @Override + @RequiredReadAction protected String getCommandName() { if (myConstructor != null) { return RefactoringLocalize.replaceConstructor0WithAFactoryMethod(DescriptiveNameUtil.getDescriptiveName(myConstructor)).get(); diff --git a/plugin/src/main/java/com/intellij/java/impl/refactoring/safeDelete/JavaSafeDeleteProcessor.java b/plugin/src/main/java/com/intellij/java/impl/refactoring/safeDelete/JavaSafeDeleteProcessor.java index 07dfd06d28..150504b434 100644 --- a/plugin/src/main/java/com/intellij/java/impl/refactoring/safeDelete/JavaSafeDeleteProcessor.java +++ b/plugin/src/main/java/com/intellij/java/impl/refactoring/safeDelete/JavaSafeDeleteProcessor.java @@ -59,24 +59,27 @@ import jakarta.annotation.Nullable; import java.util.*; +import java.util.function.Predicate; @ExtensionImpl(id = "javaProcessor") public class JavaSafeDeleteProcessor extends SafeDeleteProcessorDelegateBase { private static final Logger LOG = Logger.getInstance(JavaSafeDeleteProcessor.class); - public boolean handlesElement(final PsiElement element) { + @Override + public boolean handlesElement(PsiElement element) { return element instanceof PsiClass || element instanceof PsiMethod || element instanceof PsiField || element instanceof PsiParameter || element instanceof PsiLocalVariable || element instanceof PsiPackage; } @Nullable + @Override @RequiredUIAccess public NonCodeUsageSearchInfo findUsages( - final PsiElement element, - final PsiElement[] allElementsToDelete, - final List usages + PsiElement element, + PsiElement[] allElementsToDelete, + List usages ) { - Condition insideDeletedCondition = getUsageInsideDeletedFilter(allElementsToDelete); + Predicate insideDeletedCondition = getUsageInsideDeletedFilter(allElementsToDelete); if (element instanceof PsiClass psiClass) { findClassUsages(psiClass, allElementsToDelete, usages); if (element instanceof PsiTypeParameter typeParameter) { @@ -96,7 +99,7 @@ else if (element instanceof PsiParameter parameter) { else if (element instanceof PsiLocalVariable localVariable) { for (PsiReference reference : ReferencesSearch.search(element)) { PsiReferenceExpression referencedElement = (PsiReferenceExpression)reference.getElement(); - final PsiStatement statement = PsiTreeUtil.getParentOfType(referencedElement, PsiStatement.class); + PsiStatement statement = PsiTreeUtil.getParentOfType(referencedElement, PsiStatement.class); boolean isSafeToDelete = PsiUtil.isAccessedForWriting(referencedElement); boolean hasSideEffects = false; @@ -123,14 +126,14 @@ public Collection getElementsToSearch( ) { Project project = element.getProject(); if (element instanceof PsiPackage psiPackage && module != null) { - final PsiDirectory[] directories = psiPackage.getDirectories(GlobalSearchScope.moduleScope(module)); + PsiDirectory[] directories = psiPackage.getDirectories(GlobalSearchScope.moduleScope(module)); if (directories.length == 0) { return null; } return Arrays.asList(directories); } else if (element instanceof PsiMethod method) { - final PsiMethod[] methods = SuperMethodWarningUtil.checkSuperMethods( + PsiMethod[] methods = SuperMethodWarningUtil.checkSuperMethods( method, RefactoringLocalize.toDeleteWithUsageSearch().get(), allElementsToDelete @@ -138,15 +141,15 @@ else if (element instanceof PsiMethod method) { if (methods.length == 0) { return null; } - final ArrayList psiMethods = new ArrayList<>(Arrays.asList(methods)); + ArrayList psiMethods = new ArrayList<>(Arrays.asList(methods)); psiMethods.add(method); return psiMethods; } else if (element instanceof PsiParameter parameter && parameter.getDeclarationScope() instanceof PsiMethod method) { - final Set parametersToDelete = new HashSet<>(); + Set parametersToDelete = new HashSet<>(); parametersToDelete.add(parameter); - final int parameterIndex = method.getParameterList().getParameterIndex((PsiParameter)element); - final List superMethods = new ArrayList<>(Arrays.asList(method.findDeepestSuperMethods())); + int parameterIndex = method.getParameterList().getParameterIndex(parameter); + List superMethods = new ArrayList<>(Arrays.asList(method.findDeepestSuperMethods())); if (superMethods.isEmpty()) { superMethods.add(method); } @@ -178,9 +181,10 @@ else if (element instanceof PsiParameter parameter && parameter.getDeclarationSc } @Override + @RequiredReadAction public UsageView showUsages(UsageInfo[] usages, UsageViewPresentation presentation, UsageViewManager manager, PsiElement[] elements) { - final List overridingMethods = new ArrayList<>(); - final List others = new ArrayList<>(); + List overridingMethods = new ArrayList<>(); + List others = new ArrayList<>(); for (UsageInfo usage : usages) { if (usage instanceof SafeDeleteOverridingMethodUsageInfo safeDeleteOverridingMethodUsageInfo) { overridingMethods.add(safeDeleteOverridingMethodUsageInfo.getOverridingMethod()); @@ -210,22 +214,23 @@ public UsageView showUsages(UsageInfo[] usages, UsageViewPresentation presentati ); } + @Override @RequiredUIAccess public Collection getAdditionalElementsToDelete( - final PsiElement element, - final Collection allElementsToDelete, - final boolean askUser + PsiElement element, + Collection allElementsToDelete, + boolean askUser ) { if (element instanceof PsiField field) { - final Project project = element.getProject(); + Project project = element.getProject(); String propertyName = JavaCodeStyleManager.getInstance(project).variableNameToPropertyName(field.getName(), VariableKind.FIELD); PsiClass aClass = field.getContainingClass(); if (aClass != null) { - boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC); + boolean isStatic = field.isStatic(); PsiMethod[] getters = GetterSetterPrototypeProvider.findGetters(aClass, propertyName, isStatic); if (getters != null) { - final List validGetters = new ArrayList<>(1); + List validGetters = new ArrayList<>(1); for (PsiMethod getter : getters) { if (!allElementsToDelete.contains(getter) && (getter != null && getter.isPhysical())) { validGetters.add(getter); @@ -239,13 +244,12 @@ public Collection getAdditionalElementsToDelete( setter = null; } if (askUser && (getters != null || setter != null)) { - final String message = - RefactoringMessageUtil.getGetterSetterMessage( - field.getName(), - RefactoringLocalize.deleteTitle().get(), - getters != null ? getters[0] : null, - setter - ); + String message = RefactoringMessageUtil.getGetterSetterMessage( + field.getName(), + RefactoringLocalize.deleteTitle().get(), + getters != null ? getters[0] : null, + setter + ); if (!project.getApplication().isUnitTestMode() && Messages.showYesNoDialog( project, @@ -270,17 +274,19 @@ public Collection getAdditionalElementsToDelete( return null; } - public Collection findConflicts(final PsiElement element, final PsiElement[] allElementsToDelete) { + @Override + @RequiredReadAction + public Collection findConflicts(PsiElement element, PsiElement[] allElementsToDelete) { if (element instanceof PsiMethod method) { - final PsiClass containingClass = method.getContainingClass(); + PsiClass containingClass = method.getContainingClass(); - if (!containingClass.hasModifierProperty(PsiModifier.ABSTRACT)) { - final PsiMethod[] superMethods = method.findSuperMethods(); + if (!containingClass.isAbstract()) { + PsiMethod[] superMethods = method.findSuperMethods(); for (PsiMethod superMethod : superMethods) { if (isInside(superMethod, allElementsToDelete)) { continue; } - if (superMethod.hasModifierProperty(PsiModifier.ABSTRACT)) { + if (superMethod.isAbstract()) { LocalizeValue message = RefactoringLocalize.zeroImplements1( RefactoringUIUtil.getDescription(element, true), RefactoringUIUtil.getDescription(superMethod, true) @@ -294,8 +300,9 @@ public Collection findConflicts(final PsiElement element, final PsiEleme } @Nullable + @Override @RequiredUIAccess - public UsageInfo[] preprocessUsages(final Project project, final UsageInfo[] usages) { + public UsageInfo[] preprocessUsages(Project project, UsageInfo[] usages) { ArrayList result = new ArrayList<>(); ArrayList overridingMethods = new ArrayList<>(); for (UsageInfo usage : usages) { @@ -327,7 +334,8 @@ else if (usage instanceof SafeDeleteOverridingMethodUsageInfo) { return result.toArray(new UsageInfo[result.size()]); } - public void prepareForDeletion(final PsiElement element) throws IncorrectOperationException { + @Override + public void prepareForDeletion(PsiElement element) throws IncorrectOperationException { if (element instanceof PsiVariable variable) { variable.normalizeDeclaration(); } @@ -399,15 +407,16 @@ else if (element instanceof PsiPackage) { } } - public static Condition getUsageInsideDeletedFilter(final PsiElement[] allElementsToDelete) { + public static Condition getUsageInsideDeletedFilter(PsiElement[] allElementsToDelete) { return usage -> !(usage instanceof PsiFile) && isInside(usage, allElementsToDelete); } - private static void findClassUsages(final PsiClass psiClass, final PsiElement[] allElementsToDelete, final List usages) { - final boolean justPrivates = containsOnlyPrivates(psiClass); + @RequiredReadAction + private static void findClassUsages(PsiClass psiClass, PsiElement[] allElementsToDelete, List usages) { + boolean justPrivates = containsOnlyPrivates(psiClass); ReferencesSearch.search(psiClass).forEach(reference -> { - final PsiElement element = reference.getElement(); + PsiElement element = reference.getElement(); if (!isInside(element, allElementsToDelete)) { PsiElement parent = element.getParent(); @@ -430,26 +439,26 @@ private static boolean isInNonStaticImport(PsiElement element) { } @RequiredReadAction - private static boolean containsOnlyPrivates(final PsiClass aClass) { - final PsiField[] fields = aClass.getFields(); + private static boolean containsOnlyPrivates(PsiClass aClass) { + PsiField[] fields = aClass.getFields(); for (PsiField field : fields) { - if (!field.hasModifierProperty(PsiModifier.PRIVATE)) { + if (!field.isPrivate()) { return false; } } - final PsiMethod[] methods = aClass.getMethods(); + PsiMethod[] methods = aClass.getMethods(); for (PsiMethod method : methods) { - if (!method.hasModifierProperty(PsiModifier.PRIVATE)) { + if (!method.isPrivate()) { if (method.isConstructor()) { //skip non-private constructors with call to super only - final PsiCodeBlock body = method.getBody(); + PsiCodeBlock body = method.getBody(); if (body != null) { - final PsiStatement[] statements = body.getStatements(); + PsiStatement[] statements = body.getStatements(); if (statements.length == 0) { continue; } if (statements.length == 1 && statements[0] instanceof PsiExpressionStatement expressionStatement) { - final PsiExpression expression = expressionStatement.getExpression(); + PsiExpression expression = expressionStatement.getExpression(); if (expression instanceof PsiMethodCallExpression methodCallExpression) { PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); if (methodExpression.getText().equals(PsiKeyword.SUPER)) { @@ -463,9 +472,9 @@ private static boolean containsOnlyPrivates(final PsiClass aClass) { } } - final PsiClass[] inners = aClass.getInnerClasses(); + PsiClass[] inners = aClass.getInnerClasses(); for (PsiClass inner : inners) { - if (!inner.hasModifierProperty(PsiModifier.PRIVATE)) { + if (!inner.isPrivate()) { return false; } } @@ -473,17 +482,17 @@ private static boolean containsOnlyPrivates(final PsiClass aClass) { return true; } - private static void findTypeParameterExternalUsages(final PsiTypeParameter typeParameter, final Collection usages) { + private static void findTypeParameterExternalUsages(PsiTypeParameter typeParameter, Collection usages) { PsiTypeParameterListOwner owner = typeParameter.getOwner(); if (owner != null) { - final PsiTypeParameterList parameterList = owner.getTypeParameterList(); + PsiTypeParameterList parameterList = owner.getTypeParameterList(); if (parameterList != null) { - final int paramsCount = parameterList.getTypeParameters().length; - final int index = parameterList.getTypeParameterIndex(typeParameter); + int paramsCount = parameterList.getTypeParameters().length; + int index = parameterList.getTypeParameterIndex(typeParameter); ReferencesSearch.search(owner).forEach(reference -> { if (reference instanceof PsiJavaCodeReferenceElement javaCodeReferenceElement) { - final PsiReferenceParameterList parameterList1 = javaCodeReferenceElement.getParameterList(); + PsiReferenceParameterList parameterList1 = javaCodeReferenceElement.getParameterList(); if (parameterList1 != null) { PsiTypeElement[] typeArgs = parameterList1.getTypeParameterElements(); if (typeArgs.length > index) { @@ -502,25 +511,25 @@ private static void findTypeParameterExternalUsages(final PsiTypeParameter typeP @Nullable @RequiredReadAction - private static Condition findMethodUsages( + private static Predicate findMethodUsages( PsiMethod psiMethod, - final PsiElement[] allElementsToDelete, + PsiElement[] allElementsToDelete, List usages ) { - final Collection references = ReferencesSearch.search(psiMethod).findAll(); + Collection references = ReferencesSearch.search(psiMethod).findAll(); if (psiMethod.isConstructor()) { return findConstructorUsages(psiMethod, references, usages, allElementsToDelete); } - final PsiMethod[] overridingMethods = + PsiMethod[] overridingMethods = removeDeletedMethods(OverridingMethodsSearch.search(psiMethod, true).toArray(PsiMethod.EMPTY_ARRAY), allElementsToDelete); - final HashMap> methodToReferences = new HashMap<>(); + Map> methodToReferences = new HashMap<>(); for (PsiMethod overridingMethod : overridingMethods) { - final Collection overridingReferences = ReferencesSearch.search(overridingMethod).findAll(); + Collection overridingReferences = ReferencesSearch.search(overridingMethod).findAll(); methodToReferences.put(overridingMethod, overridingReferences); } - final Set validOverriding = validateOverridingMethods( + Set validOverriding = validateOverridingMethods( psiMethod, references, Arrays.asList(overridingMethods), @@ -529,7 +538,7 @@ private static Condition findMethodUsages( allElementsToDelete ); for (PsiReference reference : references) { - final PsiElement element = reference.getElement(); + PsiElement element = reference.getElement(); if (!isInside(element, allElementsToDelete) && !isInside(element, validOverriding)) { usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo( element, @@ -541,7 +550,7 @@ private static Condition findMethodUsages( return usage -> !(usage instanceof PsiFile) && (isInside(usage, allElementsToDelete) || isInside(usage, validOverriding)); } - private static PsiMethod[] removeDeletedMethods(PsiMethod[] methods, final PsiElement[] allElementsToDelete) { + private static PsiMethod[] removeDeletedMethods(PsiMethod[] methods, PsiElement[] allElementsToDelete) { ArrayList list = new ArrayList<>(); for (PsiMethod method : methods) { boolean isDeleted = false; @@ -560,9 +569,11 @@ private static PsiMethod[] removeDeletedMethods(PsiMethod[] methods, final PsiEl @Nullable @RequiredReadAction - private static Condition findConstructorUsages( - PsiMethod constructor, Collection originalReferences, List usages, - final PsiElement[] allElementsToDelete + private static Predicate findConstructorUsages( + PsiMethod constructor, + Collection originalReferences, + List usages, + PsiElement[] allElementsToDelete ) { HashMap> constructorsToRefs = new HashMap<>(); HashSet newConstructors = new HashSet<>(); @@ -576,7 +587,7 @@ private static Condition findConstructorUsages( do { passConstructors.clear(); for (PsiMethod method : newConstructors) { - final Collection references = constructorsToRefs.get(method); + Collection references = constructorsToRefs.get(method); for (PsiReference reference : references) { PsiMethod overridingConstructor = getOverridingConstructorOfSuperCall(reference.getElement()); if (overridingConstructor != null && !constructorsToRefs.containsKey(overridingConstructor)) { @@ -591,7 +602,7 @@ private static Condition findConstructorUsages( } while (!newConstructors.isEmpty()); - final Set validOverriding = validateOverridingMethods( + Set validOverriding = validateOverridingMethods( constructor, originalReferences, constructorsToRefs.keySet(), @@ -603,22 +614,22 @@ private static Condition findConstructorUsages( return usage -> !(usage instanceof PsiFile) && (isInside(usage, allElementsToDelete) || isInside(usage, validOverriding)); } - private static boolean isTheOnlyEmptyDefaultConstructor(final PsiMethod constructor) { + private static boolean isTheOnlyEmptyDefaultConstructor(PsiMethod constructor) { if (constructor.getParameterList().getParameters().length > 0) { return false; } - final PsiCodeBlock body = constructor.getBody(); + PsiCodeBlock body = constructor.getBody(); return !(body != null && body.getStatements().length > 0) && constructor.getContainingClass().getConstructors().length == 1; } @RequiredReadAction private static Set validateOverridingMethods( PsiMethod originalMethod, - final Collection originalReferences, + Collection originalReferences, Collection overridingMethods, - HashMap> methodToReferences, + Map> methodToReferences, List usages, - final PsiElement[] allElementsToDelete + PsiElement[] allElementsToDelete ) { Set validOverriding = new LinkedHashSet<>(overridingMethods); Set multipleInterfaceImplementations = new HashSet<>(); @@ -627,10 +638,10 @@ private static Set validateOverridingMethods( anyNewBadRefs = false; for (PsiMethod overridingMethod : overridingMethods) { if (validOverriding.contains(overridingMethod)) { - final Collection overridingReferences = methodToReferences.get(overridingMethod); + Collection overridingReferences = methodToReferences.get(overridingMethod); boolean anyOverridingRefs = false; - for (final PsiReference overridingReference : overridingReferences) { - final PsiElement element = overridingReference.getElement(); + for (PsiReference overridingReference : overridingReferences) { + PsiElement element = overridingReference.getElement(); if (!isInside(element, allElementsToDelete) && !isInside(element, validOverriding)) { anyOverridingRefs = true; break; @@ -646,7 +657,7 @@ private static Set validateOverridingMethods( anyNewBadRefs = true; for (PsiReference reference : originalReferences) { - final PsiElement element = reference.getElement(); + PsiElement element = reference.getElement(); if (!isInside(element, allElementsToDelete) && !isInside(element, overridingMethods)) { usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(element, originalMethod, false)); validOverriding.clear(); @@ -679,11 +690,11 @@ private static Set validateOverridingMethods( } private static boolean isMultipleInterfacesImplementation( - final PsiMethod method, + PsiMethod method, PsiMethod originalMethod, - final PsiElement[] allElementsToDelete + PsiElement[] allElementsToDelete ) { - final PsiMethod[] methods = method.findSuperMethods(); + PsiMethod[] methods = method.findSuperMethods(); for (PsiMethod superMethod : methods) { if (ArrayUtil.find(allElementsToDelete, superMethod) < 0 && !MethodSignatureUtil.isSuperMethod(originalMethod, superMethod)) { return true; @@ -694,7 +705,7 @@ private static boolean isMultipleInterfacesImplementation( @Nullable @RequiredReadAction - private static PsiMethod getOverridingConstructorOfSuperCall(final PsiElement element) { + private static PsiMethod getOverridingConstructorOfSuperCall(PsiElement element) { if (element instanceof PsiReferenceExpression refExpr && "super".equals(element.getText()) && refExpr.getParent() instanceof PsiMethodCallExpression methodCall && methodCall.getParent() instanceof PsiExpressionStatement expr @@ -711,19 +722,19 @@ private static boolean canBePrivate( PsiMethod method, Collection references, Collection deleted, - final PsiElement[] allElementsToDelete + PsiElement[] allElementsToDelete ) { - final PsiClass containingClass = method.getContainingClass(); + PsiClass containingClass = method.getContainingClass(); if (containingClass == null) { return false; } PsiManager manager = method.getManager(); - final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject()); - final PsiElementFactory factory = facade.getElementFactory(); - final PsiModifierList privateModifierList; + JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject()); + PsiElementFactory factory = facade.getElementFactory(); + PsiModifierList privateModifierList; try { - final PsiMethod newMethod = factory.createMethod("x3", PsiType.VOID); + PsiMethod newMethod = factory.createMethod("x3", PsiType.VOID); privateModifierList = newMethod.getModifierList(); privateModifierList.setModifierProperty(PsiModifier.PRIVATE, true); } @@ -732,7 +743,7 @@ private static boolean canBePrivate( return false; } for (PsiReference reference : references) { - final PsiElement element = reference.getElement(); + PsiElement element = reference.getElement(); if (!isInside(element, allElementsToDelete) && !isInside(element, deleted) && !facade.getResolveHelper().isAccessible(method, privateModifierList, element, null, null)) { return false; @@ -741,16 +752,16 @@ private static boolean canBePrivate( return true; } - private static Condition findFieldUsages( - final PsiField psiField, - final List usages, - final PsiElement[] allElementsToDelete + private static Predicate findFieldUsages( + PsiField psiField, + List usages, + PsiElement[] allElementsToDelete ) { - final Condition isInsideDeleted = getUsageInsideDeletedFilter(allElementsToDelete); + Predicate isInsideDeleted = getUsageInsideDeletedFilter(allElementsToDelete); ReferencesSearch.search(psiField).forEach(reference -> { - if (!isInsideDeleted.value(reference.getElement())) { - final PsiElement element = reference.getElement(); - final PsiElement parent = element.getParent(); + if (!isInsideDeleted.test(reference.getElement())) { + PsiElement element = reference.getElement(); + PsiElement parent = element.getParent(); if (parent instanceof PsiAssignmentExpression assignment && element == assignment.getLExpression()) { usages.add(new SafeDeleteFieldWriteReference(assignment, psiField)); } @@ -773,8 +784,8 @@ private static Condition findFieldUsages( return isInsideDeleted; } - private static void findParameterUsages(final PsiParameter parameter, final List usages) { - final PsiMethod method = (PsiMethod)parameter.getDeclarationScope(); + private static void findParameterUsages(PsiParameter parameter, List usages) { + PsiMethod method = (PsiMethod)parameter.getDeclarationScope(); //search for refs to current method only, do not search for refs to overriding methods, they'll be searched separately ReferencesSearch.search(method).forEach(reference -> { PsiElement element = reference.getElement(); @@ -786,7 +797,7 @@ private static void findParameterUsages(final PsiParameter parameter, final List ReferencesSearch.search(parameter).forEach(reference -> { PsiElement element = reference.getElement(); - final PsiDocTag docTag = PsiTreeUtil.getParentOfType(element, PsiDocTag.class); + PsiDocTag docTag = PsiTreeUtil.getParentOfType(element, PsiDocTag.class); if (docTag != null) { usages.add(new SafeDeleteReferenceJavaDeleteUsageInfo(docTag, parameter, true)); return true; @@ -799,7 +810,7 @@ private static void findParameterUsages(final PsiParameter parameter, final List isSafeDelete = true; } else if (methodExpression.getQualifierExpression() instanceof PsiSuperExpression) { - final PsiMethod superMethod = call.resolveMethod(); + PsiMethod superMethod = call.resolveMethod(); if (superMethod != null && MethodSignatureUtil.isSuperMethod(superMethod, method)) { isSafeDelete = true; } @@ -811,10 +822,12 @@ else if (methodExpression.getQualifierExpression() instanceof PsiSuperExpression }); } + @RequiredReadAction private static boolean isInside(PsiElement place, PsiElement[] ancestors) { return isInside(place, Arrays.asList(ancestors)); } + @RequiredReadAction private static boolean isInside(PsiElement place, Collection ancestors) { for (PsiElement element : ancestors) { if (isInside(place, element)) { @@ -824,6 +837,7 @@ private static boolean isInside(PsiElement place, Collection result = detectTurnToSuperRefs(refs, new ArrayList()); + List result = detectTurnToSuperRefs(refs, new ArrayList<>()); - final UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]); + UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]); return UsageViewUtil.removeDuplicatedUsages(usageInfos); } - protected void refreshElements(final PsiElement[] elements) { + @Override + protected void refreshElements(PsiElement[] elements) { LOG.assertTrue(elements.length == 2 && elements[0] instanceof PsiClass && elements[1] instanceof PsiClass); setClasses((PsiClass)elements[0], (PsiClass)elements[1]); } @@ -94,23 +104,25 @@ protected void refreshElements(final PsiElement[] elements) { @Override @RequiredUIAccess protected boolean preprocessUsages(@Nonnull SimpleReference refUsages) { - if (!ApplicationManager.getApplication().isUnitTestMode() && refUsages.get().length == 0) { - String message = RefactoringLocalize.noUsagesCanBeReplaced(myClass.getQualifiedName(), mySuper.getQualifiedName()).get(); - Messages.showInfoMessage(myProject, message, TurnRefsToSuperHandler.REFACTORING_NAME); + if (!myProject.getApplication().isUnitTestMode() && refUsages.get().length == 0) { + LocalizeValue message = RefactoringLocalize.noUsagesCanBeReplaced(myClass.getQualifiedName(), mySuper.getQualifiedName()); + Messages.showInfoMessage(myProject, message.get(), TurnRefsToSuperHandler.REFACTORING_NAME); return false; } return super.preprocessUsages(refUsages); } - protected boolean canTurnToSuper(final PsiElement refElement) { - return super.canTurnToSuper(refElement) && - JavaPsiFacade.getInstance(myProject).getResolveHelper().isAccessible(mySuper, refElement, null); + @Override + protected boolean canTurnToSuper(PsiElement refElement) { + return super.canTurnToSuper(refElement) + && JavaPsiFacade.getInstance(myProject).getResolveHelper().isAccessible(mySuper, refElement, null); } - protected void performRefactoring(UsageInfo[] usages) { + @Override + protected void performRefactoring(@Nonnull UsageInfo[] usages) { try { - final PsiClass aSuper = mySuper; + PsiClass aSuper = mySuper; processTurnToSuperRefs(usages, aSuper); } @@ -121,17 +133,19 @@ protected void performRefactoring(UsageInfo[] usages) { performVariablesRenaming(); } + @Override + @RequiredReadAction protected boolean isInSuper(PsiElement member) { - if (!(member instanceof PsiMember)) { + if (!(member instanceof PsiMember psiMember)) { return false; } - final PsiManager manager = member.getManager(); - if (InheritanceUtil.isInheritorOrSelf(mySuper, ((PsiMember)member).getContainingClass(), true)) { + PsiManager manager = member.getManager(); + if (InheritanceUtil.isInheritorOrSelf(mySuper, psiMember.getContainingClass(), true)) { return true; } - if (member instanceof PsiField) { - final PsiClass containingClass = ((PsiField)member).getContainingClass(); + if (member instanceof PsiField field) { + PsiClass containingClass = field.getContainingClass(); LanguageLevel languageLevel = PsiUtil.getLanguageLevel(member); if (manager.areElementsEquivalent( containingClass, @@ -140,13 +154,14 @@ protected boolean isInSuper(PsiElement member) { return true; } } - else if (member instanceof PsiMethod) { - return mySuper.findMethodBySignature((PsiMethod)member, true) != null; + else if (member instanceof PsiMethod method) { + return mySuper.findMethodBySignature(method, true) != null; } return false; } + @Override protected boolean isSuperInheritor(PsiClass aClass) { return InheritanceUtil.isInheritorOrSelf(mySuper, aClass, true); } @@ -164,7 +179,8 @@ public boolean isReplaceInstanceOf() { } @Nonnull - protected Collection getElementsToWrite(@Nonnull final UsageViewDescriptor descriptor) { + @Override + protected Collection getElementsToWrite(@Nonnull UsageViewDescriptor descriptor) { return Collections.emptyList(); // neither myClass nor mySuper are subject to change, it's just references that are going to change } } \ No newline at end of file diff --git a/plugin/src/main/java/com/intellij/java/impl/refactoring/turnRefsToSuper/TurnRefsToSuperProcessorBase.java b/plugin/src/main/java/com/intellij/java/impl/refactoring/turnRefsToSuper/TurnRefsToSuperProcessorBase.java index b930c46ec6..f841e3ae3b 100644 --- a/plugin/src/main/java/com/intellij/java/impl/refactoring/turnRefsToSuper/TurnRefsToSuperProcessorBase.java +++ b/plugin/src/main/java/com/intellij/java/impl/refactoring/turnRefsToSuper/TurnRefsToSuperProcessorBase.java @@ -30,9 +30,9 @@ import com.intellij.java.language.psi.util.MethodSignatureUtil; import com.intellij.java.language.psi.util.PsiUtil; import com.intellij.java.language.psi.util.TypeConversionUtil; -import consulo.application.ApplicationManager; +import consulo.annotation.access.RequiredReadAction; +import consulo.annotation.access.RequiredWriteAction; import consulo.application.progress.ProgressManager; -import consulo.application.util.function.Processor; import consulo.language.editor.refactoring.BaseRefactoringProcessor; import consulo.language.editor.refactoring.localize.RefactoringLocalize; import consulo.language.editor.refactoring.rename.AutomaticRenamingDialog; @@ -51,7 +51,6 @@ import consulo.util.lang.ref.SimpleReference; import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; -import org.jetbrains.annotations.NonNls; import java.util.*; @@ -64,18 +63,18 @@ public abstract class TurnRefsToSuperProcessorBase extends BaseRefactoringProces protected final boolean myReplaceInstanceOf; protected PsiManager myManager; protected PsiSearchHelper mySearchHelper; - protected HashSet myMarkedNodes = new HashSet(); + protected Set myMarkedNodes = new HashSet<>(); private Queue myExpressionsQueue; - protected HashMap myElementToNode = new HashMap(); - protected Map myVariablesRenames = new HashMap(); + protected Map myElementToNode = new HashMap<>(); + protected Map myVariablesRenames = new HashMap<>(); private final String mySuperClassName; - private final List myVariablesUsages = new ArrayList(); + private final List myVariablesUsages = new ArrayList<>(); @Override @RequiredUIAccess protected boolean preprocessUsages(@Nonnull SimpleReference refUsages) { UsageInfo[] usages = refUsages.get(); - List filtered = new ArrayList(); + List filtered = new ArrayList<>(); for (UsageInfo usage : usages) { if (usage instanceof TurnToSuperReferenceUsageInfo) { filtered.add(usage); @@ -83,26 +82,22 @@ protected boolean preprocessUsages(@Nonnull SimpleReference refUsag } myVariableRenamer = new AutomaticVariableRenamer(myClass, mySuperClassName, filtered); - if (!ApplicationManager.getApplication().isUnitTestMode() && - myVariableRenamer.hasAnythingToRename()) { - final AutomaticRenamingDialog dialog = new AutomaticRenamingDialog(myProject, myVariableRenamer); + if (!myProject.getApplication().isUnitTestMode() && myVariableRenamer.hasAnythingToRename()) { + AutomaticRenamingDialog dialog = new AutomaticRenamingDialog(myProject, myVariableRenamer); dialog.show(); if (!dialog.isOK()) { return false; } - final List variables = myVariableRenamer.getElements(); - for (final PsiNamedElement namedElement : variables) { - final PsiVariable variable = (PsiVariable)namedElement; - final SmartPsiElementPointer pointer = SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(variable); + List variables = myVariableRenamer.getElements(); + for (PsiNamedElement namedElement : variables) { + PsiVariable variable = (PsiVariable)namedElement; + SmartPsiElementPointer pointer = SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(variable); myVariablesRenames.put(pointer, myVariableRenamer.getNewName(variable)); } - Runnable runnable = new Runnable() { - public void run() { - myVariableRenamer.findUsages(myVariablesUsages, false, false); - } - }; + @RequiredReadAction + Runnable runnable = () -> myVariableRenamer.findUsages(myVariablesUsages, false, false); if (!ProgressManager.getInstance() .runProcessWithProgressSynchronously(runnable, RefactoringLocalize.searchingForVariables().get(), true, myProject)) { @@ -116,19 +111,19 @@ public void run() { private AutomaticVariableRenamer myVariableRenamer; + @RequiredWriteAction protected void performVariablesRenaming() { try { //forget about smart pointers - Map variableRenames = new HashMap(); + Map variableRenames = new HashMap<>(); for (Map.Entry entry : myVariablesRenames.entrySet()) { variableRenames.put(entry.getKey().getElement(), entry.getValue()); } for (UsageInfo usage : myVariablesUsages) { - if (usage instanceof MoveRenameUsageInfo) { - final MoveRenameUsageInfo renameUsageInfo = ((MoveRenameUsageInfo)usage); - final String newName = variableRenames.get(renameUsageInfo.getUpToDateReferencedElement()); - final PsiReference reference = renameUsageInfo.getReference(); + if (usage instanceof MoveRenameUsageInfo renameUsageInfo) { + String newName = variableRenames.get(renameUsageInfo.getUpToDateReferencedElement()); + PsiReference reference = renameUsageInfo.getReference(); if (reference != null) { reference.handleElementRename(newName); } @@ -136,9 +131,9 @@ protected void performVariablesRenaming() { } for (Map.Entry entry : myVariablesRenames.entrySet()) { - final String newName = entry.getValue(); + String newName = entry.getValue(); if (newName != null) { - final PsiVariable variable = (PsiVariable)entry.getKey().getElement(); + PsiVariable variable = (PsiVariable)entry.getKey().getElement(); variable.setName(newName); } } @@ -157,11 +152,12 @@ protected TurnRefsToSuperProcessorBase(Project project, boolean replaceInstanceO myReplaceInstanceOf = replaceInstanceOf; } - protected ArrayList detectTurnToSuperRefs(PsiReference[] refs, final ArrayList result) { + @RequiredReadAction + protected ArrayList detectTurnToSuperRefs(PsiReference[] refs, ArrayList result) { buildGraph(refs); for (PsiReference ref : refs) { - final PsiElement element = ref.getElement(); + PsiElement element = ref.getElement(); if (canTurnToSuper(element)) { result.add(new TurnToSuperReferenceUsageInfo(element)); } @@ -173,19 +169,19 @@ protected boolean canTurnToSuper(PsiElement ref) { return !myMarkedNodes.contains(ref); } - protected static void processTurnToSuperRefs(UsageInfo[] usages, final PsiClass aSuper) throws IncorrectOperationException { + @RequiredWriteAction + protected static void processTurnToSuperRefs(UsageInfo[] usages, PsiClass aSuper) throws IncorrectOperationException { for (UsageInfo usage : usages) { if (usage instanceof TurnToSuperReferenceUsageInfo) { - final PsiElement element = usage.getElement(); + PsiElement element = usage.getElement(); if (element != null) { - final PsiReference ref = element.getReference(); + PsiReference ref = element.getReference(); assert ref != null; PsiElement newElement = ref.bindToElement(aSuper); - if (newElement.getParent() instanceof PsiTypeElement) { - if (newElement.getParent().getParent() instanceof PsiTypeCastExpression) { - fixPossiblyRedundantCast((PsiTypeCastExpression)newElement.getParent().getParent()); - } + if (newElement.getParent() instanceof PsiTypeElement typeElem + && typeElem.getParent() instanceof PsiTypeCastExpression typeCast) { + fixPossiblyRedundantCast(typeCast); } } } @@ -223,9 +219,10 @@ private static void fixPossiblyRedundantCast(PsiTypeCastExpression cast) throws exprToReplace.replace(operand); } + @RequiredReadAction private void buildGraph(PsiReference[] refs) { myMarkedNodes.clear(); - myExpressionsQueue = new Queue(refs.length); + myExpressionsQueue = new Queue<>(refs.length); myElementToNode.clear(); for (PsiReference ref : refs) { processUsage(ref.getElement()); @@ -240,13 +237,8 @@ private void buildGraph(PsiReference[] refs) { private void processUsage(PsiElement ref) { if (ref instanceof PsiReferenceExpression) { - final PsiElement parent = ref.getParent(); - if (parent instanceof PsiReferenceExpression) { - final PsiReferenceExpression refExpr = (PsiReferenceExpression)parent; - final PsiElement refMember = refExpr.resolve(); - if (!isInSuper(refMember)) { - markNode(ref); - } + if (ref.getParent() instanceof PsiReferenceExpression refExpr && !isInSuper(refExpr.resolve())) { + markNode(ref); } return; } @@ -260,41 +252,36 @@ private void processUsage(PsiElement ref) { parent = pparent; pparent = parent.getParent(); } - final PsiTypeElement typeElement = (PsiTypeElement)parent; + PsiTypeElement typeElement = (PsiTypeElement)parent; addLink(typeElement, ref); addLink(ref, typeElement); - if (pparent instanceof PsiVariable) { - processVariableType((PsiVariable)pparent); + if (pparent instanceof PsiVariable variable) { + processVariableType(variable); } - else if (pparent instanceof PsiMethod) { - processMethodReturnType((PsiMethod)pparent); + else if (pparent instanceof PsiMethod method) { + processMethodReturnType(method); } else if (pparent instanceof PsiTypeCastExpression) { addLink(pparent, typeElement); addLink(typeElement, pparent); } - else if (pparent instanceof PsiReferenceParameterList) { - final PsiReferenceParameterList refParameterList = ((PsiReferenceParameterList)pparent); - final PsiElement ppparent = pparent.getParent(); - if (ppparent instanceof PsiJavaCodeReferenceElement) { - final PsiJavaCodeReferenceElement classReference = (PsiJavaCodeReferenceElement)ppparent; - if (classReference.getParent() instanceof PsiReferenceList) { - final PsiReferenceList referenceList = ((PsiReferenceList)ppparent.getParent()); - final PsiClass parentClass = PsiTreeUtil.getParentOfType(ref, PsiClass.class); + else if (pparent instanceof PsiReferenceParameterList refParameterList) { + PsiElement ppparent = pparent.getParent(); + if (ppparent instanceof PsiJavaCodeReferenceElement classReference) { + if (classReference.getParent() instanceof PsiReferenceList referenceList) { + PsiClass parentClass = PsiTreeUtil.getParentOfType(ref, PsiClass.class); if (parentClass != null) { if (referenceList.equals(parentClass.getExtendsList()) || referenceList.equals(parentClass.getImplementsList())) { - final PsiTypeElement[] typeParameterElements = refParameterList.getTypeParameterElements(); + PsiTypeElement[] typeParameterElements = refParameterList.getTypeParameterElements(); for (int i = 0; i < typeParameterElements.length; i++) { - if (typeParameterElements[i] == typeElement) { - final PsiElement resolved = classReference.resolve(); - if (resolved instanceof PsiClass) { - final PsiTypeParameter[] typeParameters = ((PsiClass)resolved).getTypeParameters(); - if (typeParameters.length > i) { - linkTypeParameterInstantiations(typeParameters[i], typeElement, parentClass); - return; - } + if (typeParameterElements[i] == typeElement + && classReference.resolve() instanceof PsiClass psiClass) { + PsiTypeParameter[] typeParameters = psiClass.getTypeParameters(); + if (typeParameters.length > i) { + linkTypeParameterInstantiations(typeParameters[i], typeElement, parentClass); + return; } } } @@ -306,7 +293,7 @@ else if (classReference.getParent() instanceof PsiTypeElement) { return; } else if (classReference.getParent() instanceof PsiNewExpression) { - final PsiVariable variable = PsiTreeUtil.getParentOfType(classReference, PsiVariable.class); + PsiVariable variable = PsiTreeUtil.getParentOfType(classReference, PsiVariable.class); if (variable != null) { processUsage(variable); return; @@ -320,23 +307,22 @@ else if (classReference.getParent() instanceof PsiAnonymousClass) { markNode(ref); //??? } } - else if (parent instanceof PsiNewExpression) { - PsiNewExpression newExpression = (PsiNewExpression)parent; - if (newExpression.getType() instanceof PsiArrayType) { - addLink(newExpression, ref); - addLink(ref, newExpression); - PsiArrayInitializerExpression initializer = newExpression.getArrayInitializer(); + else if (parent instanceof PsiNewExpression newExpr) { + if (newExpr.getType() instanceof PsiArrayType) { + addLink(newExpr, ref); + addLink(ref, newExpr); + PsiArrayInitializerExpression initializer = newExpr.getArrayInitializer(); if (initializer != null) { addLink(ref, initializer); } - checkToArray(ref, newExpression); + checkToArray(ref, newExpr); } else { markNode(ref); } } - else if (parent instanceof PsiJavaCodeReferenceElement && ref.equals(((PsiJavaCodeReferenceElement)parent).getQualifier())) { - final PsiElement resolved = ((PsiJavaCodeReferenceElement)parent).resolve(); + else if (parent instanceof PsiJavaCodeReferenceElement codeRefElem && ref.equals(codeRefElem.getQualifier())) { + PsiElement resolved = codeRefElem.resolve(); if (resolved == null || !isInSuper(resolved)) { markNode(ref); } @@ -348,58 +334,49 @@ else if (parent instanceof PsiJavaCodeReferenceElement && ref.equals(((PsiJavaCo private void linkTypeParameterInstantiations( PsiTypeParameter typeParameter, - final PsiTypeElement instantiation, - final PsiClass inheritingClass + PsiTypeElement instantiation, + PsiClass inheritingClass ) { - final PsiTypeParameterListOwner owner = typeParameter.getOwner(); - if (owner instanceof PsiClass) { - final PsiClass ownerClass = ((PsiClass)owner); - final LocalSearchScope derivedScope = new LocalSearchScope(inheritingClass); - final PsiSubstitutor substitutor = TypeConversionUtil.getClassSubstitutor(ownerClass, inheritingClass, PsiSubstitutor.EMPTY); + PsiTypeParameterListOwner owner = typeParameter.getOwner(); + if (owner instanceof PsiClass ownerClass) { + LocalSearchScope derivedScope = new LocalSearchScope(inheritingClass); + PsiSubstitutor substitutor = TypeConversionUtil.getClassSubstitutor(ownerClass, inheritingClass, PsiSubstitutor.EMPTY); if (substitutor == null) { return; } - final LocalSearchScope baseScope = new LocalSearchScope(ownerClass); - ReferencesSearch.search(typeParameter, baseScope).forEach(new Processor() { - public boolean process(final PsiReference ref) { - final PsiElement element = ref.getElement(); - final PsiElement parent = element.getParent(); - if (parent instanceof PsiTypeElement) { - final PsiElement pparent = parent.getParent(); - if (pparent instanceof PsiMethod && parent.equals(((PsiMethod)pparent).getReturnTypeElement())) { - final PsiMethod method = (PsiMethod)pparent; - final MethodSignature signature = method.getSignature(substitutor); - if (PsiUtil.isAccessible(method, inheritingClass, null)) { - final PsiMethod inInheritor = MethodSignatureUtil.findMethodBySignature(inheritingClass, signature, false); - if (inInheritor != null && inInheritor.getReturnTypeElement() != null) { - addLink(instantiation, method.getReturnTypeElement()); - addLink(method.getReturnTypeElement(), instantiation); - } + LocalSearchScope baseScope = new LocalSearchScope(ownerClass); + ReferencesSearch.search(typeParameter, baseScope).forEach(ref -> { + if (ref.getElement().getParent() instanceof PsiTypeElement typeElem) { + PsiElement pparent = typeElem.getParent(); + if (pparent instanceof PsiMethod method && typeElem.equals(method.getReturnTypeElement())) { + MethodSignature signature = method.getSignature(substitutor); + if (PsiUtil.isAccessible(method, inheritingClass, null)) { + PsiMethod inInheritor = MethodSignatureUtil.findMethodBySignature(inheritingClass, signature, false); + if (inInheritor != null && inInheritor.getReturnTypeElement() != null) { + addLink(instantiation, method.getReturnTypeElement()); + addLink(method.getReturnTypeElement(), instantiation); } } - else if (pparent instanceof PsiParameter) { - final PsiParameter parameter = (PsiParameter)pparent; - if (parameter.getDeclarationScope() instanceof PsiMethod) { - PsiMethod method = (PsiMethod)parameter.getDeclarationScope(); - final int index = ((PsiParameterList)parameter.getParent()).getParameterIndex(parameter); - final MethodSignature signature = method.getSignature(substitutor); - if (PsiUtil.isAccessible(method, inheritingClass, null)) { - final PsiMethod inInheritor = - MethodSignatureUtil.findMethodBySignature(inheritingClass, signature, false); - if (inInheritor != null) { - final PsiParameter[] inheritorParams = inInheritor.getParameterList().getParameters(); - LOG.assertTrue(inheritorParams.length > index); - final PsiTypeElement hisTypeElement = inheritorParams[index].getTypeElement(); - addLink(instantiation, hisTypeElement); - addLink(hisTypeElement, instantiation); - } - } + } + else if (pparent instanceof PsiParameter parameter + && parameter.getDeclarationScope() instanceof PsiMethod method) { + int index = ((PsiParameterList)parameter.getParent()).getParameterIndex(parameter); + MethodSignature signature = method.getSignature(substitutor); + if (PsiUtil.isAccessible(method, inheritingClass, null)) { + PsiMethod inInheritor = + MethodSignatureUtil.findMethodBySignature(inheritingClass, signature, false); + if (inInheritor != null) { + PsiParameter[] inheritorParams = inInheritor.getParameterList().getParameters(); + LOG.assertTrue(inheritorParams.length > index); + PsiTypeElement hisTypeElement = inheritorParams[index].getTypeElement(); + addLink(instantiation, hisTypeElement); + addLink(hisTypeElement, instantiation); } } } - - return true; } + + return true; }); } } @@ -424,87 +401,63 @@ else if (method.isVarArgs() && argIndex >= params.length) { } } + @RequiredReadAction private void checkToArray(PsiElement ref, PsiNewExpression newExpression) { - PsiElement tmp; - - final PsiClass javaUtilCollectionClass = - JavaPsiFacade.getInstance(myManager.getProject()).findClass(CommonClassNames.JAVA_UTIL_COLLECTION, ref.getResolveScope()); - if (javaUtilCollectionClass == null) { - return; - } - tmp = newExpression.getParent(); - if (!(tmp instanceof PsiExpressionList)) { - return; - } - tmp = tmp.getParent(); - if (!(tmp instanceof PsiMethodCallExpression)) { - return; - } - PsiMethodCallExpression methodCall = (PsiMethodCallExpression)tmp; - tmp = tmp.getParent(); - if (!(tmp instanceof PsiTypeCastExpression)) { - return; - } - PsiTypeCastExpression typeCast = (PsiTypeCastExpression)tmp; - - PsiReferenceExpression methodRef = methodCall.getMethodExpression(); - tmp = methodRef.resolve(); - if (!(tmp instanceof PsiMethod)) { - return; - } - PsiMethod method = (PsiMethod)tmp; - @NonNls final String name = method.getName(); - if (!name.equals("toArray")) { - return; - } + PsiClass javaUtilCollectionClass = JavaPsiFacade.getInstance(myManager.getProject()) + .findClass(CommonClassNames.JAVA_UTIL_COLLECTION, ref.getResolveScope()); + if (javaUtilCollectionClass != null + && newExpression.getParent() instanceof PsiExpressionList expressionList + && expressionList.getParent() instanceof PsiMethodCallExpression methodCall + && methodCall.getParent() instanceof PsiTypeCastExpression typeCast + && methodCall.getMethodExpression().resolve() instanceof PsiMethod method + && "toArray".equals(method.getName())) { + + PsiClass methodClass = method.getContainingClass(); + if (!methodClass.isInheritor(javaUtilCollectionClass, true)) { + return; + } - PsiClass methodClass = method.getContainingClass(); - if (!methodClass.isInheritor(javaUtilCollectionClass, true)) { - return; + // ok, this is an implementation of java.util.Collection.toArray + addLink(typeCast, ref); } - - // ok, this is an implementation of java.util.Collection.toArray - addLink(typeCast, ref); - } private void processVariableType(PsiVariable variable) { final PsiTypeElement type = variable.getTypeElement(); - final PsiExpression initializer = variable.getInitializer(); + PsiExpression initializer = variable.getInitializer(); if (initializer != null) { addLink(type, initializer); } for (PsiReference ref : ReferencesSearch.search(variable)) { - final PsiElement element = ref.getElement(); + PsiElement element = ref.getElement(); addLink(element, type); addLink(type, element); analyzeVarUsage(element); } - if (variable instanceof PsiParameter) { - final PsiElement declScope = ((PsiParameter)variable).getDeclarationScope(); + if (variable instanceof PsiParameter parameter) { + PsiElement declScope = parameter.getDeclarationScope(); if (declScope instanceof PsiCatchSection) { markNode(type); } - else if (declScope instanceof PsiForeachStatement) { - final PsiExpression iteratedValue = ((PsiForeachStatement)declScope).getIteratedValue(); + else if (declScope instanceof PsiForeachStatement forEach) { + PsiExpression iteratedValue = forEach.getIteratedValue(); addLink(type, iteratedValue); addLink(iteratedValue, type); } - else if (declScope instanceof PsiMethod) { - final PsiMethod method = (PsiMethod)declScope; - final int index = method.getParameterList().getParameterIndex((PsiParameter)variable); + else if (declScope instanceof PsiMethod method) { + final int index = method.getParameterList().getParameterIndex(parameter); { for (PsiReference call : ReferencesSearch.search(method)) { PsiElement ref = call.getElement(); PsiExpressionList argumentList; - if (ref.getParent() instanceof PsiCall) { - argumentList = ((PsiCall)ref.getParent()).getArgumentList(); + if (ref.getParent() instanceof PsiCall psiCall) { + argumentList = psiCall.getArgumentList(); } - else if (ref.getParent() instanceof PsiAnonymousClass) { - argumentList = ((PsiConstructorCall)ref.getParent().getParent()).getArgumentList(); + else if (ref.getParent() instanceof PsiAnonymousClass anonymousClass) { + argumentList = ((PsiConstructorCall)anonymousClass.getParent()).getArgumentList(); } else { continue; @@ -521,28 +474,28 @@ else if (ref.getParent() instanceof PsiAnonymousClass) { } final class Inner { - void linkInheritors(final PsiMethod[] methods) { - for (final PsiMethod superMethod : methods) { - final PsiParameter[] parameters = superMethod.getParameterList().getParameters(); + void linkInheritors(PsiMethod[] methods) { + for (PsiMethod superMethod : methods) { + PsiParameter[] parameters = superMethod.getParameterList().getParameters(); if (index >= parameters.length) { continue; } - final PsiTypeElement superType = parameters[index].getTypeElement(); + PsiTypeElement superType = parameters[index].getTypeElement(); addLink(superType, type); addLink(type, superType); } } } - final PsiMethod[] superMethods = method.findSuperMethods(); + PsiMethod[] superMethods = method.findSuperMethods(); new Inner().linkInheritors(superMethods); PsiClass containingClass = method.getContainingClass(); - List subClasses = new ArrayList(ClassInheritorsSearch.search(containingClass, false).findAll()); + List subClasses = new ArrayList<>(ClassInheritorsSearch.search(containingClass, false).findAll()); // ??? In the theory this is non-efficient way: too many inheritors can be processed. // ??? But in real use it seems reasonably fast. If poor performance problems emerged, // ??? should be optimized for (int i1 = 0; i1 != subClasses.size(); ++i1) { - final PsiMethod[] mBSs = subClasses.get(i1).findMethodsBySignature(method, true); + PsiMethod[] mBSs = subClasses.get(i1).findMethodsBySignature(method, true); new Inner().linkInheritors(mBSs); } } @@ -551,41 +504,41 @@ void linkInheritors(final PsiMethod[] methods) { } } else if (variable instanceof PsiResourceVariable) { - final PsiJavaParserFacade facade = JavaPsiFacade.getInstance(myProject).getParserFacade(); + PsiJavaParserFacade facade = JavaPsiFacade.getInstance(myProject).getParserFacade(); checkConstrainingType(type, facade.createTypeFromText(CommonClassNames.JAVA_LANG_AUTO_CLOSEABLE, variable)); } } - private void analyzeVarUsage(final PsiElement element) { + private void analyzeVarUsage(PsiElement element) { PsiType constrainingType = null; - final PsiElement parent = element.getParent(); + PsiElement parent = element.getParent(); if (parent instanceof PsiReturnStatement) { - final PsiMethod method = PsiTreeUtil.getParentOfType(parent, PsiMethod.class); + PsiMethod method = PsiTreeUtil.getParentOfType(parent, PsiMethod.class); assert method != null; constrainingType = method.getReturnType(); } - else if (parent instanceof PsiAssignmentExpression) { - constrainingType = ((PsiAssignmentExpression)parent).getLExpression().getType(); + else if (parent instanceof PsiAssignmentExpression assignment) { + constrainingType = assignment.getLExpression().getType(); } //todo[ann] this works for AImpl->A but fails on List (see testForEach1() and testIDEADEV23807()). //else if (parent instanceof PsiForeachStatement) { - // final PsiType exprType = ((PsiExpression)element).getType(); - // if (!(exprType instanceof PsiArrayType)) { - // final PsiJavaParserFacade facade = JavaPsiFacade.getInstance(myProject).getParserFacade(); - // constrainingType = facade.createTypeFromText(CommonClassNames.JAVA_LANG_ITERABLE, parent); - // } + // final PsiType exprType = ((PsiExpression)element).getType(); + // if (!(exprType instanceof PsiArrayType)) { + // final PsiJavaParserFacade facade = JavaPsiFacade.getInstance(myProject).getParserFacade(); + // constrainingType = facade.createTypeFromText(CommonClassNames.JAVA_LANG_ITERABLE, parent); + // } //} - else if (parent instanceof PsiLocalVariable) { - constrainingType = ((PsiLocalVariable)parent).getType(); + else if (parent instanceof PsiLocalVariable localVar) { + constrainingType = localVar.getType(); } checkConstrainingType(element, constrainingType); } private void checkConstrainingType(PsiElement element, @Nullable PsiType constrainingType) { - if (constrainingType instanceof PsiClassType) { - final PsiClass resolved = ((PsiClassType)constrainingType).resolve(); + if (constrainingType instanceof PsiClassType classType) { + PsiClass resolved = classType.resolve(); if (!myClass.equals(resolved)) { if (resolved == null || !isSuperInheritor(resolved)) { markNode(element); @@ -594,30 +547,30 @@ private void checkConstrainingType(PsiElement element, @Nullable PsiType constra } } - private void processMethodReturnType(final PsiMethod method) { + private void processMethodReturnType(PsiMethod method) { final PsiTypeElement returnType = method.getReturnTypeElement(); for (PsiReference call : ReferencesSearch.search(method)) { - final PsiElement ref = call.getElement(); + PsiElement ref = call.getElement(); if (PsiTreeUtil.getParentOfType(ref, PsiDocComment.class) != null) { continue; } - final PsiElement parent = ref.getParent(); + PsiElement parent = ref.getParent(); addLink(parent, returnType); } - final PsiReturnStatement[] returnStatements = RefactoringUtil.findReturnStatements(method); - for (final PsiReturnStatement returnStatement : returnStatements) { - final PsiExpression returnValue = returnStatement.getReturnValue(); + PsiReturnStatement[] returnStatements = RefactoringUtil.findReturnStatements(method); + for (PsiReturnStatement returnStatement : returnStatements) { + PsiExpression returnValue = returnStatement.getReturnValue(); if (returnValue != null) { addLink(returnType, returnValue); } } - final PsiMethod[] superMethods = method.findSuperMethods(); + PsiMethod[] superMethods = method.findSuperMethods(); final class Inner { - public void linkInheritors(final PsiMethod[] methods) { - for (final PsiMethod superMethod : methods) { - final PsiTypeElement superType = superMethod.getReturnTypeElement(); + public void linkInheritors(PsiMethod[] methods) { + for (PsiMethod superMethod : methods) { + PsiTypeElement superType = superMethod.getReturnTypeElement(); addLink(superType, returnType); addLink(returnType, superType); } @@ -629,9 +582,9 @@ public void linkInheritors(final PsiMethod[] methods) { // ??? But in real use it seems reasonably fast. If poor performance problems emerged, // ??? should be optimized PsiClass containingClass = method.getContainingClass(); - final PsiClass[] subClasses = ClassInheritorsSearch.search(containingClass, false).toArray(PsiClass.EMPTY_ARRAY); + PsiClass[] subClasses = ClassInheritorsSearch.search(containingClass, false).toArray(PsiClass.EMPTY_ARRAY); for (int i1 = 0; i1 != subClasses.length; ++i1) { - final PsiMethod[] mBSs = subClasses[i1].findMethodsBySignature(method, true); + PsiMethod[] mBSs = subClasses[i1].findMethodsBySignature(method, true); new Inner().linkInheritors(mBSs); } } @@ -640,16 +593,14 @@ private void processQueue() { while (!myExpressionsQueue.isEmpty()) { PsiExpression expr = myExpressionsQueue.pullFirst(); PsiElement parent = expr.getParent(); - if (parent instanceof PsiAssignmentExpression) { - PsiAssignmentExpression assignment = (PsiAssignmentExpression)parent; + if (parent instanceof PsiAssignmentExpression assignment) { if (assignment.getRExpression() != null) { addLink(assignment.getLExpression(), assignment.getRExpression()); } addLink(assignment, assignment.getLExpression()); addLink(assignment.getLExpression(), assignment); } - else if (parent instanceof PsiArrayAccessExpression) { - PsiArrayAccessExpression arrayAccess = (PsiArrayAccessExpression)parent; + else if (parent instanceof PsiArrayAccessExpression arrayAccess) { if (expr.equals(arrayAccess.getArrayExpression())) { addLink(arrayAccess, expr); addLink(expr, arrayAccess); @@ -659,19 +610,16 @@ else if (parent instanceof PsiParenthesizedExpression) { addLink(parent, expr); addLink(expr, parent); } - else if (parent instanceof PsiArrayInitializerExpression) { - PsiArrayInitializerExpression arrayInitializerExpr = (PsiArrayInitializerExpression)parent; - PsiExpression[] initializers = arrayInitializerExpr.getInitializers(); - for (PsiExpression initializer : initializers) { + else if (parent instanceof PsiArrayInitializerExpression arrayInitializerExpr) { + for (PsiExpression initializer : arrayInitializerExpr.getInitializers()) { addLink(arrayInitializerExpr, initializer); } } - else if (parent instanceof PsiExpressionList) { - PsiElement pparent = parent.getParent(); - if (pparent instanceof PsiCallExpression) { - PsiMethod method = ((PsiCallExpression)pparent).resolveMethod(); + else if (parent instanceof PsiExpressionList expressionList) { + if (parent.getParent() instanceof PsiCallExpression call) { + PsiMethod method = call.resolveMethod(); if (method != null) { - addArgumentParameterLink(expr, (PsiExpressionList)parent, method); + addArgumentParameterLink(expr, expressionList, method); } } } @@ -680,16 +628,12 @@ else if (parent instanceof PsiExpressionList) { protected void markNodes() { //for (Iterator iterator = myDependencyMap.keySet().getSectionsIterator(); getSectionsIterator.hasNext();) { - for (final PsiElement element : myElementToNode.keySet()) { + for (PsiElement element : myElementToNode.keySet()) { if (element instanceof PsiExpression) { - final PsiElement parent = element.getParent(); - if (parent instanceof PsiReferenceExpression) { - final PsiReferenceExpression refExpr = (PsiReferenceExpression)parent; - if (element.equals(refExpr.getQualifierExpression())) { - final PsiElement refElement = refExpr.resolve(); - if (refElement != null && !isInSuper(refElement)) { - markNode(element); - } + if (element.getParent() instanceof PsiReferenceExpression refExpr && element.equals(refExpr.getQualifierExpression())) { + PsiElement refElement = refExpr.resolve(); + if (refElement != null && !isInSuper(refElement)) { + markNode(element); } } } @@ -700,9 +644,9 @@ else if (!myReplaceInstanceOf && element.getParent() != null else if (element.getParent() instanceof PsiClassObjectAccessExpression) { markNode(element); } - else if (element instanceof PsiParameter) { - final PsiType type = TypeConversionUtil.erasure(((PsiParameter)element).getType()); - final PsiClass aClass = PsiUtil.resolveClassInType(type); + else if (element instanceof PsiParameter parameter) { + PsiType type = TypeConversionUtil.erasure(parameter.getType()); + PsiClass aClass = PsiUtil.resolveClassInType(type); if (aClass != null) { if (!myManager.isInProject(element) || !myManager.areElementsEquivalent(aClass, myClass)) { if (!isSuperInheritor(aClass)) { @@ -727,16 +671,16 @@ protected void addLink(PsiElement source, PsiElement target) { if (from == null) { from = new Node(source); - if (source instanceof PsiExpression) { - myExpressionsQueue.addLast((PsiExpression)source); + if (source instanceof PsiExpression expression) { + myExpressionsQueue.addLast(expression); } myElementToNode.put(source, from); } if (to == null) { to = new Node(target); - if (target instanceof PsiExpression) { - myExpressionsQueue.addLast((PsiExpression)target); + if (target instanceof PsiExpression expression) { + myExpressionsQueue.addLast(expression); } myElementToNode.put(target, to); } @@ -745,10 +689,10 @@ protected void addLink(PsiElement source, PsiElement target) { } private void spreadMarks() { - final LinkedList markedNodes = new LinkedList(); + List markedNodes = new LinkedList<>(); - for (final PsiElement markedNode : myMarkedNodes) { - final Node node = myElementToNode.get(markedNode); + for (PsiElement markedNode : myMarkedNodes) { + Node node = myElementToNode.get(markedNode); if (node != null) { markedNodes.addFirst(node); } @@ -757,11 +701,12 @@ private void spreadMarks() { GlobalAnalyzer.doOneEnd(markedNodes, new Colorer()); } - private void markNode(final PsiElement node) { + private void markNode(PsiElement node) { myMarkedNodes.add(node); } class Colorer implements OneEndFunctor { + @Override public Mark compute(Mark from, Mark edge, Mark to) { VisitMark mark = new VisitMark((VisitMark)to); @@ -791,6 +736,7 @@ private static class VisitMark implements Mark { private boolean myVisited; private final PsiElement myElement; + @Override public boolean coincidesWith(Mark x) { return ((VisitMark)x).myVisited == myVisited; } @@ -819,7 +765,7 @@ public PsiElement getElement() { } private static class Node extends NodeImpl { - private final HashSet mySuccessors = new HashSet(); + private final Set mySuccessors = new HashSet<>(); private VisitMark myMark; public Node(PsiElement x) { @@ -827,10 +773,12 @@ public Node(PsiElement x) { myMark = new VisitMark(x); } + @Override public Mark getMark() { return myMark; } + @Override public void setMark(Mark x) { myMark = (VisitMark)x; } diff --git a/plugin/src/main/java/com/intellij/java/impl/refactoring/util/FixableUsagesRefactoringProcessor.java b/plugin/src/main/java/com/intellij/java/impl/refactoring/util/FixableUsagesRefactoringProcessor.java index 3fa91c9bea..5e554b2de1 100644 --- a/plugin/src/main/java/com/intellij/java/impl/refactoring/util/FixableUsagesRefactoringProcessor.java +++ b/plugin/src/main/java/com/intellij/java/impl/refactoring/util/FixableUsagesRefactoringProcessor.java @@ -16,6 +16,7 @@ package com.intellij.java.impl.refactoring.util; import com.intellij.xml.util.XmlUtil; +import consulo.annotation.access.RequiredReadAction; import consulo.language.editor.refactoring.BaseRefactoringProcessor; import consulo.language.editor.refactoring.util.CommonRefactoringUtil; import consulo.language.psi.PsiElement; @@ -42,9 +43,9 @@ protected FixableUsagesRefactoringProcessor(Project project) { protected void performRefactoring(@Nonnull UsageInfo[] usageInfos) { CommonRefactoringUtil.sortDepthFirstRightLeftOrder(usageInfos); for (UsageInfo usageInfo : usageInfos) { - if (usageInfo instanceof FixableUsageInfo) { + if (usageInfo instanceof FixableUsageInfo fixableUsageInfo) { try { - ((FixableUsageInfo)usageInfo).fixUsage(); + fixableUsageInfo.fixUsage(); } catch (IncorrectOperationException e) { LOG.info(e); @@ -58,13 +59,14 @@ protected void performRefactoring(@Nonnull UsageInfo[] usageInfos) { protected final UsageInfo[] findUsages() { List usages = Collections.synchronizedList(new ArrayList()); findUsages(usages); - final int numUsages = usages.size(); - final FixableUsageInfo[] usageArray = usages.toArray(new FixableUsageInfo[numUsages]); + int numUsages = usages.size(); + FixableUsageInfo[] usageArray = usages.toArray(new FixableUsageInfo[numUsages]); return usageArray; } protected abstract void findUsages(@Nonnull List usages); + @RequiredReadAction protected static void checkConflicts(SimpleReference refUsages, MultiMap conflicts) { for (UsageInfo info : refUsages.get()) { String conflict = ((FixableUsageInfo)info).getConflictMessage();