Skip to content

Commit

Permalink
Lambda (#419)
Browse files Browse the repository at this point in the history
* lambda

* @disabled

* toString

* form

* wip

* wip

* wip

* lambda

* wip

* wip

* lambdaGenerics

* lambdaGenerics

* lambdaGenerics

* lambdaGenerics

* METHOD_MATCHERS

* e

* form

* wip

* wip

* undo

* Update EqualsAvoidsNullVisitor.java

try undo

* Revert "Update EqualsAvoidsNullVisitor.java"

This reverts commit 4d0fece.

* Slight polish

---------

Co-authored-by: Vincent Potucek <vincent.potucek@sap.com>
Co-authored-by: Tim te Beek <tim@moderne.io>
  • Loading branch information
3 people authored Dec 27, 2024
1 parent f7469d1 commit 34d6589
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class EqualsAvoidsNullVisitor<P> extends JavaVisitor<P> {
public J visitMethodInvocation(J.MethodInvocation method, P p) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, p);
if (m.getSelect() != null && !(m.getSelect() instanceof J.Literal) &&
isStringComparisonMethod(m) && hasCompatibleArgument(m)) {
isStringComparisonMethod(m) && hasCompatibleArgument(m)) {

maybeHandleParentBinary(m);

Expand Down Expand Up @@ -91,11 +91,11 @@ private boolean hasCompatibleArgument(J.MethodInvocation m) {

private boolean isStringComparisonMethod(J.MethodInvocation methodInvocation) {
return EQUALS.matches(methodInvocation) ||
!style.getIgnoreEqualsIgnoreCase() &&
EQUALS_IGNORE_CASE.matches(methodInvocation) ||
COMPARE_TO.matches(methodInvocation) ||
COMPARE_TO_IGNORE_CASE.matches(methodInvocation) ||
CONTENT_EQUALS.matches(methodInvocation);
!style.getIgnoreEqualsIgnoreCase() &&
EQUALS_IGNORE_CASE.matches(methodInvocation) ||
COMPARE_TO.matches(methodInvocation) ||
COMPARE_TO_IGNORE_CASE.matches(methodInvocation) ||
CONTENT_EQUALS.matches(methodInvocation);
}

private void maybeHandleParentBinary(J.MethodInvocation m) {
Expand All @@ -104,7 +104,7 @@ private void maybeHandleParentBinary(J.MethodInvocation m) {
if (((J.Binary) parent).getOperator() == J.Binary.Type.And && ((J.Binary) parent).getLeft() instanceof J.Binary) {
J.Binary potentialNullCheck = (J.Binary) ((J.Binary) parent).getLeft();
if (isNullLiteral(potentialNullCheck.getLeft()) && matchesSelect(potentialNullCheck.getRight(), requireNonNull(m.getSelect())) ||
isNullLiteral(potentialNullCheck.getRight()) && matchesSelect(potentialNullCheck.getLeft(), requireNonNull(m.getSelect()))) {
isNullLiteral(potentialNullCheck.getRight()) && matchesSelect(potentialNullCheck.getLeft(), requireNonNull(m.getSelect()))) {
doAfterVisit(new RemoveUnnecessaryNullCheck<>((J.Binary) parent));
}
}
Expand Down
123 changes: 123 additions & 0 deletions src/test/java/org/openrewrite/staticanalysis/EqualsAvoidsNullTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.staticanalysis;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
Expand Down Expand Up @@ -262,6 +263,128 @@ private boolean isFoo(String foo, String bar) {
);
}

@Test
void generics() {
rewriteRun(
//language=java
java(
"""
import java.util.List;
public class Constants {
public static final String FOO = "FOO";
}
class A {
private <T> void r(T e) {
e.toString().equals(Constants.FOO);
}
}
""",
"""
import java.util.List;
public class Constants {
public static final String FOO = "FOO";
}
class A {
private <T> void r(T e) {
Constants.FOO.equals(e.toString());
}
}
"""
)
);
}

@Test
void lambda() {
rewriteRun(
//language=java
java(
"""
import java.util.List;
public class Constants {
public static final String FOO = "FOO";
}
class A {
private void isFoo(List<Object> list) {
list.stream().filter(c -> c.toString().contentEquals(Constants.FOO));
}
}
""",
"""
import java.util.List;
public class Constants {
public static final String FOO = "FOO";
}
class A {
private void isFoo(List<Object> list) {
list.stream().filter(c -> Constants.FOO.contentEquals(c.toString()));
}
}
"""
)
);
}

@Test
@Disabled("Not yet supported")
void lambdaGenerics() {
rewriteRun(
//language=java
java(
"""
import java.util.List;
public class Constants {
public static final String FOO = "FOO";
}
class C {
boolean c(String k) {
return true;
}
Object get(String k) {
return null;
}
void r(String k, String v) {
}
}
class A {
private <T extends C> void rr(List<String> f, T e) {
f.stream()
.filter(fn -> e.c(fn))
.forEach(fn -> e.get(fn).equals(Constants.FOO));
}
}
""",
"""
import java.util.List;
public class Constants {
public static final String FOO = "FOO";
}
class C {
boolean c(String k) {
return true;
}
Object get(String k) {
return null;
}
void r(String k, String v) {
}
}
class A {
private <T extends C> void rr(List<String> f, T e) {
f.stream()
.filter(fn -> e.c(fn))
.forEach(fn -> Constants.FOO.equals(e.get(fn)));
}
}
"""
)
);
}

@Test
void nonStaticNonFinalNoChange() {
rewriteRun(
Expand Down

0 comments on commit 34d6589

Please sign in to comment.