Search in sources :

Example 6 with MoveRenameUsageInfo

use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.

the class RenameJavaClassProcessor method findCollisions.

public void findCollisions(final PsiElement element, final String newName, final Map<? extends PsiElement, String> allRenames, final List<UsageInfo> result) {
    final PsiClass aClass = (PsiClass) element;
    final ClassCollisionsDetector classCollisionsDetector = new ClassCollisionsDetector(aClass);
    Collection<UsageInfo> initialResults = new ArrayList<>(result);
    for (UsageInfo usageInfo : initialResults) {
        if (usageInfo instanceof MoveRenameUsageInfo) {
            classCollisionsDetector.addClassCollisions(usageInfo.getElement(), newName, result);
        }
    }
    findSubmemberHidesMemberCollisions(aClass, newName, result);
    if (aClass instanceof PsiTypeParameter) {
        final PsiTypeParameterListOwner owner = ((PsiTypeParameter) aClass).getOwner();
        if (owner != null) {
            for (PsiTypeParameter typeParameter : owner.getTypeParameters()) {
                if (Comparing.equal(newName, typeParameter.getName())) {
                    result.add(new UnresolvableCollisionUsageInfo(aClass, typeParameter) {

                        @Override
                        public String getDescription() {
                            return "There is already type parameter in " + RefactoringUIUtil.getDescription(aClass, false) + " with name " + newName;
                        }
                    });
                }
            }
        }
    }
}
Also used : MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo)

Example 7 with MoveRenameUsageInfo

use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.

the class MoveClassesOrPackagesUtil method findUsages.

public static UsageInfo[] findUsages(final PsiElement element, boolean searchInStringsAndComments, boolean searchInNonJavaFiles, final String newQName) {
    PsiManager manager = element.getManager();
    ArrayList<UsageInfo> results = new ArrayList<>();
    Set<PsiReference> foundReferences = new HashSet<>();
    GlobalSearchScope projectScope = GlobalSearchScope.projectScope(manager.getProject());
    for (PsiReference reference : ReferencesSearch.search(element, projectScope, false)) {
        TextRange range = reference.getRangeInElement();
        if (foundReferences.contains(reference))
            continue;
        results.add(new MoveRenameUsageInfo(reference.getElement(), reference, range.getStartOffset(), range.getEndOffset(), element, false));
        foundReferences.add(reference);
    }
    findNonCodeUsages(searchInStringsAndComments, searchInNonJavaFiles, element, newQName, results);
    preprocessUsages(results);
    return results.toArray(new UsageInfo[results.size()]);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) TextRange(com.intellij.openapi.util.TextRange) UsageInfo(com.intellij.usageView.UsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo)

Example 8 with MoveRenameUsageInfo

use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.

the class UsageViewUtil method removeDuplicatedUsages.

@NotNull
public static UsageInfo[] removeDuplicatedUsages(@NotNull UsageInfo[] usages) {
    Set<UsageInfo> set = new LinkedHashSet<>(Arrays.asList(usages));
    // Replace duplicates of move rename usage infos in injections from non code usages of master files
    String newTextInNonCodeUsage = null;
    for (UsageInfo usage : usages) {
        if (!(usage instanceof NonCodeUsageInfo))
            continue;
        newTextInNonCodeUsage = ((NonCodeUsageInfo) usage).newText;
        break;
    }
    if (newTextInNonCodeUsage != null) {
        for (UsageInfo usage : usages) {
            if (!(usage instanceof MoveRenameUsageInfo))
                continue;
            PsiFile file = usage.getFile();
            if (file != null) {
                PsiElement context = InjectedLanguageManager.getInstance(file.getProject()).getInjectionHost(file);
                if (context != null) {
                    PsiElement usageElement = usage.getElement();
                    if (usageElement == null)
                        continue;
                    PsiReference psiReference = usage.getReference();
                    if (psiReference == null)
                        continue;
                    int injectionOffsetInMasterFile = InjectedLanguageManager.getInstance(usageElement.getProject()).injectedToHost(usageElement, usageElement.getTextOffset());
                    TextRange rangeInElement = usage.getRangeInElement();
                    assert rangeInElement != null : usage;
                    TextRange range = rangeInElement.shiftRight(injectionOffsetInMasterFile);
                    PsiFile containingFile = context.getContainingFile();
                    //move package to another package
                    if (containingFile == null)
                        continue;
                    set.remove(NonCodeUsageInfo.create(containingFile, range.getStartOffset(), range.getEndOffset(), ((MoveRenameUsageInfo) usage).getReferencedElement(), newTextInNonCodeUsage));
                }
            }
        }
    }
    return set.toArray(new UsageInfo[set.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with MoveRenameUsageInfo

use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.

the class ChangeSignatureProcessorBase method filterUsages.

protected static List<UsageInfo> filterUsages(List<UsageInfo> infos) {
    Map<PsiElement, MoveRenameUsageInfo> moveRenameInfos = new HashMap<>();
    Set<PsiElement> usedElements = new HashSet<>();
    List<UsageInfo> result = new ArrayList<>(infos.size() / 2);
    for (UsageInfo info : infos) {
        LOG.assertTrue(info != null);
        PsiElement element = info.getElement();
        if (info instanceof MoveRenameUsageInfo) {
            if (usedElements.contains(element))
                continue;
            moveRenameInfos.put(element, (MoveRenameUsageInfo) info);
        } else {
            moveRenameInfos.remove(element);
            usedElements.add(element);
            if (!(info instanceof PossiblyIncorrectUsage) || ((PossiblyIncorrectUsage) info).isCorrect()) {
                result.add(info);
            }
        }
    }
    result.addAll(moveRenameInfos.values());
    return result;
}
Also used : HashMap(com.intellij.util.containers.hash.HashMap) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) HashSet(com.intellij.util.containers.hash.HashSet)

Example 10 with MoveRenameUsageInfo

use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.

the class RenameJavaMethodProcessor method renameElement.

public void renameElement(final PsiElement psiElement, final String newName, final UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    PsiMethod method = (PsiMethod) psiElement;
    Set<PsiMethod> methodAndOverriders = new HashSet<>();
    Set<PsiClass> containingClasses = new HashSet<>();
    LinkedHashSet<PsiElement> renamedReferences = new LinkedHashSet<>();
    List<MemberHidesOuterMemberUsageInfo> outerHides = new ArrayList<>();
    List<MemberHidesStaticImportUsageInfo> staticImportHides = new ArrayList<>();
    methodAndOverriders.add(method);
    containingClasses.add(method.getContainingClass());
    // do actual rename of overriding/implementing methods and of references to all them
    for (UsageInfo usage : usages) {
        PsiElement element = usage.getElement();
        if (element == null)
            continue;
        if (usage instanceof MemberHidesStaticImportUsageInfo) {
            staticImportHides.add((MemberHidesStaticImportUsageInfo) usage);
        } else if (usage instanceof MemberHidesOuterMemberUsageInfo) {
            PsiJavaCodeReferenceElement collidingRef = (PsiJavaCodeReferenceElement) element;
            PsiMethod resolved = (PsiMethod) collidingRef.resolve();
            outerHides.add(new MemberHidesOuterMemberUsageInfo(element, resolved));
        } else if (!(element instanceof PsiMethod)) {
            final PsiReference ref;
            if (usage instanceof MoveRenameUsageInfo) {
                ref = usage.getReference();
            } else {
                ref = element.getReference();
            }
            if (ref instanceof PsiImportStaticReferenceElement && ((PsiImportStaticReferenceElement) ref).multiResolve(false).length > 1) {
                continue;
            }
            if (ref != null) {
                PsiElement e = processRef(ref, newName);
                if (e != null) {
                    renamedReferences.add(e);
                }
            }
        } else {
            PsiMethod overrider = (PsiMethod) element;
            methodAndOverriders.add(overrider);
            containingClasses.add(overrider.getContainingClass());
        }
    }
    // do actual rename of method
    method.setName(newName);
    for (UsageInfo usage : usages) {
        PsiElement element = usage.getElement();
        if (element instanceof PsiMethod) {
            ((PsiMethod) element).setName(newName);
        }
    }
    if (listener != null) {
        listener.elementRenamed(method);
    }
    for (PsiElement element : renamedReferences) {
        fixNameCollisionsWithInnerClassMethod(element, newName, methodAndOverriders, containingClasses, method.hasModifierProperty(PsiModifier.STATIC));
    }
    qualifyOuterMemberReferences(outerHides);
    qualifyStaticImportReferences(staticImportHides);
    if (!method.isConstructor() && method.findDeepestSuperMethods().length == 0) {
        PsiAnnotation annotation = AnnotationUtil.findAnnotation(method, CommonClassNames.JAVA_LANG_OVERRIDE);
        if (annotation != null && annotation.isPhysical()) {
            annotation.delete();
        }
    }
}
Also used : MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) HashSet(com.intellij.util.containers.HashSet)

Aggregations

MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)16 UsageInfo (com.intellij.usageView.UsageInfo)15 PsiElement (com.intellij.psi.PsiElement)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 UnresolvableCollisionUsageInfo (com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo)3 NonCodeUsageInfo (com.intellij.refactoring.util.NonCodeUsageInfo)3 TextRange (com.intellij.openapi.util.TextRange)2 PsiFile (com.intellij.psi.PsiFile)2 PsiReference (com.intellij.psi.PsiReference)2 DefaultConstructorImplicitUsageInfo (com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo)2 NoConstructorClassUsageInfo (com.intellij.refactoring.util.usageInfo.NoConstructorClassUsageInfo)2 HashSet (com.intellij.util.containers.HashSet)2 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 ProperTextRange (com.intellij.openapi.util.ProperTextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 LightElement (com.intellij.psi.impl.light.LightElement)1 PsiDocTagValue (com.intellij.psi.javadoc.PsiDocTagValue)1 XmlElement (com.intellij.psi.xml.XmlElement)1