diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveRedundantTypeCast.java b/src/main/java/org/openrewrite/staticanalysis/RemoveRedundantTypeCast.java index 0ce786e73..44620d8dd 100644 --- a/src/main/java/org/openrewrite/staticanalysis/RemoveRedundantTypeCast.java +++ b/src/main/java/org/openrewrite/staticanalysis/RemoveRedundantTypeCast.java @@ -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()); diff --git a/src/test/java/org/openrewrite/staticanalysis/AvoidBoxedBooleanExpressionsTest.java b/src/test/java/org/openrewrite/staticanalysis/AvoidBoxedBooleanExpressionsTest.java index 1645e09c6..8952cfe4d 100644 --- a/src/test/java/org/openrewrite/staticanalysis/AvoidBoxedBooleanExpressionsTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/AvoidBoxedBooleanExpressionsTest.java @@ -22,7 +22,7 @@ import static org.openrewrite.java.Assertions.java; -public class AvoidBoxedBooleanExpressionsTest implements RewriteTest { +class AvoidBoxedBooleanExpressionsTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { diff --git a/src/test/java/org/openrewrite/staticanalysis/BigDecimalDoubleConstructorTest.java b/src/test/java/org/openrewrite/staticanalysis/BigDecimalDoubleConstructorTest.java index 5ab866cde..508b68fb0 100644 --- a/src/test/java/org/openrewrite/staticanalysis/BigDecimalDoubleConstructorTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/BigDecimalDoubleConstructorTest.java @@ -23,7 +23,7 @@ import static org.openrewrite.java.Assertions.java; @SuppressWarnings("UnpredictableBigDecimalConstructorCall") -public class BigDecimalDoubleConstructorTest implements RewriteTest { +class BigDecimalDoubleConstructorTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { diff --git a/src/test/java/org/openrewrite/staticanalysis/CommonStaticAnalysisIssuesPerformanceTest.java b/src/test/java/org/openrewrite/staticanalysis/CommonStaticAnalysisIssuesPerformanceTest.java index f67ca3260..f4bffd037 100644 --- a/src/test/java/org/openrewrite/staticanalysis/CommonStaticAnalysisIssuesPerformanceTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/CommonStaticAnalysisIssuesPerformanceTest.java @@ -22,7 +22,7 @@ import static org.openrewrite.java.Assertions.java; -public class CommonStaticAnalysisIssuesPerformanceTest implements RewriteTest { +class CommonStaticAnalysisIssuesPerformanceTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { diff --git a/src/test/java/org/openrewrite/staticanalysis/CompareEnumsWithEqualityOperatorTest.java b/src/test/java/org/openrewrite/staticanalysis/CompareEnumsWithEqualityOperatorTest.java index a9d58c481..ab31eb487 100644 --- a/src/test/java/org/openrewrite/staticanalysis/CompareEnumsWithEqualityOperatorTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/CompareEnumsWithEqualityOperatorTest.java @@ -23,7 +23,7 @@ import static org.openrewrite.java.Assertions.java; -public class CompareEnumsWithEqualityOperatorTest implements RewriteTest { +class CompareEnumsWithEqualityOperatorTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec.recipe(new CompareEnumsWithEqualityOperator()); diff --git a/src/test/java/org/openrewrite/staticanalysis/FixStringFormatExpressionsTest.java b/src/test/java/org/openrewrite/staticanalysis/FixStringFormatExpressionsTest.java index 5a51c2bad..e2a37c2ed 100644 --- a/src/test/java/org/openrewrite/staticanalysis/FixStringFormatExpressionsTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/FixStringFormatExpressionsTest.java @@ -23,7 +23,7 @@ import static org.openrewrite.java.Assertions.java; @SuppressWarnings("ALL") -public class FixStringFormatExpressionsTest implements RewriteTest { +class FixStringFormatExpressionsTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec.recipe(new FixStringFormatExpressions()); diff --git a/src/test/java/org/openrewrite/staticanalysis/RemoveRedundantTypeCastTest.java b/src/test/java/org/openrewrite/staticanalysis/RemoveRedundantTypeCastTest.java index 652079347..3a518a6fc 100644 --- a/src/test/java/org/openrewrite/staticanalysis/RemoveRedundantTypeCastTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/RemoveRedundantTypeCastTest.java @@ -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; @@ -394,6 +395,7 @@ class ExtendTest extends Test { @Test void lambdaWithComplexTypeInference() { rewriteRun( + //language=java java( """ import java.util.LinkedHashMap; @@ -425,6 +427,7 @@ public MapDropdownChoice(Supplier> choiceMap) { @Test void returnPrimitiveIntToWrapperLong() { rewriteRun( + //language=java java( """ class Test { @@ -440,6 +443,7 @@ Long method() { @Test void castWildcard() { rewriteRun( + //language=java java( """ import java.util.ArrayList; @@ -459,6 +463,7 @@ void method() { @Test void removeImport() { rewriteRun( + //language=java java( """ import java.util.ArrayList; @@ -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) -> { + }); + } + } + """ + ) + ); + } } diff --git a/src/test/java/org/openrewrite/staticanalysis/RemoveSystemOutPrintlnTest.java b/src/test/java/org/openrewrite/staticanalysis/RemoveSystemOutPrintlnTest.java index 9ca93f3df..5de77314c 100644 --- a/src/test/java/org/openrewrite/staticanalysis/RemoveSystemOutPrintlnTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/RemoveSystemOutPrintlnTest.java @@ -22,7 +22,7 @@ import static org.openrewrite.java.Assertions.java; -public class RemoveSystemOutPrintlnTest implements RewriteTest { +class RemoveSystemOutPrintlnTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { diff --git a/src/test/java/org/openrewrite/staticanalysis/RemoveToStringCallsFromArrayInstancesTest.java b/src/test/java/org/openrewrite/staticanalysis/RemoveToStringCallsFromArrayInstancesTest.java index 08846e618..8053a052c 100644 --- a/src/test/java/org/openrewrite/staticanalysis/RemoveToStringCallsFromArrayInstancesTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/RemoveToStringCallsFromArrayInstancesTest.java @@ -24,7 +24,7 @@ import static org.openrewrite.java.Assertions.java; @SuppressWarnings({"ImplicitArrayToString", "UnnecessaryLocalVariable", "RedundantStringFormatCall", "MalformedFormatString", "PrimitiveArrayArgumentToVarargsMethod"}) -public class RemoveToStringCallsFromArrayInstancesTest implements RewriteTest { +class RemoveToStringCallsFromArrayInstancesTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec.recipe(new RemoveToStringCallsFromArrayInstances()); diff --git a/src/test/java/org/openrewrite/staticanalysis/ReplaceRedundantFormatWithPrintfTest.java b/src/test/java/org/openrewrite/staticanalysis/ReplaceRedundantFormatWithPrintfTest.java index a1cbbb96c..c0b0c3af1 100644 --- a/src/test/java/org/openrewrite/staticanalysis/ReplaceRedundantFormatWithPrintfTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/ReplaceRedundantFormatWithPrintfTest.java @@ -22,7 +22,7 @@ import static org.openrewrite.java.Assertions.java; -public class ReplaceRedundantFormatWithPrintfTest implements RewriteTest { +class ReplaceRedundantFormatWithPrintfTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { diff --git a/src/test/java/org/openrewrite/staticanalysis/ReplaceStackWithDequeTest.java b/src/test/java/org/openrewrite/staticanalysis/ReplaceStackWithDequeTest.java index 33ef91c5b..5455113d0 100644 --- a/src/test/java/org/openrewrite/staticanalysis/ReplaceStackWithDequeTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/ReplaceStackWithDequeTest.java @@ -24,7 +24,7 @@ import static org.openrewrite.java.Assertions.java; -public class ReplaceStackWithDequeTest implements RewriteTest { +class ReplaceStackWithDequeTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec.recipe(new ReplaceStackWithDeque()); diff --git a/src/test/java/org/openrewrite/staticanalysis/SimplifyDurationCreationUnitsTest.java b/src/test/java/org/openrewrite/staticanalysis/SimplifyDurationCreationUnitsTest.java index 366a19004..c2f210337 100644 --- a/src/test/java/org/openrewrite/staticanalysis/SimplifyDurationCreationUnitsTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/SimplifyDurationCreationUnitsTest.java @@ -23,7 +23,7 @@ import static org.openrewrite.java.Assertions.java; -public class SimplifyDurationCreationUnitsTest implements RewriteTest { +class SimplifyDurationCreationUnitsTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { diff --git a/src/test/java/org/openrewrite/staticanalysis/UpperCaseLiteralSuffixesTest.java b/src/test/java/org/openrewrite/staticanalysis/UpperCaseLiteralSuffixesTest.java index cb6c0b6c0..7d54c7354 100644 --- a/src/test/java/org/openrewrite/staticanalysis/UpperCaseLiteralSuffixesTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/UpperCaseLiteralSuffixesTest.java @@ -24,7 +24,7 @@ import static org.openrewrite.java.Assertions.java; @SuppressWarnings("ALL") -public class UpperCaseLiteralSuffixesTest implements RewriteTest { +class UpperCaseLiteralSuffixesTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { diff --git a/src/test/java/org/openrewrite/staticanalysis/UseForEachRemoveInsteadOfSetRemoveAllTest.java b/src/test/java/org/openrewrite/staticanalysis/UseForEachRemoveInsteadOfSetRemoveAllTest.java index 1d2c085f4..1e34bf201 100644 --- a/src/test/java/org/openrewrite/staticanalysis/UseForEachRemoveInsteadOfSetRemoveAllTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/UseForEachRemoveInsteadOfSetRemoveAllTest.java @@ -23,7 +23,7 @@ import static org.openrewrite.java.Assertions.java; @SuppressWarnings("ALL") -public class UseForEachRemoveInsteadOfSetRemoveAllTest implements RewriteTest { +class UseForEachRemoveInsteadOfSetRemoveAllTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { diff --git a/src/test/java/org/openrewrite/staticanalysis/UseListSortTest.java b/src/test/java/org/openrewrite/staticanalysis/UseListSortTest.java index bd9c79d9f..378003039 100644 --- a/src/test/java/org/openrewrite/staticanalysis/UseListSortTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/UseListSortTest.java @@ -23,7 +23,7 @@ import static org.openrewrite.java.Assertions.java; @SuppressWarnings("ALL") -public class UseListSortTest implements RewriteTest { +class UseListSortTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec.recipe(new UseListSort()); diff --git a/src/test/java/org/openrewrite/staticanalysis/groovy/MinimumSwitchCasesTest.java b/src/test/java/org/openrewrite/staticanalysis/groovy/MinimumSwitchCasesTest.java index c10fa95a5..0256565a0 100644 --- a/src/test/java/org/openrewrite/staticanalysis/groovy/MinimumSwitchCasesTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/groovy/MinimumSwitchCasesTest.java @@ -25,7 +25,7 @@ import static org.openrewrite.groovy.Assertions.groovy; -public class MinimumSwitchCasesTest implements RewriteTest { +class MinimumSwitchCasesTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { diff --git a/src/test/java/org/openrewrite/staticanalysis/groovy/ReplaceLambdaWithMethodReferenceTest.java b/src/test/java/org/openrewrite/staticanalysis/groovy/ReplaceLambdaWithMethodReferenceTest.java index 7ab9f7b64..af0a36d6c 100644 --- a/src/test/java/org/openrewrite/staticanalysis/groovy/ReplaceLambdaWithMethodReferenceTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/groovy/ReplaceLambdaWithMethodReferenceTest.java @@ -23,7 +23,7 @@ import static org.openrewrite.groovy.Assertions.groovy; -public class ReplaceLambdaWithMethodReferenceTest implements RewriteTest { +class ReplaceLambdaWithMethodReferenceTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) {