Patch getMethodDeclarationNode method instead of individual calls

This commit is contained in:
Rawi01 authored and Roel Spilker committed 2021-12-12 02:30:37 +01:00
1 parent 025a76aa66
commit ff7b1fbb43
2 files changed
+56 -37

No files matched your search

@@ -95,6 +95,7 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable {
patchEclipseDebugPatches(sm);
patchJavadoc(sm);
patchASTConverterLiterals(sm);
patchASTNodeSearchUtil(sm);
patchPostCompileHookEcj(sm);
@@ -174,17 +175,6 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable {
.transplant()
.build());
/* Get real generated node instead of a random one generated by the annotation */
sm.addScriptIfWitness(OSGI_TYPES, 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"))
.target(new MethodTarget("org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoringProcessor", "createAbstractMethod"))
.target(new MethodTarget("org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoringProcessor", "addMethodStubForAbstractMethod"))
.target(new MethodTarget("org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoringProcessor", "createChangeManager"))
.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.launch.PatchFixesHider$PatchFixes", "getRealMethodDeclarationNode", "org.eclipse.jdt.core.dom.MethodDeclaration", "org.eclipse.jdt.core.IMethod", "org.eclipse.jdt.core.dom.CompilationUnit"))
.transplant().build());
/* Do not add @Override's for generated methods */
sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.exitEarly()
.target(new MethodTarget("org.eclipse.jdt.core.dom.rewrite.ListRewrite", "insertFirst"))
@@ -995,5 +985,18 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable {
.transplant()
.build());
}
private static void patchASTNodeSearchUtil(ScriptManager sm) {
/*
* If an annotation generates more than one method the normal node search returns one of them instead of the right one.
* This patch method also compares method name and method parameter types to identify the right method.
*/
sm.addScriptIfWitness(OSGI_TYPES, ScriptBuilder.wrapReturnValue()
.target(new MethodTarget("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"))
.wrapMethod(new Hook("lombok.launch.PatchFixesHider$PatchFixes", "getRealMethodDeclarationNode", "org.eclipse.jdt.core.dom.MethodDeclaration", "org.eclipse.jdt.core.dom.MethodDeclaration", "org.eclipse.jdt.core.IMethod", "org.eclipse.jdt.core.dom.CompilationUnit"))
.request(StackRequest.RETURN_VALUE, StackRequest.PARAM1, StackRequest.PARAM2)
.transplant()
.build());
}
}
@@ -39,8 +39,8 @@ 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.MethodDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
@@ -56,7 +56,6 @@ 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.structure.ASTNodeSearchUtil;
import org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.IncomingMemberVisibilityAdjustment;
import static lombok.eclipse.EcjAugments.ASTNode_generatedBy;
@@ -540,35 +539,52 @@ final class PatchFixesHider {
}
}
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)) {
IType declaringType = sourceMethod.getDeclaringType();
Stack<IType> typeStack = new Stack<IType>();
while (declaringType != null) {
typeStack.push(declaringType);
declaringType = declaringType.getDeclaringType();
}
IType rootType = typeStack.pop();
org.eclipse.jdt.core.dom.AbstractTypeDeclaration typeDeclaration = findTypeDeclaration(rootType, cuUnit.types());
while (!typeStack.isEmpty() && typeDeclaration != null) {
typeDeclaration = findTypeDeclaration(typeStack.pop(), typeDeclaration.bodyDeclarations());
}
if (typeStack.isEmpty() && typeDeclaration != null) {
String methodName = sourceMethod.getElementName();
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;
public static org.eclipse.jdt.core.dom.MethodDeclaration getRealMethodDeclarationNode(org.eclipse.jdt.core.dom.MethodDeclaration original, org.eclipse.jdt.core.IMethod sourceMethod, org.eclipse.jdt.core.dom.CompilationUnit cuUnit) throws JavaModelException {
if (!isGenerated(original)) return original;
IType declaringType = sourceMethod.getDeclaringType();
Stack<IType> typeStack = new Stack<IType>();
while (declaringType != null) {
typeStack.push(declaringType);
declaringType = declaringType.getDeclaringType();
}
IType rootType = typeStack.pop();
org.eclipse.jdt.core.dom.AbstractTypeDeclaration typeDeclaration = findTypeDeclaration(rootType, cuUnit.types());
while (!typeStack.isEmpty() && typeDeclaration != null) {
typeDeclaration = findTypeDeclaration(typeStack.pop(), typeDeclaration.bodyDeclarations());
}
String targetMethodName = sourceMethod.getElementName();
List<String> targetMethodParameterTypes = new ArrayList<String>();
for (String parameterType : sourceMethod.getParameterTypes()) {
targetMethodParameterTypes.add(org.eclipse.jdt.core.Signature.toString(parameterType));
}
if (typeStack.isEmpty() && typeDeclaration != null) {
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(targetMethodName)) continue;
if (methodDeclaration.parameters().size() != targetMethodParameterTypes.size()) continue;
if (!isGenerated(methodDeclaration)) continue;
boolean parameterTypesEquals = true;
for (int i = 0; i < methodDeclaration.parameters().size(); i++) {
SingleVariableDeclaration variableDeclaration = (SingleVariableDeclaration) methodDeclaration.parameters().get(i);
if (!variableDeclaration.getType().toString().equals(targetMethodParameterTypes.get(i))) {
parameterTypesEquals = false;
break;
}
}
if (parameterTypesEquals) {
return methodDeclaration;
}
}
}
}
return methodDeclarationNode;
return original;
}
// part of getRealMethodDeclarationNode