mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 10:18:46 +00:00
Patch for renaming fields with Getter/Setter/Data in eclipse.
This commit is contained in:
1 parent
26ad9e7f5f
commit
d02bbf6261
2 files changed
+66
No files matched your search
@@ -28,6 +28,8 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup;
|
||||
|
||||
import lombok.core.AgentLauncher;
|
||||
import lombok.patcher.Hook;
|
||||
import lombok.patcher.MethodTarget;
|
||||
@@ -108,10 +110,28 @@ public class EclipsePatcher implements AgentLauncher.AgentLaunchable {
|
||||
patchLombokizeAST(sm);
|
||||
patchEcjTransformers(sm, ecjOnly);
|
||||
patchExtensionMethod(sm, ecjOnly);
|
||||
patchRenameField(sm);
|
||||
|
||||
if (reloadExistingClasses) sm.reloadClasses(instrumentation);
|
||||
}
|
||||
|
||||
private static void patchRenameField(ScriptManager sm) {
|
||||
/* RefactoringSearchEngine.search will not return results when renaming field and Data Annotation is present. Return a fake Element to make checks pass */
|
||||
sm.addScript(ScriptBuilder.wrapMethodCall()
|
||||
.target(new MethodTarget("org.eclipse.jdt.internal.corext.refactoring.rename.RenameFieldProcessor", "checkAccessorDeclarations", "org.eclipse.ltk.core.refactoring.RefactoringStatus", "org.eclipse.core.runtime.IProgressMonitor", "org.eclipse.jdt.core.IMethod"))
|
||||
.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());
|
||||
|
||||
/* 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());
|
||||
}
|
||||
|
||||
private static void patchExtractInterface(ScriptManager sm) {
|
||||
/* Fix sourceEnding for generated nodes to avoid null pointer */
|
||||
sm.addScript(ScriptBuilder.wrapMethodCall()
|
||||
|
||||
@@ -36,11 +36,13 @@ import lombok.eclipse.EclipseAugments;
|
||||
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.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.search.SearchMatch;
|
||||
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
|
||||
import org.eclipse.jdt.internal.compiler.ast.Annotation;
|
||||
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
|
||||
@@ -55,9 +57,11 @@ import org.eclipse.jdt.internal.compiler.lookup.Scope;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
|
||||
import org.eclipse.jdt.internal.compiler.parser.Parser;
|
||||
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
|
||||
import org.eclipse.jdt.internal.core.SourceField;
|
||||
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;
|
||||
|
||||
/** These contain a mix of the following:
|
||||
@@ -565,6 +569,48 @@ final class PatchFixesHider {
|
||||
return result.size() == methods.length ? methods : result.toArray(new IMethod[result.size()]);
|
||||
}
|
||||
|
||||
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[result.size()]);
|
||||
}
|
||||
|
||||
public static SearchResultGroup[] createFakeSearchResult(SearchResultGroup[] returnValue,
|
||||
Object/*
|
||||
* org.eclipse.jdt.internal.corext.refactoring.rename.
|
||||
* RenameFieldProcessor
|
||||
*/ processor) throws Exception {
|
||||
if (returnValue == null || returnValue.length == 0) {
|
||||
// if no matches were found, check if Data annotation is present on the class
|
||||
Field declaredField = processor.getClass().getDeclaredField("fField");
|
||||
if (declaredField != null) {
|
||||
declaredField.setAccessible(true);
|
||||
SourceField fField = (SourceField) declaredField.get(processor);
|
||||
IAnnotation dataAnnotation = fField.getDeclaringType().getAnnotation("Data");
|
||||
if (dataAnnotation != null) {
|
||||
// add fake item, to make refactoring checks pass
|
||||
return new SearchResultGroup[] {new SearchResultGroup(null, new SearchMatch[1])};
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
public static SimpleName[] removeGeneratedSimpleNames(SimpleName[] in) throws Exception {
|
||||
Field f = SimpleName.class.getField("$isGenerated");
|
||||
|
||||
|
||||
Reference in new issue
Block a user