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

UseLambdaForFunctionalInterface recipe should not convert when code uses a static field from enum constructor #415

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
StringBuilder templateBuilder = new StringBuilder();
J.MethodDeclaration methodDeclaration = (J.MethodDeclaration) n.getBody().getStatements().get(0);

if (isLambdaInEnumConstructorWithStaticField(getCursor(), methodDeclaration)) {
return n;
}

// If the functional interface method has type parameters, we can't replace it with a lambda.
if (methodDeclaration.getTypeParameters() != null && !methodDeclaration.getTypeParameters().isEmpty()) {
return n;
Expand Down Expand Up @@ -137,6 +141,37 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
return n;
}

private boolean isLambdaInEnumConstructorWithStaticField(Cursor cursor, J.MethodDeclaration md) {
if (md.getBody() == null) {
return false;
}

for (Statement s : md.getBody().getStatements()) {
J.MethodInvocation e = null;
if (s instanceof J.VariableDeclarations && ((J.VariableDeclarations) s).getVariables().size() == 1 && ((J.VariableDeclarations) s).getVariables().get(0).getInitializer() instanceof J.MethodInvocation) {
e = (J.MethodInvocation) ((J.VariableDeclarations) s).getVariables().get(0).getInitializer();
} else if (s instanceof J.MethodInvocation) {
e = (J.MethodInvocation) s;
}

if (e != null && e.getSelect() instanceof J.Identifier) {
JavaType.Variable v = ((J.Identifier) e.getSelect()).getFieldType();
if (v != null && v.hasFlags(Flag.Static)) {
JavaType owner = v.getOwner();
if (owner instanceof JavaType.Class && ((JavaType.Class) owner).getKind() == JavaType.Class.Kind.Enum) {
J.MethodDeclaration parentMethodDeclaration = cursor.dropParentUntil(p -> p instanceof J.MethodDeclaration).getValue();
J.ClassDeclaration parentClass = cursor.dropParentUntil(p -> p instanceof J.ClassDeclaration).getValue();
if (parentMethodDeclaration.isConstructor() && owner.equals(parentClass.getType())) {
return true;
}
}
}
}
}

return false;
}

private J maybeAddCast(J.Lambda lambda, J.NewClass original) {
J parent = getCursor().getParentTreeCursor().getValue();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,4 +774,144 @@ public <T> List<T> call() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/413")
void dontUseLambdaWhenEnumAccessesStaticFieldFromConstructor() {
rewriteRun(
//language=java
java(
"""
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
enum Test {
A, B;

private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");

Test() {
Runnable r = new Runnable() {
@Override
public void run() {
DATE_FORMAT.format(LocalDate.now());
}
};
}
}
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/413")
void dontUseLambdaWhenEnumAccessesStaticFieldFromConstructorWithAssignment() {
rewriteRun(
//language=java
java(
"""
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
enum Test {
A, B;

private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");

Test() {
Runnable r = new Runnable() {
@Override
public void run() {
String s = DATE_FORMAT.format(LocalDate.now());
}
};
}
}
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/413")
void doUseLambdaWhenEnumAccessesNonStaticFieldFromConstructor() {
rewriteRun(
//language=java
java(
"""
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
enum Test {
A, B;

private final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");

Test() {
Runnable r = new Runnable() {
@Override
public void run() {
DATE_FORMAT.format(LocalDate.now());
}
};
}
}
""",
"""
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
enum Test {
A, B;

private final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");

Test() {
Runnable r = () ->
DATE_FORMAT.format(LocalDate.now());
}
}
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/413")
void doUseLambdaWhenEnumAccessesStaticFieldFromMethod() {
rewriteRun(
//language=java
java(
"""
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
enum Test {
A, B;

private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");

void test() {
Runnable r = new Runnable() {
@Override
public void run() {
DATE_FORMAT.format(LocalDate.now());
}
};
}
}
""",
"""
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
enum Test {
A, B;

private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd");

void test() {
Runnable r = () ->
DATE_FORMAT.format(LocalDate.now());
}
}
"""
)
);
}
}
Loading