Skip to content

Commit

Permalink
Fix for #1091: index field reference for delegated assignment in closure
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 23, 2020
1 parent 8d9a692 commit 9920402
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,16 @@ public void visitArrayExpression(final ArrayExpression expression) {

@Override
public void visitBinaryExpression(final BinaryExpression expression) {
if (expression.getOperation().isA(Types.ASSIGNMENT_OPERATOR) &&
expression.getLeftExpression() instanceof VariableExpression) {
if (expression.getLeftExpression() instanceof VariableExpression &&
expression.getOperation().isA(Types.ASSIGNMENT_OPERATOR)) {
String name = expression.getLeftExpression().getText();
int offset = expression.getLeftExpression().getStart();
// index "setName(x)" for Call Hierarchy/Find References
visitNameReference(AccessorSupport.SETTER, name, offset);
// index "name"
requestor.acceptFieldReference(name.toCharArray(), offset);
// index "name(x)" for SyntheticAccessorsRenameParticipant
requestor.acceptMethodReference(name.toCharArray(), 1, offset);
// index "setName(x)"
visitNameReference(AccessorSupport.SETTER, name, offset);

expression.getRightExpression().visit(this);
} else {
Expand Down Expand Up @@ -305,15 +307,14 @@ public void visitVariableExpression(final VariableExpression expression) {
if (expression.getEnd() > 0) {
String name = expression.getName();
int offset = expression.getStart();
// index "name"
requestor.acceptFieldReference(name.toCharArray(), offset);
// index "name()" for SyntheticAccessorsRenameParticipant
requestor.acceptMethodReference(name.toCharArray(), 0, offset);
// index "getName()" and "isName()"
visitNameReference(AccessorSupport.GETTER, name, offset);
visitNameReference(AccessorSupport.ISSER, name, offset);
// index "name()" for SyntheticAccessorsRenameParticipant
requestor.acceptMethodReference(name.toCharArray(), 0, offset);
// index "name" for package references and anything else?
requestor.acceptUnknownReference(name.toCharArray(), offset);
}
//super.visitVariableExpression(expression);
}

//--------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package p

class MyBean {
def fooBar, bar
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package p

void test() {
new MyBean().with {
fooBar = 'foo'
bar = 'bar'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package p

class MyBean {
def foo, bar
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package p

void test() {
new MyBean().with {
foo = 'foo'
bar = 'bar'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,10 @@ final class RenameFieldTests extends RefactoringTestSuite {
def status = runTest('MyBean', 'foo', 'fooBar')
assert status.entries[0].message.startsWith('Found potential matches.')
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1091
void test16() {
def status = runTest('MyBean', 'fooBar', 'foo')
assert status.isOK()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -433,45 +433,45 @@ final class SyntheticAccessorRenamingTests extends RenameRefactoringTestSuite {
performRefactoringAndUndo('setBar', new TestSource(
pack: 'p', name: 'Pojo.java',
contents: '''\
package p;
public class Pojo {
public void setFoo(int foo) { this.foo = foo; }
public int getFoo() { return this.foo; }
private int foo = -1;
}
'''.stripIndent(),
finalContents: '''\
package p;
public class Pojo {
public void setBar(int foo) { this.foo = foo; }
public int getFoo() { return this.foo; }
private int foo = -1;
}
'''.stripIndent()
|package p;
|public class Pojo {
| public void setFoo(int foo) { this.foo = foo; }
| public int getFoo() { return this.foo; }
| private int foo = -1;
|}
|'''.stripMargin(),
finalContents: '''\
|package p;
|public class Pojo {
| public void setBar(int foo) { this.foo = foo; }
| public int getFoo() { return this.foo; }
| private int foo = -1;
|}
|'''.stripMargin()
), new TestSource(
pack: 'q', name: 'Script.groovy',
contents: '''\
package q
import p.Pojo
new Pojo().with {
foo = 1
foo += 1
getFoo()
setFoo(3)
def val = foo + 456
}
'''.stripIndent(),
finalContents: '''\
package q
import p.Pojo
new Pojo().with {
bar = 1
bar += 1
getFoo()
setBar(3)
def val = foo + 456
}
'''.stripIndent()
|package q
|import p.Pojo
|new Pojo().with {
| foo = 1
| foo += 1
| getFoo()
| setFoo(3)
| def val = foo + 456
|}
|'''.stripMargin(),
finalContents: '''\
|package q
|import p.Pojo
|new Pojo().with {
| bar = 1
| bar += 1
| getFoo()
| setBar(3)
| def val = foo + 456
|}
|'''.stripMargin()
))
}

Expand All @@ -482,45 +482,84 @@ final class SyntheticAccessorRenamingTests extends RenameRefactoringTestSuite {
performRefactoringAndUndo('bar', new TestSource(
pack: 'p', name: 'Pojo.java',
contents: '''\
package p;
public class Pojo {
private int foo = -1;
public int getFoo() { return this.foo; }
public void setFoo(int val) { this.foo = val; }
}
'''.stripIndent(),
finalContents: '''\
package p;
public class Pojo {
private int bar = -1;
public int getBar() { return this.bar; }
public void setBar(int val) { this.bar = val; }
}
'''.stripIndent()
|package p;
|public class Pojo {
| private int foo = -1;
| public int getFoo() { return this.foo; }
| public void setFoo(int val) { this.foo = val; }
|}
|'''.stripMargin(),
finalContents: '''\
|package p;
|public class Pojo {
| private int bar = -1;
| public int getBar() { return this.bar; }
| public void setBar(int val) { this.bar = val; }
|}
|'''.stripMargin()
), new TestSource(
pack: 'q', name: 'Script.groovy',
contents: '''\
package q
import p.Pojo
new Pojo().with {
foo = 1
foo += 1
getFoo()
setFoo(3)
def val = foo + 456
}
'''.stripIndent(),
finalContents: '''\
package q
import p.Pojo
new Pojo().with {
bar = 1
bar += 1
getBar()
setBar(3)
def val = bar + 456
}
'''.stripIndent()
|package q
|import p.Pojo
|new Pojo().with {
| foo = 1
| foo += 1
| getFoo()
| setFoo(3)
| def val = foo + 456
|}
|'''.stripMargin(),
finalContents: '''\
|package q
|import p.Pojo
|new Pojo().with {
| bar = 1
| bar += 1
| getBar()
| setBar(3)
| def val = bar + 456
|}
|'''.stripMargin()
))
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1091
void testMultiFileRename8() {
performRefactoringAndUndo('foo', new TestSource(
pack: 'p', name: 'Pogo.groovy',
contents: '''\
|package p
|class Pogo {
| def fooBar, bar
|}
|'''.stripMargin(),
finalContents: '''\
|package p
|class Pogo {
| def foo, bar
|}
|'''.stripMargin()
), new TestSource(
pack: 'q', name: 'Script.groovy',
contents: '''\
|package q
|import p.Pogo
|new Pogo().with {
| fooBar = 1
| fooBar += 1
| def val = fooBar + 456
|}
|'''.stripMargin(),
finalContents: '''\
|package q
|import p.Pogo
|new Pogo().with {
| foo = 1
| foo += 1
| def val = foo + 456
|}
|'''.stripMargin()
))
}

Expand Down

0 comments on commit 9920402

Please sign in to comment.