Skip to content

Commit

Permalink
File owner types (#336)
Browse files Browse the repository at this point in the history
* Fixed variable owner names.
Added signature builder tests for top level properties and functions.

* Added type mapping tests for top level fields and methods.
  • Loading branch information
traceyyoshima authored Oct 9, 2023
1 parent 15fe58b commit 7dcee5a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 20 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/openrewrite/kotlin/KotlinTypeGoat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ package org.openrewrite.kotlin

import java.lang.Object

// TODO: FIX ME. Files needs to declare fields and methods to assert type mapping.
const val field = 10
fun function() {}

@AnnotationWithRuntimeRetention
@AnnotationWithSourceRetention
abstract class KotlinTypeGoat<T, S> where S: PT<S>, S: C {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,8 @@ class KotlinTypeSignatureBuilder(private val firSession: FirSession) : JavaTypeS
}
} else if (ownerSymbol is FirFunctionSymbol<*>) {
owner = methodDeclarationSignature(ownerSymbol, null)
} else if (ownerSymbol is FirFileSymbol) {
owner = convertFileNameToFqn(ownerSymbol.fir.name)
} else if (ownerSymbol != null) {
owner = classSignature(ownerSymbol.fir)
}
Expand Down
52 changes: 42 additions & 10 deletions src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*/
package org.openrewrite.kotlin;

import org.jetbrains.kotlin.fir.declarations.FirProperty;
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.Parser;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Flag;
Expand All @@ -28,9 +31,13 @@
import org.openrewrite.kotlin.tree.K;
import org.openrewrite.test.RewriteTest;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.ExecutionContext.REQUIRE_PRINT_EQUALS_INPUT;
Expand All @@ -40,26 +47,26 @@
@SuppressWarnings("ConstantConditions")
public class KotlinTypeMappingTest {
private static final String goat = StringUtils.readFully(KotlinTypeMappingTest.class.getResourceAsStream("/KotlinTypeGoat.kt"));

private static final K.CompilationUnit cu;
private static final K.ClassDeclaration goatClassDeclaration;

static {
InMemoryExecutionContext ctx = new InMemoryExecutionContext();
ctx.putMessage(REQUIRE_PRINT_EQUALS_INPUT, false);
//noinspection OptionalGetWithoutIsPresent
goatClassDeclaration = requireNonNull(((K.CompilationUnit) KotlinParser.builder()
.logCompilationWarningsAndErrors(true)
.build()
.parse(ctx, goat)
.findFirst()
.get())
cu = (K.CompilationUnit) KotlinParser.builder()
.logCompilationWarningsAndErrors(true)
.build()
.parseInputs(singletonList(new Parser.Input(Paths.get("KotlinTypeGoat.kt"), () -> new ByteArrayInputStream(goat.getBytes(StandardCharsets.UTF_8)))), null, ctx)
.findFirst()
.orElseThrow();

goatClassDeclaration = cu
.getStatements()
.stream()
.filter(K.ClassDeclaration.class::isInstance)
.findFirst()
.map(K.ClassDeclaration.class::cast)
.orElseThrow()
);
.orElseThrow();
}

private static final JavaType.Parameterized goatType =
Expand Down Expand Up @@ -128,6 +135,31 @@ void fieldType() {
assertThat(property.getSetter().getMethodType().toString().substring(declaringType.toString().length())).isEqualTo("{name=accessor,return=kotlin.Unit,parameters=[kotlin.Int]}");
}

@Test
void fileField() {
J.VariableDeclarations.NamedVariable nv = cu.getStatements().stream()
.filter(it -> it instanceof J.VariableDeclarations)
.flatMap(it -> ((J.VariableDeclarations) it).getVariables().stream())
.filter(it -> "field".equals(it.getSimpleName())).findFirst().orElseThrow();

assertThat(nv.getName().getType().toString()).isEqualTo("kotlin.Int");
assertThat(nv.getName().getFieldType()).isEqualTo(nv.getVariableType());
assertThat(nv.getVariableType().toString())
.isEqualTo("KotlinTypeGoatKt{name=field,type=kotlin.Int}");
}

@Test
void fileFunction() {
J.MethodDeclaration md = cu.getStatements().stream()
.filter(it -> it instanceof J.MethodDeclaration)
.map(J.MethodDeclaration.class::cast)
.filter(it -> "function".equals(it.getSimpleName())).findFirst().orElseThrow();

assertThat(md.getName().getType()).isEqualTo(md.getMethodType());
assertThat(md.getMethodType().toString())
.isEqualTo("KotlinTypeGoatKt{name=function,return=kotlin.Unit,parameters=[]}");
}

@Test
void kotlinAnyHasNoSuperType() {
assertThat(goatType.getSupertype().getSupertype()).isNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class KotlinTypeSignatureBuilderTest {
.moduleName("test")
.build()
.parse(singletonList(new Parser.Input(Paths.get("KotlinTypeGoat.kt"), () -> new ByteArrayInputStream(goat.getBytes(StandardCharsets.UTF_8)))), disposable,
new ParsingExecutionContextView(new InMemoryExecutionContext(Throwable::printStackTrace)));;
new ParsingExecutionContextView(new InMemoryExecutionContext(Throwable::printStackTrace)));

@AfterAll
static void afterAll() {
Expand All @@ -63,6 +63,7 @@ private FirFile getCompiledSource() {

public String constructorSignature() {
return signatureBuilder().methodDeclarationSignature(getCompiledSource().getDeclarations().stream()
.filter(FirRegularClass.class::isInstance)
.map(FirRegularClass.class::cast)
.flatMap(it -> it.getDeclarations().stream())
.filter(FirConstructor.class::isInstance)
Expand All @@ -74,6 +75,7 @@ public String constructorSignature() {

public Object innerClassSignature(String innerClassSimpleName) {
return signatureBuilder().signature(getCompiledSource().getDeclarations().stream()
.filter(FirRegularClass.class::isInstance)
.map(FirRegularClass.class::cast)
.flatMap(it -> it.getDeclarations().stream())
.filter(FirRegularClass.class::isInstance)
Expand All @@ -86,6 +88,7 @@ public Object innerClassSignature(String innerClassSimpleName) {

public String fieldSignature(String field) {
return signatureBuilder().variableSignature(getCompiledSource().getDeclarations().stream()
.filter(FirRegularClass.class::isInstance)
.map(FirRegularClass.class::cast)
.flatMap(it -> it.getDeclarations().stream())
.filter(FirProperty.class::isInstance)
Expand All @@ -99,13 +102,14 @@ public String fieldSignature(String field) {
@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);
.filter(FirRegularClass.class::isInstance)
.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) {
Expand All @@ -126,6 +130,7 @@ public String fieldPropertySetterSignature(String field) {

public Object firstMethodParameterSignature(String methodName) {
return signatureBuilder().signature(getCompiledSource().getDeclarations().stream()
.filter(FirRegularClass.class::isInstance)
.map(FirRegularClass.class::cast)
.flatMap(it -> it.getDeclarations().stream())
.filter(FirSimpleFunction.class::isInstance)
Expand All @@ -140,6 +145,7 @@ public Object firstMethodParameterSignature(String methodName) {

public Object lastClassTypeParameter() {
return signatureBuilder().signature(getCompiledSource().getDeclarations().stream()
.filter(FirRegularClass.class::isInstance)
.map(FirRegularClass.class::cast)
.findFirst()
.orElseThrow()
Expand All @@ -149,6 +155,7 @@ public Object lastClassTypeParameter() {

public String methodSignature(String methodName) {
return signatureBuilder().methodDeclarationSignature(getCompiledSource().getDeclarations().stream()
.filter(FirRegularClass.class::isInstance)
.map(FirRegularClass.class::cast)
.flatMap(it -> it.getDeclarations().stream())
.filter(FirSimpleFunction.class::isInstance)
Expand All @@ -159,6 +166,24 @@ public String methodSignature(String methodName) {
.getSymbol(), null);
}

@Test
void fileField() {
FirProperty firProperty = getCompiledSource().getDeclarations().stream()
.filter(it -> it instanceof FirProperty && "field".equals(((FirProperty) it).getName().asString()))
.map(it -> (FirProperty) it).findFirst().orElseThrow();
assertThat(signatureBuilder().variableSignature(firProperty.getSymbol(), getCompiledSource().getSymbol()))
.isEqualTo("KotlinTypeGoatKt{name=field,type=kotlin.Int}");
}

@Test
void fileFunction() {
FirSimpleFunction function = getCompiledSource().getDeclarations().stream()
.filter(it -> it instanceof FirSimpleFunction && "function".equals(((FirSimpleFunction) it).getName().asString()))
.map(it -> (FirSimpleFunction) it).findFirst().orElseThrow();
assertThat(signatureBuilder().methodDeclarationSignature(function.getSymbol(), getCompiledSource().getSymbol()))
.isEqualTo("KotlinTypeGoatKt{name=function,return=kotlin.Unit,parameters=[]}");
}

@Test
void constructor() {
assertThat(constructorSignature())
Expand Down
4 changes: 3 additions & 1 deletion src/test/resources/KotlinTypeGoat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ package org.openrewrite.kotlin

import java.lang.Object

// TODO: FIX ME. Files needs to declare fields and methods to assert type mapping.
const val field = 10
fun function() {}

@AnnotationWithRuntimeRetention
@AnnotationWithSourceRetention
abstract class KotlinTypeGoat<T, S> where S: PT<S>, S: C {
Expand Down

0 comments on commit 7dcee5a

Please sign in to comment.