mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 22:17:41 +00:00
[fixes #3373] Find references for extension methods
This commit is contained in:
1 parent
d56b576c26
commit
afceb138e6
13 files changed
+184
-2
No files matched your search
@@ -0,0 +1,15 @@
|
||||
package pkg;
|
||||
|
||||
public static class Extension {
|
||||
public static String test(String s) {
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String test(String s, int i) {
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String test(String s, String... s2) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package pkg;
|
||||
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
|
||||
@ExtensionMethod(Extension.class)
|
||||
public class Usage {
|
||||
public void test() {
|
||||
private String string;
|
||||
string.test();
|
||||
string.test("a");
|
||||
string.test(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package pkg;
|
||||
|
||||
public static class Extension {
|
||||
public static String newTest(String s) {
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String test(String s, int i) {
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String test(String s, String... s2) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package pkg;
|
||||
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
|
||||
@ExtensionMethod(Extension.class)
|
||||
public class Usage {
|
||||
public void test() {
|
||||
private String string;
|
||||
string.newTest();
|
||||
string.test("a");
|
||||
string.test(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package pkg;
|
||||
|
||||
public static class Extension {
|
||||
public static String test(String s) {
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String test(String s, int i) {
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String test(String s, String... s2) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package pkg;
|
||||
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
|
||||
@ExtensionMethod(Extension.class)
|
||||
public class Usage {
|
||||
public void test() {
|
||||
private String string;
|
||||
string.test();
|
||||
string.test("a");
|
||||
string.test(1);
|
||||
}
|
||||
}
|
||||
@@ -8,9 +8,10 @@ import lombok.eclipse.cleanup.CleanupTest;
|
||||
import lombok.eclipse.edit.SelectTest;
|
||||
import lombok.eclipse.refactoring.ExtractInterfaceTest;
|
||||
import lombok.eclipse.refactoring.RenameTest;
|
||||
import lombok.eclipse.references.FindReferencesTest;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({ExtractInterfaceTest.class, RenameTest.class, SelectTest.class, CleanupTest.class})
|
||||
@SuiteClasses({ExtractInterfaceTest.class, RenameTest.class, SelectTest.class, CleanupTest.class, FindReferencesTest.class})
|
||||
public class EclipseTests {
|
||||
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public class SetupBeforeAfterTest extends SetupTest {
|
||||
protected void succeeded(Description description) {
|
||||
try {
|
||||
compareWithAfter();
|
||||
} catch (Throwable e) {
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,11 @@ import static lombok.eclipse.RefactoringUtils.performRefactoring;
|
||||
|
||||
import org.eclipse.jdt.core.ICompilationUnit;
|
||||
import org.eclipse.jdt.core.IField;
|
||||
import org.eclipse.jdt.core.IMethod;
|
||||
import org.eclipse.jdt.core.IType;
|
||||
import org.eclipse.jdt.internal.corext.refactoring.rename.RenameFieldProcessor;
|
||||
import org.eclipse.jdt.internal.corext.refactoring.rename.RenameMethodProcessor;
|
||||
import org.eclipse.jdt.internal.corext.refactoring.rename.RenameNonVirtualMethodProcessor;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -68,4 +71,16 @@ public class RenameTest {
|
||||
|
||||
performRefactoring(renameFieldProcessor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extensionMethod() throws Exception {
|
||||
ICompilationUnit cu = setup.getPackageFragment().getCompilationUnit("Extension.java");
|
||||
IType type = cu.findPrimaryType();
|
||||
IMethod method = type.getMethods()[0];
|
||||
|
||||
RenameMethodProcessor renameMethodProcessor = new RenameNonVirtualMethodProcessor(method);
|
||||
renameMethodProcessor.setNewElementName("newTest");
|
||||
|
||||
performRefactoring(renameMethodProcessor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package lombok.eclipse.references;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.jdt.core.ICompilationUnit;
|
||||
import org.eclipse.jdt.core.IJavaElement;
|
||||
import org.eclipse.jdt.core.IType;
|
||||
import org.eclipse.jdt.core.JavaModelException;
|
||||
import org.eclipse.jdt.core.search.IJavaSearchConstants;
|
||||
import org.eclipse.jdt.core.search.SearchEngine;
|
||||
import org.eclipse.jdt.core.search.SearchMatch;
|
||||
import org.eclipse.jdt.core.search.SearchParticipant;
|
||||
import org.eclipse.jdt.core.search.SearchPattern;
|
||||
import org.eclipse.jdt.internal.corext.refactoring.CollectingSearchRequestor;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import lombok.eclipse.EclipseRunner;
|
||||
import lombok.eclipse.SetupSingleFileTest;
|
||||
|
||||
@RunWith(EclipseRunner.class)
|
||||
public class FindReferencesTest {
|
||||
|
||||
@Rule
|
||||
public SetupSingleFileTest setup = new SetupSingleFileTest();
|
||||
|
||||
@Test
|
||||
public void extensionMethod() throws Exception {
|
||||
ICompilationUnit extensionCu = setup.getPackageFragment().getCompilationUnit("Extension.java");
|
||||
IType type = extensionCu.findPrimaryType();
|
||||
List<SearchMatch> firstResult = searchInProject(type.getMethods()[0]);
|
||||
assertEquals(firstResult.size(), 2);
|
||||
|
||||
ICompilationUnit usageCu = setup.getPackageFragment().getCompilationUnit("Usage.java");
|
||||
List<SearchMatch> secondResult = searchInProject(usageCu.codeSelect(170, 0)[0]);
|
||||
assertEquals(secondResult.size(), 2);
|
||||
}
|
||||
|
||||
private List<SearchMatch> searchInProject(IJavaElement element) throws CoreException, JavaModelException {
|
||||
CollectingSearchRequestor requestor = new CollectingSearchRequestor();
|
||||
SearchEngine engine = new SearchEngine();
|
||||
engine.search(
|
||||
SearchPattern.createPattern(element, IJavaSearchConstants.ALL_OCCURRENCES, SearchPattern.R_EXACT_MATCH),
|
||||
new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
|
||||
SearchEngine.createJavaSearchScope(new IJavaElement[] { setup.getJavaProject() }),
|
||||
requestor,
|
||||
null
|
||||
);
|
||||
|
||||
return requestor.getResults();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user