Skip to content

Commit

Permalink
Fixed type mapping for getters on properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
traceyyoshima committed Oct 6, 2023
1 parent 3bf91df commit 12732f0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/main/java/org/openrewrite/kotlin/KotlinTypeGoat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/
@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "unused")

// Whenever this class is changed, make a corresponding change in JavaTypeGoat in the main resources folder.
// Whenever this class is changed, make a corresponding change in KotlinTypeGoat in the main resources folder.
package org.openrewrite.kotlin

import java.lang.Object

// TODO: FIX ME. Files needs to declare fields and methods to assert type mapping.
@AnnotationWithRuntimeRetention
@AnnotationWithSourceRetention
abstract class KotlinTypeGoat<T, S> {
Expand All @@ -28,6 +29,9 @@ abstract class KotlinTypeGoat<T, S> {

val field: Int = 10

val gettableField: Int
get() = 10

// 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 @@ -500,7 +500,7 @@ class KotlinTypeMapping(typeCache: JavaTypeCache, firSession: FirSession) : Java
): JavaType.Method? {
val methodSymbol = function?.symbol
if (methodSymbol != null) {
val signature = signatureBuilder.methodDeclarationSignature(function.symbol)
val signature = signatureBuilder.methodDeclarationSignature(function.symbol, ownerFallBack)
val existing = typeCache.get<JavaType.Method>(signature)
if (existing != null) {
return existing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.fir.java.declarations.FirJavaField
import org.jetbrains.kotlin.fir.java.declarations.FirJavaMethod
import org.jetbrains.kotlin.fir.java.declarations.FirJavaValueParameter
import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
import org.jetbrains.kotlin.fir.resolve.toFirRegularClass
import org.jetbrains.kotlin.fir.resolve.toFirRegularClassSymbol
import org.jetbrains.kotlin.fir.resolve.toSymbol
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
Expand Down Expand Up @@ -68,7 +69,7 @@ class KotlinTypeSignatureBuilder(private val firSession: FirSession) : JavaTypeS
}

is FirFunction -> {
return methodDeclarationSignature(type.symbol)
return methodDeclarationSignature(type.symbol, ownerSymbol)
}

is FirVariable -> {
Expand Down Expand Up @@ -640,7 +641,8 @@ class KotlinTypeSignatureBuilder(private val firSession: FirSession) : JavaTypeS
* Generate the method declaration signature.
*/
@OptIn(SymbolInternals::class)
fun methodDeclarationSignature(symbol: FirFunctionSymbol<out FirFunction>): String {
fun methodDeclarationSignature(symbol: FirFunctionSymbol<out FirFunction>,
ownerSymbol: @Nullable FirBasedSymbol<*>?): String {
var s: String =
when {
symbol is FirConstructorSymbol -> {
Expand All @@ -650,7 +652,10 @@ class KotlinTypeSignatureBuilder(private val firSession: FirSession) : JavaTypeS
classSignature(symbol.dispatchReceiverType!!)
}
symbol.getOwnerLookupTag() != null && symbol.getOwnerLookupTag()!!.toFirRegularClassSymbol(firSession) != null -> {
classSignature(symbol.getOwnerLookupTag()!!.toFirRegularClassSymbol(firSession)!!)
classSignature(symbol.getOwnerLookupTag()!!.toFirRegularClass(firSession)!!)
}
ownerSymbol != null -> {
signature(ownerSymbol.fir)
}
else -> {
"{undefined}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public String constructorSignature() {
.map(FirFunction.class::cast)
.findFirst()
.orElseThrow()
.getSymbol());
.getSymbol(), null);
}

public Object innerClassSignature(String innerClassSimpleName) {
Expand All @@ -95,6 +95,21 @@ public String fieldSignature(String field) {
.getSymbol(), 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);
}
return signatureBuilder().methodDeclarationSignature(getter.getGetter().getSymbol(), getCompiledSource().getSymbol());
}

public Object firstMethodParameterSignature(String methodName) {
return signatureBuilder().signature(getCompiledSource().getDeclarations().stream()
.map(FirRegularClass.class::cast)
Expand Down Expand Up @@ -127,7 +142,7 @@ public String methodSignature(String methodName) {
.filter(it -> methodName.equals(it.getName().asString()))
.findFirst()
.orElseThrow()
.getSymbol());
.getSymbol(), null);
}

@Test
Expand All @@ -148,6 +163,12 @@ void fieldType() {
.isEqualTo("org.openrewrite.kotlin.KotlinTypeGoat{name=field,type=kotlin.Int}");
}

@Test
void gettableField() {
assertThat(fieldPropertyGetterSignature("gettableField"))
.isEqualTo("org.openrewrite.kotlin.KotlinTypeGoat{name=accessor,return=kotlin.Int,parameters=[]}");
}

@Test
void classSignature() {
assertThat(firstMethodParameterSignature("clazz"))
Expand Down
6 changes: 5 additions & 1 deletion src/test/resources/KotlinTypeGoat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/
@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "unused")

// Whenever this class is changed, make a corresponding change in JavaTypeGoat in the main java source set.
// Whenever this class is changed, make a corresponding change in KotlinTypeGoat in the main java source set.
package org.openrewrite.kotlin

import java.lang.Object

// TODO: FIX ME. Files needs to declare fields and methods to assert type mapping.
@AnnotationWithRuntimeRetention
@AnnotationWithSourceRetention
abstract class KotlinTypeGoat<T, S> {
Expand All @@ -28,6 +29,9 @@ abstract class KotlinTypeGoat<T, S> {

val field: Int = 10

val gettableField: Int
get() = 10

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

enum class EnumTypeA {
Expand Down

0 comments on commit 12732f0

Please sign in to comment.