Skip to content

Commit 32d2713

Browse files
committed
Adopt SemanticallyEqual
1 parent 4f960f0 commit 32d2713

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/main/java/org/openrewrite/staticanalysis/InlineVariable.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@
2121
import org.openrewrite.TreeVisitor;
2222
import org.openrewrite.internal.ListUtils;
2323
import org.openrewrite.java.JavaIsoVisitor;
24-
import org.openrewrite.java.tree.Expression;
25-
import org.openrewrite.java.tree.J;
26-
import org.openrewrite.java.tree.JavaType;
27-
import org.openrewrite.java.tree.Statement;
24+
import org.openrewrite.java.search.SemanticallyEqual;
25+
import org.openrewrite.java.tree.*;
2826

2927
import java.time.Duration;
3028
import java.util.Collections;
@@ -63,17 +61,18 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
6361
public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
6462
J.Block bl = super.visitBlock(block, ctx);
6563
List<Statement> statements = bl.getStatements();
66-
if (statements.size() > 1) {
67-
String identReturned = identReturned(statements);
64+
if (1 < statements.size()) {
65+
J.Identifier identReturned = identReturnedOrThrown(statements);
6866
if (identReturned != null) {
6967
Statement secondLastStatement = statements.get(statements.size() - 2);
7068
if (secondLastStatement instanceof J.VariableDeclarations) {
7169
J.VariableDeclarations varDec = (J.VariableDeclarations) secondLastStatement;
7270
// Only inline if there's exactly one variable declared
7371
if (varDec.getVariables().size() == 1) {
7472
J.VariableDeclarations.NamedVariable identDefinition = varDec.getVariables().get(0);
75-
if (varDec.getLeadingAnnotations().isEmpty() && identDefinition.getSimpleName().equals(identReturned)) {
76-
bl = inlineExpression(statements, bl, identDefinition.getInitializer(), varDec.getPrefix(), varDec.getComments());
73+
if (varDec.getLeadingAnnotations().isEmpty() &&
74+
SemanticallyEqual.areEqual(identDefinition.getName(), identReturned)) {
75+
return inlineExpression(identDefinition.getInitializer(), bl, statements, varDec.getPrefix(), varDec.getComments());
7776
}
7877
}
7978
} else if (secondLastStatement instanceof J.Assignment) {
@@ -83,8 +82,8 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
8382
// Only inline local variable assignments, not fields
8483
if (assignedVar.getFieldType() != null &&
8584
assignedVar.getFieldType().getOwner() instanceof JavaType.Method &&
86-
assignedVar.getSimpleName().equals(identReturned)) {
87-
bl = inlineExpression(statements, bl, assignment.getAssignment(), assignment.getPrefix(), assignment.getComments());
85+
SemanticallyEqual.areEqual(assignedVar, identReturned)) {
86+
return inlineExpression(assignment.getAssignment(), bl, statements, assignment.getPrefix(), assignment.getComments());
8887
}
8988
}
9089
}
@@ -93,45 +92,47 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
9392
return bl;
9493
}
9594

96-
private J.Block inlineExpression(List<Statement> statements, J.Block bl, @Nullable Expression expression,
97-
org.openrewrite.java.tree.Space prefix, List<org.openrewrite.java.tree.Comment> comments) {
95+
private J.Block inlineExpression(@Nullable Expression expression, J.Block bl, List<Statement> statements,
96+
Space prefix, List<Comment> comments) {
9897
if (expression == null) {
9998
return bl;
10099
}
101100

102101
return bl.withStatements(ListUtils.map(statements, (i, statement) -> {
103102
if (i == statements.size() - 2) {
104103
return null;
105-
} else if (i == statements.size() - 1) {
104+
}
105+
if (i == statements.size() - 1) {
106106
if (statement instanceof J.Return) {
107107
J.Return return_ = (J.Return) statement;
108-
return return_.withExpression(expression
109-
.withPrefix(requireNonNull(return_.getExpression()).getPrefix()))
108+
return return_
109+
.withExpression(expression.withPrefix(requireNonNull(return_.getExpression()).getPrefix()))
110110
.withPrefix(prefix.withComments(ListUtils.concatAll(comments, return_.getComments())));
111-
} else if (statement instanceof J.Throw) {
111+
}
112+
if (statement instanceof J.Throw) {
112113
J.Throw thrown = (J.Throw) statement;
113-
return thrown.withException(expression
114-
.withPrefix(requireNonNull(thrown.getException()).getPrefix()))
114+
return thrown.
115+
withException(expression.withPrefix(requireNonNull(thrown.getException()).getPrefix()))
115116
.withPrefix(prefix.withComments(ListUtils.concatAll(comments, thrown.getComments())));
116117
}
117118
}
118119
return statement;
119120
}));
120121
}
121122

122-
private @Nullable String identReturned(List<Statement> stats) {
123+
private J.@Nullable Identifier identReturnedOrThrown(List<Statement> stats) {
123124
Statement lastStatement = stats.get(stats.size() - 1);
124125
if (lastStatement instanceof J.Return) {
125126
J.Return return_ = (J.Return) lastStatement;
126127
Expression expression = return_.getExpression();
127128
if (expression instanceof J.Identifier &&
128129
!(expression.getType() instanceof JavaType.Array)) {
129-
return ((J.Identifier) expression).getSimpleName();
130+
return ((J.Identifier) expression);
130131
}
131132
} else if (lastStatement instanceof J.Throw) {
132133
J.Throw thr = (J.Throw) lastStatement;
133134
if (thr.getException() instanceof J.Identifier) {
134-
return ((J.Identifier) thr.getException()).getSimpleName();
135+
return ((J.Identifier) thr.getException());
135136
}
136137
}
137138
return null;

0 commit comments

Comments
 (0)