use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.
the class GrChageSignatureUsageSearcher method findSimpleUsagesWithoutParameters.
private PsiMethod[] findSimpleUsagesWithoutParameters(final PsiMethod method, final ArrayList<UsageInfo> result, boolean isToModifyArgs, boolean isToThrowExceptions, boolean isOriginal) {
GlobalSearchScope projectScope = GlobalSearchScope.projectScope(method.getProject());
PsiMethod[] overridingMethods = OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY);
for (PsiMethod overridingMethod : overridingMethods) {
if (GroovyLanguage.INSTANCE.equals(overridingMethod.getLanguage())) {
result.add(new OverriderUsageInfo(overridingMethod, method, isOriginal, isToModifyArgs, isToThrowExceptions));
}
}
boolean needToChangeCalls = !myChangeInfo.isGenerateDelegate() && (myChangeInfo.isNameChanged() || myChangeInfo.isParameterSetOrOrderChanged() || myChangeInfo.isExceptionSetOrOrderChanged() || myChangeInfo.isVisibilityChanged());
if (needToChangeCalls) {
PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
for (PsiReference ref : refs) {
PsiElement element = ref.getElement();
if (!GroovyLanguage.INSTANCE.equals(element.getLanguage()))
continue;
boolean isToCatchExceptions = isToThrowExceptions && needToCatchExceptions(RefactoringUtil.getEnclosingMethod(element));
if (PsiUtil.isMethodUsage(element)) {
result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
} else if (element instanceof GrDocTagValueToken) {
result.add(new UsageInfo(ref.getElement()));
} else if (element instanceof GrMethod && ((GrMethod) element).isConstructor()) {
DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo((GrMethod) element, ((GrMethod) element).getContainingClass(), method);
result.add(implicitUsageInfo);
} else if (element instanceof PsiClass) {
LOG.assertTrue(method.isConstructor());
final PsiClass psiClass = (PsiClass) element;
if (psiClass instanceof GrAnonymousClassDefinition) {
result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
continue;
}
/*if (!(myChangeInfo instanceof JavaChangeInfoImpl)) continue; todo propagate methods
if (shouldPropagateToNonPhysicalMethod(method, result, psiClass,
((JavaChangeInfoImpl)myChangeInfo).propagateParametersMethods)) {
continue;
}
if (shouldPropagateToNonPhysicalMethod(method, result, psiClass,
((JavaChangeInfoImpl)myChangeInfo).propagateExceptionsMethods)) {
continue;
}*/
result.add(new NoConstructorClassUsageInfo(psiClass));
} else if (ref instanceof PsiCallReference) {
result.add(new CallReferenceUsageInfo((PsiCallReference) ref));
} else {
result.add(new MoveRenameUsageInfo(element, ref, method));
}
}
} else if (myChangeInfo.isParameterTypesChanged()) {
PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
for (PsiReference reference : refs) {
final PsiElement element = reference.getElement();
if (element instanceof GrDocTagValueToken) {
result.add(new UsageInfo(reference));
}
}
}
// Conflicts
if (method instanceof GrMethod) {
detectLocalsCollisionsInMethod((GrMethod) method, result, isOriginal);
}
for (final PsiMethod overridingMethod : overridingMethods) {
if (overridingMethod instanceof GrMethod) {
detectLocalsCollisionsInMethod((GrMethod) overridingMethod, result, isOriginal);
}
}
return overridingMethods;
}
use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.
the class RenameGrFieldProcessor method findCollisions.
@Override
public void findCollisions(PsiElement element, String newName, Map<? extends PsiElement, String> allRenames, List<UsageInfo> result) {
List<UsageInfo> collisions = new ArrayList<>();
for (UsageInfo info : result) {
if (!(info instanceof MoveRenameUsageInfo))
continue;
final PsiElement infoElement = info.getElement();
final PsiElement referencedElement = ((MoveRenameUsageInfo) info).getReferencedElement();
if (!(infoElement instanceof GrReferenceExpression))
continue;
final GrReferenceExpression refExpr = (GrReferenceExpression) infoElement;
if (!(referencedElement instanceof GrField || refExpr.advancedResolve().isInvokedOnProperty()))
continue;
if (!(refExpr.getParent() instanceof GrCall))
continue;
final PsiType[] argTypes = PsiUtil.getArgumentTypes(refExpr, false);
final PsiType[] typeArguments = refExpr.getTypeArguments();
final MethodResolverProcessor processor = new MethodResolverProcessor(newName, refExpr, false, null, argTypes, typeArguments);
final PsiMethod resolved = ResolveUtil.resolveExistingElement(refExpr, processor, PsiMethod.class);
if (resolved == null)
continue;
collisions.add(new UnresolvableCollisionUsageInfo(resolved, refExpr) {
@Override
public String getDescription() {
return GroovyRefactoringBundle.message("usage.will.be.overriden.by.method", refExpr.getParent().getText(), PsiFormatUtil.formatMethod(resolved, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_NAME, PsiFormatUtilBase.SHOW_TYPE));
}
});
}
result.addAll(collisions);
super.findCollisions(element, newName, allRenames, result);
}
use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.
the class BaseRefactoringProcessor method createPresentation.
@NotNull
private static UsageViewPresentation createPresentation(@NotNull UsageViewDescriptor descriptor, @NotNull Usage[] usages) {
UsageViewPresentation presentation = new UsageViewPresentation();
presentation.setTabText(RefactoringBundle.message("usageView.tabText"));
presentation.setTargetsNodeText(descriptor.getProcessedElementsHeader());
presentation.setShowReadOnlyStatusAsRed(true);
presentation.setShowCancelButton(true);
presentation.setUsagesString(RefactoringBundle.message("usageView.usagesText"));
int codeUsageCount = 0;
int nonCodeUsageCount = 0;
int dynamicUsagesCount = 0;
Set<PsiFile> codeFiles = new HashSet<>();
Set<PsiFile> nonCodeFiles = new HashSet<>();
Set<PsiFile> dynamicUsagesCodeFiles = new HashSet<>();
for (Usage usage : usages) {
if (usage instanceof PsiElementUsage) {
final PsiElementUsage elementUsage = (PsiElementUsage) usage;
final PsiElement element = elementUsage.getElement();
if (element == null)
continue;
final PsiFile containingFile = element.getContainingFile();
if (elementUsage.isNonCodeUsage()) {
nonCodeUsageCount++;
nonCodeFiles.add(containingFile);
} else {
codeUsageCount++;
codeFiles.add(containingFile);
}
if (usage instanceof UsageInfo2UsageAdapter) {
final UsageInfo usageInfo = ((UsageInfo2UsageAdapter) usage).getUsageInfo();
if (usageInfo instanceof MoveRenameUsageInfo && usageInfo.isDynamicUsage()) {
dynamicUsagesCount++;
dynamicUsagesCodeFiles.add(containingFile);
}
}
}
}
codeFiles.remove(null);
nonCodeFiles.remove(null);
dynamicUsagesCodeFiles.remove(null);
String codeReferencesText = descriptor.getCodeReferencesText(codeUsageCount, codeFiles.size());
presentation.setCodeUsagesString(codeReferencesText);
final String commentReferencesText = descriptor.getCommentReferencesText(nonCodeUsageCount, nonCodeFiles.size());
if (commentReferencesText != null) {
presentation.setNonCodeUsagesString(commentReferencesText);
}
presentation.setDynamicUsagesString("Dynamic " + StringUtil.decapitalize(descriptor.getCodeReferencesText(dynamicUsagesCount, dynamicUsagesCodeFiles.size())));
String generatedCodeString;
if (codeReferencesText.contains("in code")) {
generatedCodeString = StringUtil.replace(codeReferencesText, "in code", "in generated code");
} else {
generatedCodeString = codeReferencesText + " in generated code";
}
presentation.setUsagesInGeneratedCodeString(generatedCodeString);
return presentation;
}
use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.
the class CommonMoveUtil method retargetUsages.
public static NonCodeUsageInfo[] retargetUsages(final UsageInfo[] usages, final Map<PsiElement, PsiElement> oldToNewElementsMapping) throws IncorrectOperationException {
Arrays.sort(usages, (o1, o2) -> {
final VirtualFile file1 = o1.getVirtualFile();
final VirtualFile file2 = o2.getVirtualFile();
if (Comparing.equal(file1, file2)) {
final ProperTextRange rangeInElement1 = o1.getRangeInElement();
final ProperTextRange rangeInElement2 = o2.getRangeInElement();
if (rangeInElement1 != null && rangeInElement2 != null) {
return rangeInElement2.getStartOffset() - rangeInElement1.getStartOffset();
}
return 0;
}
if (file1 == null)
return -1;
if (file2 == null)
return 1;
return Comparing.compare(file1.getPath(), file2.getPath());
});
List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<>();
for (UsageInfo usage : usages) {
if (usage instanceof NonCodeUsageInfo) {
nonCodeUsages.add((NonCodeUsageInfo) usage);
} else if (usage instanceof MoveRenameUsageInfo) {
final MoveRenameUsageInfo moveRenameUsage = (MoveRenameUsageInfo) usage;
final PsiElement oldElement = moveRenameUsage.getReferencedElement();
final PsiElement newElement = oldToNewElementsMapping.get(oldElement);
LOG.assertTrue(newElement != null, oldElement);
final PsiReference reference = moveRenameUsage.getReference();
if (reference != null) {
try {
reference.bindToElement(newElement);
} catch (IncorrectOperationException e) {
//
}
}
}
}
return nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]);
}
use of com.intellij.refactoring.util.MoveRenameUsageInfo in project intellij-community by JetBrains.
the class TurnRefsToSuperProcessorBase method performVariablesRenaming.
protected void performVariablesRenaming() {
try {
//forget about smart pointers
Map<PsiElement, String> variableRenames = new HashMap<>();
for (Map.Entry<SmartPsiElementPointer, String> 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 (reference != null) {
reference.handleElementRename(newName);
}
}
}
for (Map.Entry<SmartPsiElementPointer, String> entry : myVariablesRenames.entrySet()) {
final String newName = entry.getValue();
if (newName != null) {
final PsiVariable variable = (PsiVariable) entry.getKey().getElement();
variable.setName(newName);
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
Aggregations