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

Lambda #419

Merged
merged 25 commits into from
Dec 27, 2024
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
Loading