Skip to content

Commit 872dff4

Browse files
refactor: Adopt J.Literal.isLiteralValue(expression, null) (#773)
* refactor: Adopt `J.Literal.isLiteralValue(expression, null)` Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.java.recipes.IsLiteralNullRecipe?organizationId=ODQ2MGExMTUtNDg0My00N2EwLTgzMGMtNGE1NGExMTBmZDkw Co-authored-by: Moderne <[email protected]> * Adopt `isLiteralValue` with `null` argument --------- Co-authored-by: Moderne <[email protected]>
1 parent 9c36dc1 commit 872dff4

File tree

3 files changed

+5
-13
lines changed

3 files changed

+5
-13
lines changed

src/main/java/org/openrewrite/java/migrate/lang/var/DeclarationCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private static boolean isSingleVariableDefinition(J.VariableDeclarations vd) {
6767
}
6868

6969
initializer = initializer.unwrap();
70-
boolean isNullAssigment = initializer instanceof J.Literal && ((J.Literal) initializer).getValue() == null;
70+
boolean isNullAssigment = J.Literal.isLiteralValue(initializer, null);
7171
boolean alreadyUseVar = typeExpression instanceof J.Identifier && "var".equals(((J.Identifier) typeExpression).getSimpleName());
7272
return !isNullAssigment && !alreadyUseVar;
7373
}

src/main/java/org/openrewrite/java/migrate/util/MigrateCollectionsSingletonList.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
4949
@Override
5050
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
5151
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
52-
if (SINGLETON_LIST.matches(m) && isNotLiteralNull(m)) {
52+
if (SINGLETON_LIST.matches(m) &&
53+
!(J.Literal.isLiteralValue(m.getArguments().get(0), null))) {
5354
maybeRemoveImport("java.util.Collections");
5455
maybeAddImport("java.util.List");
5556

@@ -65,11 +66,6 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
6566
}
6667
return m;
6768
}
68-
69-
private boolean isNotLiteralNull(J.MethodInvocation m) {
70-
return !(m.getArguments().get(0) instanceof J.Literal &&
71-
((J.Literal) m.getArguments().get(0)).getValue() == null);
72-
}
7369
});
7470
}
7571
}

src/main/java/org/openrewrite/java/migrate/util/MigrateCollectionsSingletonSet.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
4747
@Override
4848
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
4949
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
50-
if (SINGLETON_SET.matches(m) && isNotLiteralNull(m)) {
50+
if (SINGLETON_SET.matches(m) &&
51+
!(J.Literal.isLiteralValue(m.getArguments().get(0), null))) {
5152
maybeRemoveImport("java.util.Collections");
5253
maybeAddImport("java.util.Set");
5354
return JavaTemplate.builder("Set.of(#{any()})")
@@ -58,11 +59,6 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
5859
}
5960
return m;
6061
}
61-
62-
private boolean isNotLiteralNull(J.MethodInvocation m) {
63-
return !(m.getArguments().get(0) instanceof J.Literal &&
64-
((J.Literal) m.getArguments().get(0)).getValue() == null);
65-
}
6662
});
6763
}
6864
}

0 commit comments

Comments
 (0)