Skip to content

Commit

Permalink
Merge branch 'main' into fix/convert-lambda-with-method-invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Feb 19, 2024
2 parents 81aab6f + a049e1c commit f00d7ea
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 21 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 @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
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) -> {
});
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f00d7ea

Please sign in to comment.