Skip to content

Commit

Permalink
add type mapping test for setters on properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 committed Oct 6, 2023
1 parent 12732f0 commit 9c61e46
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/openrewrite/kotlin/KotlinTypeGoat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ abstract class KotlinTypeGoat<T, S> {
val gettableField: Int
get() = 10

var settableField : String = ""
set ( value ) {
field = value
}

// abstract class InheritedKotlinTypeGoat<T, U> : KotlinTypeGoat<T, U>() where U : PT<U>, U : C

enum class EnumTypeA {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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"))
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/KotlinTypeGoat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ abstract class KotlinTypeGoat<T, S> {
val gettableField: Int
get() = 10

var settableField : String = ""
set ( value ) {
field = value
}

// abstract class InheritedKotlinTypeGoat<T, U> : KotlinTypeGoat<T, U>() where U : PT<U>, U : C

enum class EnumTypeA {
Expand Down

0 comments on commit 9c61e46

Please sign in to comment.