Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support inlining in eclipse #3562

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public String mapResourceName(int classFileFormatVersion, String resourceName) {
patchEcjTransformers(sm);
patchExtensionMethod(sm);
patchRenameField(sm);
patchInline(sm);
patchNullCheck(sm);

patchForTests(sm);
Expand Down Expand Up @@ -218,6 +219,15 @@ private static void patchExtractInterfaceAndPullUp(ScriptManager sm) {
.build());
}

private static void patchInline(ScriptManager sm) {
sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.wrapReturnValue()
.target(new MethodTarget("org.eclipse.jdt.internal.corext.refactoring.code.SourceProvider", "getCodeBlocks", "java.lang.String[]", "org.eclipse.jdt.internal.corext.refactoring.code.CallContext", "org.eclipse.jdt.core.dom.rewrite.ImportRewrite"))
.wrapMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "getRealCodeBlocks", "java.lang.String[]", "java.lang.String[]", "org.eclipse.jdt.internal.corext.refactoring.code.SourceProvider", "org.eclipse.jdt.internal.corext.refactoring.code.CallContext"))
.request(StackRequest.RETURN_VALUE, StackRequest.THIS, StackRequest.PARAM1)
.transplant()
.build());
}

private static void patchAboutDialog(ScriptManager sm) {
/*
* Add a line about lombok (+ version info) to eclipse's about dialog.
Expand Down Expand Up @@ -1023,7 +1033,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
42 changes: 42 additions & 0 deletions src/eclipseAgent/lombok/launch/PatchFixesHider.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Type;
Expand All @@ -61,6 +65,8 @@
import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEvent;
import org.eclipse.jdt.internal.core.dom.rewrite.TokenScanner;
import org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup;
import org.eclipse.jdt.internal.corext.refactoring.code.CallContext;
import org.eclipse.jdt.internal.corext.refactoring.code.SourceProvider;
import org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.IncomingMemberVisibilityAdjustment;

import lombok.permit.Permit;
Expand Down Expand Up @@ -902,6 +908,42 @@ public static java.lang.String getRealNodeSource(java.lang.String original, org.
public static boolean skipRewriteVisibility(IncomingMemberVisibilityAdjustment adjustment) {
return isGenerated(adjustment.getMember());
}

public static String[] getRealCodeBlocks(String[] blocks, SourceProvider sourceProvider, CallContext callContext) {
MethodDeclaration methodDeclaration = sourceProvider.getDeclaration();
if (!isGenerated(methodDeclaration)) {
return blocks;
}

try {
// Replace parameter references with actual argument
AST ast = methodDeclaration.getAST();
List<?> parameters = methodDeclaration.parameters();
for (int i = 0; i < parameters.size(); i++) {
SingleVariableDeclaration param = (SingleVariableDeclaration) parameters.get(i);
Object data = param.getProperty("org.eclipse.jdt.internal.corext.refactoring.code.ParameterData");
List<SimpleName> names = Permit.get(Permit.permissiveGetField(data.getClass(), "fReferences"), data);

for (SimpleName simpleName : names) {
ASTNode copy = ASTNode.copySubtree(ast, callContext.arguments[i]);
simpleName.getParent().setStructuralProperty(simpleName.getLocationInParent(), copy);
}
}
// Convert AST to source
StringBuilder sb = new StringBuilder();
for (Object statement : methodDeclaration.getBody().statements()) {
if (callContext.callMode != ASTNode.RETURN_STATEMENT && statement instanceof ReturnStatement) {
ReturnStatement returnStatement = (ReturnStatement) statement;
sb.append(returnStatement.getExpression());
} else {
sb.append(statement);
}
}
return new String[] {sb.toString().trim()};
} catch (Throwable e) {
return blocks;
}
}
}

public static class Tests {
Expand Down
16 changes: 16 additions & 0 deletions test/eclipse/resource/inline/getter/after/InlineGetter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pkg;

import lombok.Getter;

@Getter
public class InlineGetter {
private String string;

public String asReturn() {
return this.string;
}

public void asAssignment() {
String a = this.string;
}
}
16 changes: 16 additions & 0 deletions test/eclipse/resource/inline/getter/before/InlineGetter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pkg;

import lombok.Getter;

@Getter
public class InlineGetter {
private String string;

public String asReturn() {
return getString();
}

public void asAssignment() {
String a = getString();
}
}
12 changes: 12 additions & 0 deletions test/eclipse/resource/inline/setter/after/InlineSetter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pkg;

import lombok.Setter;

@Setter
public class InlineSetter {
private String string;

public void test() {
this.string="test";
}
}
12 changes: 12 additions & 0 deletions test/eclipse/resource/inline/setter/before/InlineSetter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pkg;

import lombok.Setter;

@Setter
public class InlineSetter {
private String string;

public void test() {
setString("test");
}
}
3 changes: 2 additions & 1 deletion test/eclipse/src/lombok/eclipse/EclipseTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import lombok.eclipse.cleanup.CleanupTest;
import lombok.eclipse.edit.SelectTest;
import lombok.eclipse.refactoring.ExtractInterfaceTest;
import lombok.eclipse.refactoring.InlineTest;
import lombok.eclipse.refactoring.RenameTest;
import lombok.eclipse.references.FindReferencesTest;

@RunWith(Suite.class)
@SuiteClasses({ExtractInterfaceTest.class, RenameTest.class, SelectTest.class, CleanupTest.class, FindReferencesTest.class})
@SuiteClasses({ExtractInterfaceTest.class, RenameTest.class, SelectTest.class, CleanupTest.class, FindReferencesTest.class, InlineTest.class})
public class EclipseTests {

}
41 changes: 41 additions & 0 deletions test/eclipse/src/lombok/eclipse/refactoring/InlineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package lombok.eclipse.refactoring;

import static lombok.eclipse.RefactoringUtils.performRefactoring;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.internal.corext.refactoring.code.InlineMethodRefactoring;
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import lombok.eclipse.EclipseRunner;
import lombok.eclipse.SetupBeforeAfterTest;

@RunWith(EclipseRunner.class)
public class InlineTest {

@Rule
public SetupBeforeAfterTest setup = new SetupBeforeAfterTest();

@Test
public void getter() throws Exception {
ICompilationUnit cu = setup.getPackageFragment().getCompilationUnit("InlineGetter.java");

CompilationUnit compilationUnit = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(cu, true);

performRefactoring(InlineMethodRefactoring.create(cu, compilationUnit, 139, 0));
performRefactoring(InlineMethodRefactoring.create(cu, compilationUnit, 200, 0));
}

@Test
public void setter() throws Exception {
ICompilationUnit cu = setup.getPackageFragment().getCompilationUnit("InlineSetter.java");

CompilationUnit compilationUnit = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(cu, true);

performRefactoring(InlineMethodRefactoring.create(cu, compilationUnit, 126, 0));
}
}