mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 06:17:44 +00:00
[fixes #562] Support inlining in eclipse
This commit is contained in:
1 parent
6fbe057fb9
commit
3c80b44d9b
8 files changed
+150
-2
No files matched your search
@@ -107,6 +107,7 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable {
|
||||
patchEcjTransformers(sm);
|
||||
patchExtensionMethod(sm);
|
||||
patchRenameField(sm);
|
||||
patchInline(sm);
|
||||
patchNullCheck(sm);
|
||||
|
||||
patchForTests(sm);
|
||||
@@ -218,6 +219,15 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable {
|
||||
.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.
|
||||
@@ -1023,7 +1033,7 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable {
|
||||
}
|
||||
|
||||
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"))
|
||||
|
||||
@@ -43,7 +43,11 @@ import org.eclipse.jdt.core.IJavaElement;
|
||||
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;
|
||||
@@ -61,6 +65,8 @@ import org.eclipse.jdt.internal.core.dom.rewrite.NodeRewriteEvent;
|
||||
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;
|
||||
@@ -902,6 +908,40 @@ final class PatchFixesHider {
|
||||
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();
|
||||
for (Object param : methodDeclaration.parameters()) {
|
||||
Object data = ((SingleVariableDeclaration)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[0]);
|
||||
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 {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package pkg;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
@Setter
|
||||
public class InlineSetter {
|
||||
private String string;
|
||||
|
||||
public void test() {
|
||||
this.string="test";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package pkg;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
@Setter
|
||||
public class InlineSetter {
|
||||
private String string;
|
||||
|
||||
public void test() {
|
||||
setString("test");
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,12 @@ import org.junit.runners.Suite.SuiteClasses;
|
||||
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 {
|
||||
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user