From c414e02d9cba5793908decdd68bf8838a70ea1a6 Mon Sep 17 00:00:00 2001 From: Jonathan Schneider Date: Tue, 10 Jan 2023 21:24:51 -0500 Subject: [PATCH] DeclarationSiteTypeVariance doesn't change bounds when they refer to final classes / issue #2621 --- .../DeclarationSiteTypeVarianceTest.java | 28 +++++++++++++++++++ .../cleanup/DeclarationSiteTypeVariance.java | 12 ++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/cleanup/DeclarationSiteTypeVarianceTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/cleanup/DeclarationSiteTypeVarianceTest.java index deb00b0ecbf..d1ef699d93d 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/cleanup/DeclarationSiteTypeVarianceTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/cleanup/DeclarationSiteTypeVarianceTest.java @@ -116,4 +116,32 @@ void test(Function f) { ) ); } + + @Test + void finalClasses() { + rewriteRun( + java( + """ + final class In {} + interface Out {} + """ + ), + java( + """ + import java.util.function.Function; + class Test { + void test(Function f) { + } + } + """, + """ + import java.util.function.Function; + class Test { + void test(Function f) { + } + } + """ + ) + ); + } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/cleanup/DeclarationSiteTypeVariance.java b/rewrite-java/src/main/java/org/openrewrite/java/cleanup/DeclarationSiteTypeVariance.java index 6280546fa16..3bbaa47e0d2 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/cleanup/DeclarationSiteTypeVariance.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/cleanup/DeclarationSiteTypeVariance.java @@ -104,9 +104,15 @@ private J.ParameterizedType useDeclarationSiteVariance(J.ParameterizedType pt, V variance == VariantTypeSpec.Variance.INVARIANT) { return tp; } - for (String excludedBound : excludedBounds) { - JavaType.FullyQualified fq = TypeUtils.asFullyQualified(tp.getType()); - if (fq != null && StringUtils.matchesGlob(fq.getFullyQualifiedName(), excludedBound)) { + + JavaType.FullyQualified fq = TypeUtils.asFullyQualified(tp.getType()); + if (fq != null) { + for (String excludedBound : excludedBounds) { + if (StringUtils.matchesGlob(fq.getFullyQualifiedName(), excludedBound)) { + return tp; + } + } + if (fq.getFlags().contains(Flag.Final)) { return tp; } }