Do not reuse replaced (thus invalid) PSI element in PyMoveFileHandler#retargetUsages

Also added several @Nullable/@NotNull annotations in
PyClassRefactoringUtil.
This commit is contained in:
Mikhail Golubev
2014-11-17 15:47:44 +03:00
parent b7545f79ac
commit 309343fb0d
2 changed files with 12 additions and 8 deletions
@@ -289,15 +289,18 @@ public final class PyClassRefactoringUtil {
return true;
}
public static boolean insertImport(PsiElement anchor, PsiNamedElement element) {
public static boolean insertImport(@NotNull PsiElement anchor, @NotNull PsiNamedElement element) {
return insertImport(anchor, element, null);
}
public static boolean insertImport(PsiElement anchor, PsiNamedElement element, @Nullable String asName) {
public static boolean insertImport(@NotNull PsiElement anchor, @NotNull PsiNamedElement element, @Nullable String asName) {
return insertImport(anchor, element, asName, PyCodeInsightSettings.getInstance().PREFER_FROM_IMPORT);
}
public static boolean insertImport(PsiElement anchor, PsiNamedElement element, @Nullable String asName, boolean preferFromImport) {
public static boolean insertImport(@NotNull PsiElement anchor,
@NotNull PsiNamedElement element,
@Nullable String asName,
boolean preferFromImport) {
if (PyBuiltinCache.getInstance(element).isBuiltin(element)) return false;
final PsiFile newFile = element.getContainingFile();
final PsiFile file = anchor.getContainingFile();
@@ -128,8 +128,8 @@ public class PyMoveFileHandler extends MoveFileHandler {
replaceWithQualifiedExpression(element, newQualifiedName);
} else {
final QualifiedName newName = QualifiedName.fromComponents(PyClassRefactoringUtil.getOriginalName(newElement));
replaceWithQualifiedExpression(element, newName);
PyClassRefactoringUtil.insertImport(element, newElement, null);
final PsiElement replaced = replaceWithQualifiedExpression(element, newName);
PyClassRefactoringUtil.insertImport(replaced, newElement, null);
}
}
}
@@ -145,15 +145,16 @@ public class PyMoveFileHandler extends MoveFileHandler {
}
}
private static void replaceWithQualifiedExpression(@NotNull PsiElement oldElement,
@Nullable QualifiedName newElementName) {
@NotNull
private static PsiElement replaceWithQualifiedExpression(@NotNull PsiElement oldElement, @Nullable QualifiedName newElementName) {
if (newElementName != null && PyClassRefactoringUtil.isValidQualifiedName(newElementName)) {
final PyElementGenerator generator = PyElementGenerator.getInstance(oldElement.getProject());
final PsiElement newElement = generator.createExpressionFromText(LanguageLevel.forElement(oldElement), newElementName.toString());
if (newElement != null) {
oldElement.replace(newElement);
return oldElement.replace(newElement);
}
}
return oldElement;
}
@Override