Skip to content

Commit

Permalink
Do not remove type casts from lambdas or method references (#257)
Browse files Browse the repository at this point in the history
In response to a reported issue.
  • Loading branch information
timtebeek authored Feb 12, 2024
1 parent 3014a60 commit a049e1c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,20 @@ public J visitTypeCast(J.TypeCast typeCast, ExecutionContext ctx) {
JavaType expressionType = visitedTypeCast.getExpression().getType();
JavaType castType = visitedTypeCast.getType();

if (targetType == null ||
(targetType instanceof JavaType.Primitive || castType instanceof JavaType.Primitive) && castType != expressionType ||
(typeCast.getExpression() instanceof J.Lambda || typeCast.getExpression() instanceof J.MemberReference) && castType instanceof JavaType.Parameterized) {
if (targetType == null) {
return visitedTypeCast;
}
if ((targetType instanceof JavaType.Primitive || castType instanceof JavaType.Primitive) && castType != expressionType) {
return visitedTypeCast;
}
if (typeCast.getExpression() instanceof J.Lambda || typeCast.getExpression() instanceof J.MemberReference) {
// Not currently supported, this will be more accurate with dataflow analysis.
return visitedTypeCast;
} else if (!(targetType instanceof JavaType.Array) && TypeUtils.isOfClassType(targetType, "java.lang.Object") ||
TypeUtils.isOfType(targetType, expressionType) ||
TypeUtils.isAssignableTo(targetType, expressionType)) {
}

if (!(targetType instanceof JavaType.Array) && TypeUtils.isOfClassType(targetType, "java.lang.Object") ||
TypeUtils.isOfType(targetType, expressionType) ||
TypeUtils.isAssignableTo(targetType, expressionType)) {
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(castType);
if (fullyQualified != null) {
maybeRemoveImport(fullyQualified.getFullyQualifiedName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

Expand Down Expand Up @@ -394,6 +395,7 @@ class ExtendTest extends Test {
@Test
void lambdaWithComplexTypeInference() {
rewriteRun(
//language=java
java(
"""
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -425,6 +427,7 @@ public MapDropdownChoice(Supplier<? extends Map<K, ? extends V>> choiceMap) {
@Test
void returnPrimitiveIntToWrapperLong() {
rewriteRun(
//language=java
java(
"""
class Test {
Expand All @@ -440,6 +443,7 @@ Long method() {
@Test
void castWildcard() {
rewriteRun(
//language=java
java(
"""
import java.util.ArrayList;
Expand All @@ -459,6 +463,7 @@ void method() {
@Test
void removeImport() {
rewriteRun(
//language=java
java(
"""
import java.util.ArrayList;
Expand All @@ -482,4 +487,45 @@ List method(List list) {
)
);
}

@Test
void retainCastInMarshaller() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion()
//language=java
.dependsOn(
"""
package org.glassfish.jaxb.core.marshaller;
import java.io.IOException;
import java.io.Writer;
public interface CharacterEscapeHandler {
void escape( char[] ch, int start, int length, boolean isAttVal, Writer out ) throws IOException;\s
}
""",
"""
package javax.xml.bind;
public interface Marshaller {
void setProperty(String var1, Object var2);
}
"""
)
),
//language=java
java(
"""
import javax.xml.bind.Marshaller;
import org.glassfish.jaxb.core.marshaller.CharacterEscapeHandler;
class Foo {
void bar(Marshaller marshaller) {
marshaller.setProperty("org.glassfish.jaxb.characterEscapeHandler", (CharacterEscapeHandler) (ch, start, length, isAttVal, out) -> {
});
}
}
"""
)
);
}
}

0 comments on commit a049e1c

Please sign in to comment.