-
Notifications
You must be signed in to change notification settings - Fork 88
No empty UUID #720
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
Draft
Pankraz76
wants to merge
1
commit into
openrewrite:main
Choose a base branch
from
Pankraz76:NoEmptyUUID
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
No empty UUID #720
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
209 changes: 209 additions & 0 deletions
209
src/main/java/org/openrewrite/staticanalysis/NoEmptyUUID.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,209 @@ | ||||||
/* | ||||||
* Copyright 2024 the original author or authors. | ||||||
* <p> | ||||||
* Licensed under the Moderne Source Available License (the "License"); | ||||||
* you may not use this file except in compliance with the License. | ||||||
* You may obtain a copy of the License at | ||||||
* <p> | ||||||
* https://docs.moderne.io/licensing/moderne-source-available-license | ||||||
* <p> | ||||||
* Unless required by applicable law or agreed to in writing, software | ||||||
* distributed under the License is distributed on an "AS IS" BASIS, | ||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
* See the License for the specific language governing permissions and | ||||||
* limitations under the License. | ||||||
*/ | ||||||
package org.openrewrite.staticanalysis; | ||||||
|
||||||
import org.openrewrite.*; | ||||||
import org.openrewrite.java.JavaIsoVisitor; | ||||||
import org.openrewrite.java.JavaTemplate; | ||||||
import org.openrewrite.java.MethodMatcher; | ||||||
import org.openrewrite.java.tree.*; | ||||||
|
||||||
public class NoEmptyUUID extends Recipe { | ||||||
|
||||||
@Override | ||||||
public String getDisplayName() { | ||||||
return "Remove impossible UUID emptiness checks"; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public String getDescription() { | ||||||
return "UUID.toString() always returns a non-empty string, so isEmpty() and isBlank() always return false."; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||||||
return new JavaIsoVisitor<ExecutionContext>() { | ||||||
private final MethodMatcher uuidToStringMatcher = new MethodMatcher("java.util.UUID toString()"); | ||||||
private final MethodMatcher isEmptyMatcher = new MethodMatcher("java.lang.String isEmpty()"); | ||||||
private final MethodMatcher isBlankMatcher = new MethodMatcher("java.lang.String isBlank()"); | ||||||
|
||||||
@Override | ||||||
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||||||
// Skip processing if this is already a replaced literal (not a real method invocation) | ||||||
if (method.getMethodType() == null) { | ||||||
return method; | ||||||
} | ||||||
|
||||||
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx); | ||||||
|
||||||
// Check if this is a call to isEmpty() or isBlank() | ||||||
if (isEmptyMatcher.matches(mi) || isBlankMatcher.matches(mi)) { | ||||||
Expression select = mi.getSelect(); | ||||||
|
||||||
// Check if the select is a UUID.toString() call or chain | ||||||
if (isUUIDToStringCall(select)) { | ||||||
// Replace with false | ||||||
return JavaTemplate.builder("false") | ||||||
.contextSensitive() | ||||||
.build() | ||||||
.apply(getCursor(), mi.getCoordinates().replace()); | ||||||
} | ||||||
} | ||||||
|
||||||
return mi; | ||||||
} | ||||||
|
||||||
private boolean isUUIDToStringCall(Expression expression) { | ||||||
if (expression instanceof J.MethodInvocation) { | ||||||
J.MethodInvocation mi = (J.MethodInvocation) expression; | ||||||
|
||||||
// Direct UUID.toString() call | ||||||
if (uuidToStringMatcher.matches(mi)) { | ||||||
return true; | ||||||
} | ||||||
|
||||||
// Chained calls like UUID.toString().trim() | ||||||
if (mi.getSelect() != null) { | ||||||
return isUUIDToStringCall(mi.getSelect()); | ||||||
} | ||||||
} | ||||||
|
||||||
// Check if this is an identifier that references a UUID string | ||||||
if (expression instanceof J.Identifier) { | ||||||
J.Identifier identifier = (J.Identifier) expression; | ||||||
return isUUIDStringVariable(identifier); | ||||||
} | ||||||
|
||||||
return false; | ||||||
} | ||||||
|
||||||
private boolean isUUIDStringVariable(J.Identifier identifier) { | ||||||
// Look for variable declarations that come from UUID.toString() | ||||||
Cursor parent = getCursor().dropParentUntil(is -> | ||||||
is instanceof J.VariableDeclarations || is instanceof J.MethodInvocation); | ||||||
|
||||||
if (parent.getValue() instanceof J.VariableDeclarations) { | ||||||
J.VariableDeclarations varDecl = (J.VariableDeclarations) parent.getValue(); | ||||||
for (J.VariableDeclarations.NamedVariable variable : varDecl.getVariables()) { | ||||||
if (variable.getName().getSimpleName().equals(identifier.getSimpleName())) { | ||||||
Expression initializer = variable.getInitializer(); | ||||||
return initializer != null && isUUIDToStringCall(initializer); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return false; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public J.Lambda visitLambda(J.Lambda lambda, ExecutionContext ctx) { | ||||||
J.Lambda l = super.visitLambda(lambda, ctx); | ||||||
|
||||||
if (l.getBody() instanceof J.MethodInvocation) { | ||||||
J.MethodInvocation body = (J.MethodInvocation) l.getBody(); | ||||||
if ((isEmptyMatcher.matches(body) || isBlankMatcher.matches(body)) && | ||||||
isUUIDToStringCall(body.getSelect())) { | ||||||
// Replace the lambda body with false | ||||||
l = l.withBody( | ||||||
JavaTemplate.builder("false") | ||||||
.contextSensitive() | ||||||
.build() | ||||||
.apply(getCursor(), body.getCoordinates().replace()) | ||||||
); | ||||||
} | ||||||
} | ||||||
|
||||||
return l; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public J.If visitIf(J.If iff, ExecutionContext ctx) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
J.If ifStatement = super.visitIf(iff, ctx); | ||||||
|
||||||
// Check if the condition contains a UUID emptiness check | ||||||
if (ifStatement.getIfCondition() instanceof J.ControlParentheses) { | ||||||
J.ControlParentheses<Expression> controlParens = (J.ControlParentheses<Expression>) ifStatement.getIfCondition(); | ||||||
Expression condition = controlParens.getTree(); | ||||||
|
||||||
if (condition instanceof J.MethodInvocation) { | ||||||
J.MethodInvocation conditionMi = (J.MethodInvocation) condition; | ||||||
if ((isEmptyMatcher.matches(conditionMi) || isBlankMatcher.matches(conditionMi)) && | ||||||
isUUIDToStringCall(conditionMi.getSelect())) { | ||||||
// Remove the entire if statement since the condition is always false | ||||||
return null; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return ifStatement; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public J.WhileLoop visitWhileLoop(J.WhileLoop whileLoop, ExecutionContext ctx) { | ||||||
J.WhileLoop wl = super.visitWhileLoop(whileLoop, ctx); | ||||||
|
||||||
// Check if the condition contains a UUID emptiness check | ||||||
if (wl.getCondition() instanceof J.ControlParentheses) { | ||||||
J.ControlParentheses<Expression> controlParens = (J.ControlParentheses<Expression>) wl.getCondition(); | ||||||
Expression condition = controlParens.getTree(); | ||||||
|
||||||
if (condition instanceof J.MethodInvocation) { | ||||||
J.MethodInvocation conditionMi = (J.MethodInvocation) condition; | ||||||
if ((isEmptyMatcher.matches(conditionMi) || isBlankMatcher.matches(conditionMi)) && | ||||||
isUUIDToStringCall(conditionMi.getSelect())) { | ||||||
// Replace with while (false) | ||||||
return wl.withCondition( | ||||||
JavaTemplate.builder("(false)") | ||||||
.contextSensitive() | ||||||
.build() | ||||||
.apply(getCursor(), wl.getCondition().getCoordinates().replace()) | ||||||
); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return wl; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public J.DoWhileLoop visitDoWhileLoop(J.DoWhileLoop doWhileLoop, ExecutionContext ctx) { | ||||||
J.DoWhileLoop dwl = super.visitDoWhileLoop(doWhileLoop, ctx); | ||||||
|
||||||
// Check if the condition contains a UUID emptiness check | ||||||
if (dwl.getWhileCondition() instanceof J.ControlParentheses) { | ||||||
J.ControlParentheses<Expression> controlParens = (J.ControlParentheses<Expression>) dwl.getWhileCondition(); | ||||||
Expression condition = controlParens.getTree(); | ||||||
|
||||||
if (condition instanceof J.MethodInvocation) { | ||||||
J.MethodInvocation conditionMi = (J.MethodInvocation) condition; | ||||||
if ((isEmptyMatcher.matches(conditionMi) || isBlankMatcher.matches(conditionMi)) && | ||||||
isUUIDToStringCall(conditionMi.getSelect())) { | ||||||
// Replace with while (false) | ||||||
return dwl.withWhileCondition( | ||||||
JavaTemplate.builder("(false)") | ||||||
.contextSensitive() | ||||||
.build() | ||||||
.apply(getCursor(), dwl.getWhileCondition().getCoordinates().replace()) | ||||||
); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return dwl; | ||||||
} | ||||||
}; | ||||||
} | ||||||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.