Skip to content

Commit

Permalink
[fixes projectlombok#1758] Remove generated nodes in search results
Browse files Browse the repository at this point in the history
Lombok can now handle generated methods in nested classes.
  • Loading branch information
Rawi01 committed Nov 19, 2023
1 parent 6fbe057 commit a4311b4
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 31 deletions.
3 changes: 3 additions & 0 deletions src/core/lombok/eclipse/TransformEclipseAST.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ public static void transform(Parser parser, CompilationUnitDeclaration ast) {
if (fileName != null && String.valueOf(fileName).endsWith("module-info.java")) return;

if (Symbols.hasSymbol("lombok.disable")) return;
// The IndexingParser only supports a single import statement, restricting lombok annotations to either fully qualified ones or
// those specified in the last import statement. To avoid handling hard to reproduce edge cases, we opt to ignore the entire parser.
if ("org.eclipse.jdt.internal.core.search.indexing.IndexingParser".equals(parser.getClass().getName())) return;
if (alreadyTransformed(ast)) return;

// Do NOT abort if (ast.bits & ASTNode.HasAllMethodBodies) != 0 - that doesn't work.
Expand Down
18 changes: 10 additions & 8 deletions src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,16 @@ private static void patchRenameField(ScriptManager sm) {
.methodToWrap(new Hook("org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine", "search", "org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup[]", "org.eclipse.jdt.core.search.SearchPattern","org.eclipse.jdt.core.search.IJavaSearchScope","org.eclipse.core.runtime.IProgressMonitor","org.eclipse.ltk.core.refactoring.RefactoringStatus"))
.wrapMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "createFakeSearchResult", "org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup[]", "org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup[]", "java.lang.Object"))
.requestExtra(StackRequest.THIS)
.transplant().build());
.transplant()
.build());

/* Filter search results which are Generated and based on Fields, e.g. Generated getters/setters */
sm.addScript(ScriptBuilder.wrapMethodCall()
.target(new MethodTarget("org.eclipse.jdt.internal.corext.refactoring.rename.RenameFieldProcessor", "addAccessorOccurrences", "void", "org.eclipse.core.runtime.IProgressMonitor", "org.eclipse.jdt.core.IMethod", "java.lang.String","java.lang.String","org.eclipse.ltk.core.refactoring.RefactoringStatus"))
.methodToWrap(new Hook("org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup", "getSearchResults", "org.eclipse.jdt.core.search.SearchMatch[]"))
.wrapMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "removeGenerated", "org.eclipse.jdt.core.search.SearchMatch[]", "org.eclipse.jdt.core.search.SearchMatch[]"))
.transplant().build());
/* Prevent adding generated nodes as possible search matches */
sm.addScript(ScriptBuilder.exitEarly()
.target(new MethodTarget("org.eclipse.jdt.internal.core.search.matching.MatchingNodeSet", "addPossibleMatch", "void", "org.eclipse.jdt.internal.compiler.ast.ASTNode"))
.decisionMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "isGenerated", "boolean", "org.eclipse.jdt.internal.compiler.ast.ASTNode"))
.request(StackRequest.PARAM1)
.transplant()
.build());
}

private static void patchExtractInterfaceAndPullUp(ScriptManager sm) {
Expand Down Expand Up @@ -1023,7 +1025,7 @@ private static void patchASTNodeSearchUtil(ScriptManager sm) {
}

private static void patchForTests(ScriptManager sm) {
sm.addScriptIfWitness(new String[] {"lombok/eclipse/EclipseTests"}, ScriptBuilder.wrapReturnValue()
sm.addScriptIfWitness(new String[] {"lombok/eclipse/EclipseRunner"}, ScriptBuilder.wrapReturnValue()
.target(new MethodTarget("org.osgi.framework.FrameworkUtil", "getBundle", "org.osgi.framework.Bundle", "java.lang.Class"))
.request(StackRequest.RETURN_VALUE, StackRequest.PARAM1)
.wrapMethod(new Hook("lombok.launch.PatchFixesHider$Tests", "getBundle", "java.lang.Object", "java.lang.Object", "java.lang.Class"))
Expand Down
22 changes: 0 additions & 22 deletions src/eclipseAgent/lombok/launch/PatchFixesHider.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IAnnotatable;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
Expand Down Expand Up @@ -772,27 +771,6 @@ public static IMethod[] removeGeneratedMethods(IMethod[] methods) throws Excepti
return result.size() == methods.length ? methods : result.toArray(new IMethod[0]);
}

public static SearchMatch[] removeGenerated(SearchMatch[] returnValue) {
List<SearchMatch> result = new ArrayList<SearchMatch>();
for (int j = 0; j < returnValue.length; j++) {
SearchMatch searchResult = returnValue[j];
if (searchResult.getElement() instanceof IField) {
IField field = (IField) searchResult.getElement();

// can not check for value=lombok because annotation is
// not fully resolved
IAnnotation annotation = field.getAnnotation("Generated");
if (annotation != null) {
// Method generated at field location, skip
continue;
}

}
result.add(searchResult);
}
return result.toArray(new SearchMatch[0]);
}

public static SearchResultGroup[] createFakeSearchResult(SearchResultGroup[] returnValue,
Object/*
* org.eclipse.jdt.internal.corext.refactoring.rename.
Expand Down
12 changes: 12 additions & 0 deletions test/eclipse/resource/rename/data/after/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pkg;

import lombok.Data;

@Data
public class A {
private String newString;

public String test() {
return getNewString();
}
}
12 changes: 12 additions & 0 deletions test/eclipse/resource/rename/data/before/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pkg;

import lombok.Data;

@Data
public class A {
private String string;

public String test() {
return getString();
}
}
9 changes: 9 additions & 0 deletions test/eclipse/resource/rename/nestedClass/after/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package pkg;

public class A {
@lombok.Getter
@lombok.Setter
public static class Nested {
private String newString;
}
}
9 changes: 9 additions & 0 deletions test/eclipse/resource/rename/nestedClass/before/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package pkg;

public class A {
@lombok.Getter
@lombok.Setter
public static class Nested {
private String string;
}
}
33 changes: 32 additions & 1 deletion test/eclipse/src/lombok/eclipse/refactoring/RenameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class RenameTest {

@Rule
public SetupBeforeAfterTest setup = new SetupBeforeAfterTest();

//
@Test
public void simple() throws Exception {
ICompilationUnit cu = setup.getPackageFragment().getCompilationUnit("A.java");
Expand All @@ -30,6 +30,8 @@ public void simple() throws Exception {

RenameFieldProcessor renameFieldProcessor = new RenameFieldProcessor(field);
renameFieldProcessor.setNewElementName("newString");
renameFieldProcessor.setRenameGetter(true);
renameFieldProcessor.setRenameSetter(true);

performRefactoring(renameFieldProcessor);
}
Expand Down Expand Up @@ -83,4 +85,33 @@ public void extensionMethod() throws Exception {

performRefactoring(renameMethodProcessor);
}

@Test
public void data() throws Exception {
ICompilationUnit cu = setup.getPackageFragment().getCompilationUnit("A.java");
IType type = cu.findPrimaryType();
IField field = type.getField("string");

RenameFieldProcessor renameFieldProcessor = new RenameFieldProcessor(field);
renameFieldProcessor.setNewElementName("newString");
renameFieldProcessor.setRenameGetter(true);
renameFieldProcessor.setRenameSetter(true);

performRefactoring(renameFieldProcessor);
}

@Test
public void nestedClass() throws Exception {
ICompilationUnit cu = setup.getPackageFragment().getCompilationUnit("A.java");
IType type = cu.findPrimaryType();
IType nestedType = type.getType("Nested");
IField field = nestedType.getField("string");

RenameFieldProcessor renameFieldProcessor = new RenameFieldProcessor(field);
renameFieldProcessor.setNewElementName("newString");
renameFieldProcessor.setRenameGetter(true);
renameFieldProcessor.setRenameSetter(true);

performRefactoring(renameFieldProcessor);
}
}

0 comments on commit a4311b4

Please sign in to comment.