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: FinalizeMethodArguments considers unary and += expressions #180

Merged
merged 6 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -12,6 +12,7 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jetbrains.annotations.NotNull;
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
Expand Down Expand Up @@ -145,6 +146,40 @@ public J.Assignment visitAssignment(J.Assignment a, AtomicBoolean hasAssignment)

return assignment;
}

// contributed as a fix for issue #176
@Override
public J.@NotNull Unary visitUnary(final J.@NotNull Unary unary, final AtomicBoolean hasAssignment) {
if (hasAssignment.get()) {
return unary;
}

final J.Unary u = super.visitUnary(unary, hasAssignment);
if (u.getOperator().isModifying() && u.getExpression() instanceof J.Identifier) {
final J.Identifier i = (J.Identifier) u.getExpression();
if (i.getSimpleName().equals(this.variable.getSimpleName())) {
hasAssignment.set(true);
}
}
return u;
}

// contributed as a fix for issue #176
@Override
public J.AssignmentOperation visitAssignmentOperation(final J.AssignmentOperation assignOp, final AtomicBoolean hasAssignment) {
if (hasAssignment.get()) {
return assignOp;
}

final J.AssignmentOperation a = super.visitAssignmentOperation(assignOp, hasAssignment);
if (a.getVariable() instanceof J.Identifier) {
final J.Identifier i = (J.Identifier) a.getVariable();
if (i.getSimpleName().equals(this.variable.getSimpleName())) {
hasAssignment.set(true);
}
}
return a;
}
}

private static Statement updateParam(final Statement p) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,74 @@ public String getSubeventNameForCategoryGroupId(final String subeventName, final
)
);
}

@Test
void doNotReplaceIfAssignedThroughUnaryOrAccumulator(){
AlejandroBertolo marked this conversation as resolved.
Show resolved Hide resolved
//language=java
rewriteRun(java("""
package Test;

class Test {

protected int addFinalToThisVar(int unalteredVariable) {
return unalteredVariable;
}

protected int increment(int variableToIncrement) {
variableToIncrement++;
return variableToIncrement;
}

protected int preIncrement(int variableToPreIncrement) {
return ++variableToPreIncrement;
}

protected int decrement(int variableToDecrement) {
variableToDecrement--;
return variableToDecrement;
}

protected int preDecrement(int variableToPreDecrement) {
return --variableToPreDecrement;
}

protected int accumulate(int add, int accumulator) {
accumulator += add;
return accumulator;
}
}
""", """
package Test;

class Test {

protected int addFinalToThisVar(final int unalteredVariable) {
return unalteredVariable;
}

protected int increment(int variableToIncrement) {
variableToIncrement++;
return variableToIncrement;
}

protected int preIncrement(int variableToPreIncrement) {
return ++variableToPreIncrement;
}

protected int decrement(int variableToDecrement) {
variableToDecrement--;
return variableToDecrement;
}

protected int preDecrement(int variableToPreDecrement) {
return --variableToPreDecrement;
}

protected int accumulate(int add, int accumulator) {
accumulator += add;
return accumulator;
}
}
"""));
}
}
Loading