Skip to content

Commit

Permalink
Fix renaming java file with lombok (eclipse-jdtls#1779)
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <snjezana.peco@redhat.com>
  • Loading branch information
snjeza authored May 26, 2021
1 parent 3b0045a commit d2c1b13
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/maven/mavenlombok/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>mavenlombok</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sample;

import lombok.Data;

@Data
public class Test {
private String name;
private int id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -562,6 +567,25 @@ public void testRenameType() throws JavaModelException, BadLocationException {
);
}

// this test should pass when starting with -javaagent:<lombok_jar> (-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);
Expand Down

0 comments on commit d2c1b13

Please sign in to comment.