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

Fix pull up scenario referring to outer this type #1854

Merged
merged 1 commit into from
Dec 14, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,18 @@ public boolean visit(SimpleName node) {
return true;
}

@Override
public boolean visit(ThisExpression node) {
if (node.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) {
ITypeBinding typeBinding= node.resolveTypeBinding();
if (isChildTypeMember(typeBinding, fSourceType) && !isChildTypeMember(typeBinding, fTargetType)) {
fConflictBinding= typeBinding;
throw new AbortSearchException();
}
}
return super.visit(node);
}

private boolean isChildTypeMember(ITypeBinding parentTypeBinding, IType type) {
if (parentTypeBinding.getQualifiedName().equals(type.getFullyQualifiedName('.'))) {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package p;

public class A {
public class BaseInner {
void innerMethodLambda(Outer outer) {
Runnable r = () -> {
System.out.println(outer.x);
outer.foo();
};
r.run();
}
}

public class Outer {
public int x = 0;
public void foo(){};

public class Inner extends BaseInner {
void innerMethod() { // Pull this method up to class BaseInner
innerMethodLambda(Outer.this);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1882,7 +1882,27 @@ public void testFail36() throws Exception {
testMonitor.assertUsedUp();
testMonitor.prepare();
assertFalse("precondition was supposed to fail", checkInputResult.isOK());
// helper2(new String[] { "method" }, new String[][] { new String[0] }, true, false, 0);
}
@Test
public void testFail37() throws Exception {
ICompilationUnit cu= createCUfromTestFile(getPackageP(), "A");
IJavaElement element= cu.getElementAt(409);
assertTrue("element is method", element instanceof IMethod);
IMethod[] methods= new IMethod[] { (IMethod)element };

PullUpRefactoringProcessor processor= createRefactoringProcessor(methods);
Refactoring ref= processor.getRefactoring();

FussyProgressMonitor testMonitor= new FussyProgressMonitor();
assertTrue("activation", ref.checkInitialConditions(testMonitor).isOK());
testMonitor.assertUsedUp();
testMonitor.prepare();
setTargetClass(processor, 0);

RefactoringStatus checkInputResult= ref.checkFinalConditions(testMonitor);
testMonitor.assertUsedUp();
testMonitor.prepare();
assertFalse("precondition was supposed to fail", checkInputResult.isOK());
}
//----------------------------------------------------------
@Test
Expand Down
Loading