From 9c61e461cc9565df17d3d7ed482214ecb6d0cf49 Mon Sep 17 00:00:00 2001 From: Kun Li Date: Fri, 6 Oct 2023 11:12:09 -0700 Subject: [PATCH] add type mapping test for setters on properties. --- .../org/openrewrite/kotlin/KotlinTypeGoat.kt | 5 +++ .../KotlinTypeSignatureBuilderTest.java | 42 ++++++++++++++----- src/test/resources/KotlinTypeGoat.kt | 5 +++ 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/openrewrite/kotlin/KotlinTypeGoat.kt b/src/main/java/org/openrewrite/kotlin/KotlinTypeGoat.kt index c14953365..cf7941252 100644 --- a/src/main/java/org/openrewrite/kotlin/KotlinTypeGoat.kt +++ b/src/main/java/org/openrewrite/kotlin/KotlinTypeGoat.kt @@ -32,6 +32,11 @@ abstract class KotlinTypeGoat { val gettableField: Int get() = 10 + var settableField : String = "" + set ( value ) { + field = value + } + // abstract class InheritedKotlinTypeGoat : KotlinTypeGoat() where U : PT, U : C enum class EnumTypeA { diff --git a/src/test/java/org/openrewrite/kotlin/KotlinTypeSignatureBuilderTest.java b/src/test/java/org/openrewrite/kotlin/KotlinTypeSignatureBuilderTest.java index c3091b4aa..fe7e0a042 100644 --- a/src/test/java/org/openrewrite/kotlin/KotlinTypeSignatureBuilderTest.java +++ b/src/test/java/org/openrewrite/kotlin/KotlinTypeSignatureBuilderTest.java @@ -24,6 +24,7 @@ import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.Parser; import org.openrewrite.internal.StringUtils; +import org.openrewrite.internal.lang.Nullable; import org.openrewrite.kotlin.internal.CompiledSource; import org.openrewrite.tree.ParsingExecutionContextView; @@ -95,19 +96,32 @@ public String fieldSignature(String field) { .getSymbol(), null); } + @Nullable + public FirProperty getProperty(String field) { + return getCompiledSource().getDeclarations().stream() + .map(FirRegularClass.class::cast) + .flatMap(it -> it.getDeclarations().stream()) + .filter(FirProperty.class::isInstance) + .map(FirProperty.class::cast) + .filter(it -> field.equals(it.getName().asString())) + .findFirst() + .orElse(null); + } + public String fieldPropertyGetterSignature(String field) { - FirProperty getter = getCompiledSource().getDeclarations().stream() - .map(FirRegularClass.class::cast) - .flatMap(it -> it.getDeclarations().stream()) - .filter(FirProperty.class::isInstance) - .map(FirProperty.class::cast) - .filter(it -> field.equals(it.getName().asString())) - .findFirst() - .orElseThrow(); - if (getter.getGetter() == null) { - throw new UnsupportedOperationException("No getter for " + field); + FirProperty property = getProperty(field); + if (property == null || property.getGetter() == null) { + throw new UnsupportedOperationException("No filed or getter for " + field); + } + return signatureBuilder().methodDeclarationSignature(property.getGetter().getSymbol(), getCompiledSource().getSymbol()); + } + + public String fieldPropertySetterSignature(String field) { + FirProperty property = getProperty(field); + if (property == null || property.getSetter() == null) { + throw new UnsupportedOperationException("No filed or setter for " + field); } - return signatureBuilder().methodDeclarationSignature(getter.getGetter().getSymbol(), getCompiledSource().getSymbol()); + return signatureBuilder().methodDeclarationSignature(property.getSetter().getSymbol(), getCompiledSource().getSymbol()); } public Object firstMethodParameterSignature(String methodName) { @@ -169,6 +183,12 @@ void gettableField() { .isEqualTo("org.openrewrite.kotlin.KotlinTypeGoat{name=accessor,return=kotlin.Int,parameters=[]}"); } + @Test + void settableField() { + assertThat(fieldPropertySetterSignature("settableField")) + .isEqualTo("org.openrewrite.kotlin.KotlinTypeGoat{name=accessor,return=kotlin.Unit,parameters=[kotlin.String]}"); + } + @Test void classSignature() { assertThat(firstMethodParameterSignature("clazz")) diff --git a/src/test/resources/KotlinTypeGoat.kt b/src/test/resources/KotlinTypeGoat.kt index 6ff52ce55..e9de69acc 100644 --- a/src/test/resources/KotlinTypeGoat.kt +++ b/src/test/resources/KotlinTypeGoat.kt @@ -32,6 +32,11 @@ abstract class KotlinTypeGoat { val gettableField: Int get() = 10 + var settableField : String = "" + set ( value ) { + field = value + } + // abstract class InheritedKotlinTypeGoat : KotlinTypeGoat() where U : PT, U : C enum class EnumTypeA {