private static void rename( PsiReference ref, String newName, PsiManager manager, PsiMember elementToResolve) { final PsiElement renamed = ref.handleElementRename(newName); PsiElement newly_resolved = ref.resolve(); if (!manager.areElementsEquivalent(newly_resolved, elementToResolve)) { if (newly_resolved instanceof PsiMethod) { newly_resolved = GroovyPropertyUtils.findFieldForAccessor((PsiMethod) newly_resolved, false); } if (!manager.areElementsEquivalent(newly_resolved, elementToResolve)) { qualify(elementToResolve, renamed, newName); } } }
public static void doRenameGenericNamedElement( @NotNull PsiElement namedElement, String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException { PsiWritableMetaData writableMetaData = null; if (namedElement instanceof PsiMetaOwner) { final PsiMetaData metaData = ((PsiMetaOwner) namedElement).getMetaData(); if (metaData instanceof PsiWritableMetaData) { writableMetaData = (PsiWritableMetaData) metaData; } } if (writableMetaData == null && !(namedElement instanceof PsiNamedElement)) { LOG.error("Unknown element type:" + namedElement); } boolean hasBindables = false; for (UsageInfo usage : usages) { if (!(usage.getReference() instanceof BindablePsiReference)) { rename(usage, newName); } else { hasBindables = true; } } if (writableMetaData != null) { writableMetaData.setName(newName); } else { PsiElement namedElementAfterRename = ((PsiNamedElement) namedElement).setName(newName); if (namedElementAfterRename != null) namedElement = namedElementAfterRename; } if (hasBindables) { for (UsageInfo usage : usages) { final PsiReference ref = usage.getReference(); if (ref instanceof BindablePsiReference) { try { ref.bindToElement(namedElement); } catch (IncorrectOperationException e) { // fall back to old scheme ref.handleElementRename(newName); } } } } if (listener != null) { listener.elementRenamed(namedElement); } }
@Override public void renameElement( final PsiElement psiElement, final String newName, final UsageInfo[] usages, final RefactoringElementListener listener) throws IncorrectOperationException { final GrField field = (GrField) psiElement; final PsiMethod getter = GroovyPropertyUtils.findGetterForField(field); final PsiMethod setter = GroovyPropertyUtils.findSetterForField(field); final String newGetterName = (getter != null && getter.getName().startsWith("is") ? "is" : "get") + StringUtil.capitalize(newName); final String newSetterName = "set" + StringUtil.capitalize(newName); final PsiManager manager = field.getManager(); List<PsiReference> getterRefs = new ArrayList<PsiReference>(); List<PsiReference> setterRefs = new ArrayList<PsiReference>(); List<PsiReference> fieldRefs = new ArrayList<PsiReference>(); for (UsageInfo usage : usages) { final PsiElement element = usage.getElement(); if (element == null) continue; PsiReference ref = element.findReferenceAt(usage.startOffset); if (ref == null) continue; PsiElement resolved = ref.resolve(); if (manager.areElementsEquivalent(resolved, getter)) { if (isPropertyAccess(element)) { fieldRefs.add(ref); } else { getterRefs.add(ref); } } else if (manager.areElementsEquivalent(resolved, setter)) { if (isPropertyAccess(element)) { fieldRefs.add(ref); } else { setterRefs.add(ref); } } else if (manager.areElementsEquivalent(resolved, field)) { fieldRefs.add(ref); } else { ref.handleElementRename(newName); } } field.setName(newName); final PsiMethod newGetter = GroovyPropertyUtils.findGetterForField(field); doRename(newGetterName, manager, getterRefs, newGetter); final PsiMethod newSetter = GroovyPropertyUtils.findSetterForField(field); doRename(newSetterName, manager, setterRefs, newSetter); doRename(newName, manager, fieldRefs, field); listener.elementRenamed(field); }
public static void rename(UsageInfo info, String newName) throws IncorrectOperationException { if (info.getElement() == null) return; PsiReference ref = info.getReference(); if (ref == null) return; ref.handleElementRename(newName); }