mirror of
https://github.com/tiennm99/lombok.git
synced 2026-09-05 00:18:37 +00:00
Extract interface now works when @Data is present.
For all methods even generated by lombok; comments for generated methods are placed above the "managing" annotations @Override for generated methods are skipped (since there is no valid position for this annotation)
This commit is contained in:
@@ -92,6 +92,7 @@ public class EclipsePatcher extends Agent {
|
||||
patchListRewriteHandleGeneratedMethods(sm);
|
||||
patchSyntaxAndOccurrencesHighlighting(sm);
|
||||
patchSortMembersOperation(sm);
|
||||
patchExtractInterface(sm);
|
||||
} else {
|
||||
patchPostCompileHookEcj(sm);
|
||||
}
|
||||
@@ -103,6 +104,24 @@ public class EclipsePatcher extends Agent {
|
||||
if (reloadExistingClasses) sm.reloadClasses(instrumentation);
|
||||
}
|
||||
|
||||
private static void patchExtractInterface(ScriptManager sm) {
|
||||
/* get real generated node in stead of a random one generated by the annotation */
|
||||
sm.addScript(ScriptBuilder.replaceMethodCall()
|
||||
.target(new MethodTarget("org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceProcessor", "createMemberDeclarations"))
|
||||
.target(new MethodTarget("org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceProcessor", "createMethodComments"))
|
||||
.methodToReplace(new Hook("org.eclipse.jdt.internal.corext.refactoring.structure.ASTNodeSearchUtil", "getMethodDeclarationNode", "org.eclipse.jdt.core.dom.MethodDeclaration", "org.eclipse.jdt.core.IMethod", "org.eclipse.jdt.core.dom.CompilationUnit"))
|
||||
.replacementMethod(new Hook("lombok.eclipse.agent.PatchFixes", "getRealMethodDeclarationNode", "org.eclipse.jdt.core.dom.MethodDeclaration", "org.eclipse.jdt.core.IMethod", "org.eclipse.jdt.core.dom.CompilationUnit"))
|
||||
.build());
|
||||
|
||||
/* Do not add @Override's for generated methods */
|
||||
sm.addScript(ScriptBuilder.exitEarly()
|
||||
.target(new MethodTarget("org.eclipse.jdt.core.dom.rewrite.ListRewrite", "insertFirst"))
|
||||
.decisionMethod(new Hook("lombok.eclipse.agent.PatchFixes", "isListRewriteOnGeneratedNode", "boolean", "org.eclipse.jdt.core.dom.rewrite.ListRewrite"))
|
||||
.request(StackRequest.THIS)
|
||||
.build());
|
||||
|
||||
}
|
||||
|
||||
private static void patchSyntaxAndOccurrencesHighlighting(ScriptManager sm) {
|
||||
/*
|
||||
* Skip generated nodes for "visual effects" (syntax highlighting && highlight occurrences)
|
||||
|
||||
@@ -36,11 +36,15 @@ import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.jdt.core.IAnnotatable;
|
||||
import org.eclipse.jdt.core.IAnnotation;
|
||||
import org.eclipse.jdt.core.IMethod;
|
||||
import org.eclipse.jdt.core.JavaModelException;
|
||||
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
|
||||
import org.eclipse.jdt.core.dom.MethodDeclaration;
|
||||
import org.eclipse.jdt.core.dom.SimpleName;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Annotation;
|
||||
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.structure.ASTNodeSearchUtil;
|
||||
|
||||
public class PatchFixes {
|
||||
public static boolean isGenerated(org.eclipse.jdt.core.dom.ASTNode node) {
|
||||
@@ -55,6 +59,10 @@ public class PatchFixes {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static boolean isListRewriteOnGeneratedNode(org.eclipse.jdt.core.dom.rewrite.ListRewrite rewrite) {
|
||||
return isGenerated(rewrite.getParent());
|
||||
}
|
||||
|
||||
public static boolean returnFalse(java.lang.Object object) {
|
||||
return false;
|
||||
}
|
||||
@@ -87,6 +95,30 @@ public class PatchFixes {
|
||||
return original;
|
||||
}
|
||||
|
||||
|
||||
// lombok.eclipse.agent.PatchFixes.getRealMethodDeclarationNode(Lorg/eclipse/jdt/core/IMethod;Lorg/eclipse/jdt/core/dom/CompilationUnit;)Lorg/eclipse/jdt/core/dom/MethodDeclaration;
|
||||
public static org.eclipse.jdt.core.dom.MethodDeclaration getRealMethodDeclarationNode(org.eclipse.jdt.core.IMethod sourceMethod, org.eclipse.jdt.core.dom.CompilationUnit cuUnit) throws JavaModelException {
|
||||
MethodDeclaration methodDeclarationNode = ASTNodeSearchUtil.getMethodDeclarationNode(sourceMethod, cuUnit);
|
||||
if (isGenerated(methodDeclarationNode)) {
|
||||
String typeName = sourceMethod.getTypeRoot().getElementName();
|
||||
String methodName = sourceMethod.getElementName();
|
||||
for (Object type : cuUnit.types()) {
|
||||
org.eclipse.jdt.core.dom.AbstractTypeDeclaration typeDeclaration = (AbstractTypeDeclaration)type;
|
||||
if ((typeDeclaration.getName()+".java").equals(typeName)) {
|
||||
for (Object declaration : typeDeclaration.bodyDeclarations()) {
|
||||
if (declaration instanceof org.eclipse.jdt.core.dom.MethodDeclaration) {
|
||||
org.eclipse.jdt.core.dom.MethodDeclaration methodDeclaration = (org.eclipse.jdt.core.dom.MethodDeclaration) declaration;
|
||||
if (methodDeclaration.getName().toString().equals(methodName)) {
|
||||
return methodDeclaration;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return methodDeclarationNode;
|
||||
}
|
||||
|
||||
public static int getSourceEndFixed(int sourceEnd, org.eclipse.jdt.internal.compiler.ast.ASTNode node) throws Exception {
|
||||
if (sourceEnd == -1) {
|
||||
org.eclipse.jdt.internal.compiler.ast.ASTNode object = (org.eclipse.jdt.internal.compiler.ast.ASTNode)node.getClass().getField("$generatedBy").get(node);
|
||||
|
||||
Reference in New Issue
Block a user