From 4cb86edc8d704d7545255f0393d5a80d35f24394 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 18 Jun 2018 15:21:30 +0700 Subject: [PATCH] RefactoringConflictsUtil#analyzeModuleConflicts: use non-walking visitor for compiled elements Review ID: IDEA-CR-33653 --- .../util/RefactoringConflictsUtil.java | 117 +++++++++++------- 1 file changed, 69 insertions(+), 48 deletions(-) diff --git a/java/java-impl/src/com/intellij/refactoring/util/RefactoringConflictsUtil.java b/java/java-impl/src/com/intellij/refactoring/util/RefactoringConflictsUtil.java index 9605fb7d5946..50de4d5b8386 100644 --- a/java/java-impl/src/com/intellij/refactoring/util/RefactoringConflictsUtil.java +++ b/java/java-impl/src/com/intellij/refactoring/util/RefactoringConflictsUtil.java @@ -28,6 +28,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Collection; import java.util.HashSet; import java.util.Set; +import java.util.function.Consumer; /** * @author anna @@ -242,64 +243,84 @@ public class RefactoringConflictsUtil { if (targetModule == null) return; final GlobalSearchScope resolveScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(targetModule); final HashSet reported = new HashSet<>(); - for (final PsiElement scope : scopes) { - scope.accept(new JavaRecursiveElementWalkingVisitor() { - @Override public void visitReferenceElement(PsiJavaCodeReferenceElement reference) { - super.visitReferenceElement(reference); - final PsiElement resolved = reference.resolve(); - if (resolved != null && - !reported.contains(resolved) && - !CommonRefactoringUtil.isAncestor(resolved, scopes) && - !(resolved instanceof LightElement) && - !haveElementInScope(resolved)) { - if (resolved instanceof PsiMethod) { - for (PsiMethod superMethod : ((PsiMethod)resolved).findDeepestSuperMethods()) { - if (haveElementInScope(superMethod)) return; - } + Consumer processor = new Consumer() { + @Override + public void accept(PsiJavaCodeReferenceElement reference) { + final PsiElement resolved = reference.resolve(); + if (resolved != null && + !reported.contains(resolved) && + !CommonRefactoringUtil.isAncestor(resolved, scopes) && + !(resolved instanceof LightElement) && + !haveElementInScope(resolved)) { + if (resolved instanceof PsiMethod) { + for (PsiMethod superMethod : ((PsiMethod)resolved).findDeepestSuperMethods()) { + if (haveElementInScope(superMethod)) return; } - final String scopeDescription = RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(reference), true); - final String message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.in.module.2", - RefactoringUIUtil.getDescription(resolved, true), - scopeDescription, - CommonRefactoringUtil.htmlEmphasize(targetModule.getName())); - conflicts.putValue(reference, CommonRefactoringUtil.capitalize(message)); - reported.add(resolved); } + final String scopeDescription = RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(reference), true); + final String message = RefactoringBundle.message("0.referenced.in.1.will.not.be.accessible.in.module.2", + RefactoringUIUtil.getDescription(resolved, true), + scopeDescription, + CommonRefactoringUtil.htmlEmphasize(targetModule.getName())); + conflicts.putValue(reference, CommonRefactoringUtil.capitalize(message)); + reported.add(resolved); } + } - private boolean haveElementInScope(PsiElement resolved) { - if (PsiSearchScopeUtil.isInScope(resolveScope, resolved)){ - return true; - } - if (!resolved.getManager().isInProject(resolved)) { - if (resolved instanceof PsiMember) { - final PsiClass containingClass = ((PsiMember)resolved).getContainingClass(); - if (containingClass != null) { - final String fqn = containingClass.getQualifiedName(); - if (fqn != null) { - final PsiClass classFromTarget = JavaPsiFacade.getInstance(project).findClass(fqn, resolveScope); - if (classFromTarget != null) { - if (resolved instanceof PsiMethod) { - return classFromTarget.findMethodsBySignature((PsiMethod)resolved, true).length > 0; - } - if (resolved instanceof PsiField ) { - return classFromTarget.findFieldByName(((PsiField)resolved).getName(), false) != null; - } - if (resolved instanceof PsiClass) { - return classFromTarget.findInnerClassByName(((PsiClass)resolved).getName(), false) != null; - } + private boolean haveElementInScope(PsiElement resolved) { + if (PsiSearchScopeUtil.isInScope(resolveScope, resolved)){ + return true; + } + if (!resolved.getManager().isInProject(resolved)) { + if (resolved instanceof PsiMember) { + final PsiClass containingClass = ((PsiMember)resolved).getContainingClass(); + if (containingClass != null) { + final String fqn = containingClass.getQualifiedName(); + if (fqn != null) { + final PsiClass classFromTarget = JavaPsiFacade.getInstance(project).findClass(fqn, resolveScope); + if (classFromTarget != null) { + if (resolved instanceof PsiMethod) { + return classFromTarget.findMethodsBySignature((PsiMethod)resolved, true).length > 0; + } + if (resolved instanceof PsiField ) { + return classFromTarget.findFieldByName(((PsiField)resolved).getName(), false) != null; + } + if (resolved instanceof PsiClass) { + return classFromTarget.findInnerClassByName(((PsiClass)resolved).getName(), false) != null; } } } } - if (resolved instanceof PsiClass) { - final String fqn = ((PsiClass)resolved).getQualifiedName(); - return fqn != null && JavaPsiFacade.getInstance(project).findClass(fqn, resolveScope) != null; - } } - return false; + if (resolved instanceof PsiClass) { + final String fqn = ((PsiClass)resolved).getQualifiedName(); + return fqn != null && JavaPsiFacade.getInstance(project).findClass(fqn, resolveScope) != null; + } } - }); + return false; + } + }; + for (final PsiElement scope : scopes) { + JavaElementVisitor visitor; + if (scope instanceof PsiCompiledElement) { + // For compiled element walking visitor should not be used: see PsiWalkingState#elementStarted + visitor = new JavaRecursiveElementVisitor() { + @Override + public void visitReferenceElement(PsiJavaCodeReferenceElement reference) { + super.visitReferenceElement(reference); + processor.accept(reference); + } + }; + } else { + visitor = new JavaRecursiveElementWalkingVisitor() { + @Override + public void visitReferenceElement(PsiJavaCodeReferenceElement reference) { + super.visitReferenceElement(reference); + processor.accept(reference); + } + }; + } + scope.accept(visitor); } boolean isInTestSources = ModuleRootManager.getInstance(targetModule).getFileIndex().isInTestSourceContent(vFile);