diff --git a/platform/lang-impl/src/com/intellij/refactoring/rename/RenameUtil.java b/platform/lang-impl/src/com/intellij/refactoring/rename/RenameUtil.java index 9d18e8486329..65edbd7699af 100644 --- a/platform/lang-impl/src/com/intellij/refactoring/rename/RenameUtil.java +++ b/platform/lang-impl/src/com/intellij/refactoring/rename/RenameUtil.java @@ -54,7 +54,7 @@ public class RenameUtil { @NotNull public static UsageInfo[] findUsages(final PsiElement element, - String newName, + final String newName, boolean searchInStringsAndComments, boolean searchForTextOccurrences, Map allRenames) { @@ -65,7 +65,7 @@ public class RenameUtil { RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(element); Collection refs = processor.findReferences(element); - for (PsiReference ref : refs) { + for (final PsiReference ref : refs) { if (ref == null) { LOG.error("null reference from processor " + processor); continue; @@ -74,6 +74,14 @@ public class RenameUtil { result.add(new MoveRenameUsageInfo(referenceElement, ref, ref.getRangeInElement().getStartOffset(), ref.getRangeInElement().getEndOffset(), element, ref.resolve() == null)); + if (!isValidName(element.getProject(), ref.getElement(), newName)) { + result.add(new UnresolvableCollisionUsageInfo(ref.getElement(), element) { + @Override + public String getDescription() { + return RefactoringBundle.message("0.is.not.an.identifier", newName, ref.getElement().getText()); + } + }); + } } processor.findCollisions(element, newName, allRenames, result); diff --git a/platform/platform-resources-en/src/messages/RefactoringBundle.properties b/platform/platform-resources-en/src/messages/RefactoringBundle.properties index 3eb998446656..dc13df3beed0 100644 --- a/platform/platform-resources-en/src/messages/RefactoringBundle.properties +++ b/platform/platform-resources-en/src/messages/RefactoringBundle.properties @@ -743,4 +743,5 @@ change.signature.leave.blank.default.value=Leave &blank change.signature.feel.lucky=Feel &lucky change.signature.use.selected.value=Use selected &value -search.for.references=Search for re&ferences \ No newline at end of file +search.for.references=Search for re&ferences +0.is.not.an.identifier={0} is not a correct identifier to use in {1} \ No newline at end of file diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/rename/RenameTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/rename/RenameTest.groovy index c758b8d07696..f95641c24e28 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/rename/RenameTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/refactoring/rename/RenameTest.groovy @@ -7,6 +7,7 @@ import com.intellij.psi.PsiMethod import com.intellij.psi.PsiReference import com.intellij.psi.impl.source.PostprocessReformattingAspect import com.intellij.psi.util.PsiTreeUtil +import com.intellij.refactoring.BaseRefactoringProcessor.ConflictsInTestsException import com.intellij.refactoring.rename.RenameProcessor import com.intellij.refactoring.rename.RenameUtil import com.intellij.refactoring.rename.inplace.VariableInplaceRenameHandler @@ -212,7 +213,7 @@ class SomeBean { """, file.text } - public void doTest() throws Throwable { + public void doTest() { final String testFile = getTestName(true).replace('$', '/') + ".test"; final List list = TestUtils.readInput(TestUtils.getAbsoluteTestDataPath() + "groovy/refactoring/rename/" + testFile); @@ -402,6 +403,26 @@ foo = 4""" assert "gpp", myFixture.file.virtualFile.extension } + public void testRenameJavaUsageFail() { + myFixture.addFileToProject "Bar.java", """ +class Bar { + void bar() { + new Foo().foo(); + } +}""" + myFixture.configureByText "Foo.groovy", """ +class Foo { + def foo() {} +}""" + try { + myFixture.renameElement myFixture.findClass("Foo").getMethods()[0], "'newName'" + } catch (ConflictsInTestsException e) { + assertEquals "'newName' is not a correct identifier to use in new Foo().foo", e.getMessage() + return; + } + assertTrue false + } + private def doInplaceRenameTest() { String prefix = TestUtils.getTestDataPath() + "groovy/refactoring/rename/" + getTestName(false) myFixture.configureByFile prefix + ".groovy";