diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corext/refactoring/rename/RenameTypeProcessor.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corext/refactoring/rename/RenameTypeProcessor.java index 6e10111b7e..6646b65bb8 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corext/refactoring/rename/RenameTypeProcessor.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corext/refactoring/rename/RenameTypeProcessor.java @@ -1248,8 +1248,14 @@ private void addConstructorRenames(TextChangeManager manager) throws CoreExcepti * if (methods[i].getNameRange() == null), then it's a binary file so it's wrong anyway * (checked as a precondition) */ - String name = RefactoringCoreMessages.RenameTypeRefactoring_rename_constructor; - TextChangeCompatibility.addTextEdit(manager.get(cu), name, new ReplaceEdit(methods[i].getNameRange().getOffset(), typeNameLength, getNewElementName())); + String methodName = methods[i].getElementName(); + ISourceRange range = methods[i].getNameRange(); + String sourceName = cu.getBuffer().getText(range.getOffset(), range.getLength()); + boolean isValid = methodName.equals(sourceName); + if (isValid) { + String name = RefactoringCoreMessages.RenameTypeRefactoring_rename_constructor; + TextChangeCompatibility.addTextEdit(manager.get(cu), name, new ReplaceEdit(methods[i].getNameRange().getOffset(), typeNameLength, getNewElementName())); + } } } } diff --git a/org.eclipse.jdt.ls.tests/projects/maven/mavenlombok/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/mavenlombok/pom.xml new file mode 100644 index 0000000000..91c7a6dc4c --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/mavenlombok/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + org.sample + mavenlombok + jar + 1.0-SNAPSHOT + + + + org.projectlombok + lombok + 1.18.20 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + 11 + 11 + + + + + diff --git a/org.eclipse.jdt.ls.tests/projects/maven/mavenlombok/src/main/java/org/sample/Test.java b/org.eclipse.jdt.ls.tests/projects/maven/mavenlombok/src/main/java/org/sample/Test.java new file mode 100644 index 0000000000..2ef7223022 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/mavenlombok/src/main/java/org/sample/Test.java @@ -0,0 +1,9 @@ +package org.sample; + +import lombok.Data; + +@Data +public class Test { + private String name; + private int id; +} diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/RenameHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/RenameHandlerTest.java index 470daaabdd..8219d75af6 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/RenameHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/RenameHandlerTest.java @@ -15,16 +15,21 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.LinkedList; import java.util.List; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.ls.core.internal.JDTUtils; import org.eclipse.jdt.ls.core.internal.ResourceUtils; @@ -562,6 +567,25 @@ public void testRenameType() throws JavaModelException, BadLocationException { ); } + // this test should pass when starting with -javaagent: (-javagent:~/.m2/repository/org/projectlombok/lombok/1.18.20/lombok-1.18.20.jar) + // https://github.com/eclipse/eclipse.jdt.ls/issues/1775 + @Test + public void testRenameTypeLombok() throws Exception { + when(preferenceManager.getPreferences().isImportMavenEnabled()).thenReturn(true); + importProjects("maven/mavenlombok"); + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("mavenlombok"); + IFile file = project.getFile("src/main/java/org/sample/Test.java"); + assertTrue(file.exists()); + ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file); + Position pos = new Position(5, 15); + String source = cu.getSource(); + String expected = source.replace("Test", "Test1"); + WorkspaceEdit edit = getRenameEdit(cu, pos, "Test1"); + assertNotNull(edit); + assertEquals(edit.getChanges().size(), 1); + assertEquals(expected, TextEditUtil.apply(source, edit.getChanges().get(JDTUtils.toURI(cu)))); + } + @Test public void testRenameSuperMethod() throws JavaModelException, BadLocationException { IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);