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;
}
});
}
}
}
}
}
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()]);
}
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()]);
}
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;
}
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();
}
}
}
Aggregations