Skip to content

Commit

Permalink
Updated KotlinTypeMapping to support private_to_this visibility. Po…
Browse files Browse the repository at this point in the history
…lish, fixed warnings. (#378)
  • Loading branch information
traceyyoshima authored Nov 10, 2023
1 parent c133e4d commit ce614e3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 44 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/org/openrewrite/kotlin/KotlinTypeMapping.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1188,11 +1188,11 @@ class KotlinTypeMapping(

private fun mapToFlagsBitmap(visibility: Visibility, modality: Modality?): Long {
var bitMask: Long = 0
when (visibility.externalDisplayName.lowercase()) {
when (visibility.name.lowercase()) {
"public" -> bitMask += 1L
"private" -> bitMask += 1L shl 1
"protected" -> bitMask += 1L shl 2
"internal", "package-private", "local" -> {}
"private", "private_to_this" -> bitMask += 1L shl 1
"protected", "protected_and_package" -> bitMask += 1L shl 2
"internal", "package", "local" -> {}
else -> throw UnsupportedOperationException("Unsupported visibility: ${visibility.name.lowercase()}")
}
if (modality != null) {
Expand Down
93 changes: 53 additions & 40 deletions src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,6 @@ public JavaType.Method methodType(String methodName) {
return type;
}

public J.VariableDeclarations getField(String fieldName) {
return goatClassDeclaration.getClassDeclaration().getBody().getStatements().stream()
.filter(it -> it instanceof org.openrewrite.java.tree.J.VariableDeclarations || it instanceof K.Property)
.map(it -> it instanceof K.Property ? ((K.Property) it).getVariableDeclarations() : (J.VariableDeclarations) it)
.map(J.VariableDeclarations.class::cast)
.filter(mv -> mv.getVariables().stream().anyMatch(v -> v.getSimpleName().equals(fieldName)))
.findFirst()
.orElse(null);
}

public K.Property getProperty(String fieldName) {
return goatClassDeclaration.getClassDeclaration().getBody().getStatements().stream()
.filter(it -> it instanceof K.Property)
Expand Down Expand Up @@ -363,6 +353,7 @@ void javaLangObject() {
assertThat(returnType.getType().getTypeParameters().get(0).toString()).isEqualTo("Generic{T}");
}

@SuppressWarnings({"KotlinConstantConditions", "RedundantExplicitType"})
@Nested
class ParsingTest implements RewriteTest {

Expand Down Expand Up @@ -458,7 +449,7 @@ void implicitInvoke() {
""", spec -> spec.afterRecipe(cu -> {
AtomicBoolean found = new AtomicBoolean(false);
new KotlinIsoVisitor<AtomicBoolean>() {
MethodMatcher matcher = new MethodMatcher("kotlin.Function1 invoke(..)");
final MethodMatcher matcher = new MethodMatcher("kotlin.Function1 invoke(..)");
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean atomicBoolean) {
if (matcher.matches(method)) {
Expand Down Expand Up @@ -544,16 +535,15 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Atomi
);
}

@SuppressWarnings({"KotlinConstantConditions", "UnusedUnaryOperator", "RedundantExplicitType"})
@Test
void whenExpression() {
//noinspection RemoveRedundantQualifierName
rewriteRun(
kotlin(
"""
@Suppress("UNUSED_VARIABLE")
fun method() {
val condition: Int = 11
when {
val a = when {
condition < 20 -> 'c'
condition < 10 -> -1
condition > 10 -> (true)
Expand Down Expand Up @@ -634,6 +624,7 @@ void operatorOverload() {
rewriteRun(
kotlin(
"""
@Suppress("UNUSED_PARAMETER")
class Foo {
operator fun contains(element: Int): Boolean {
return true
Expand Down Expand Up @@ -675,6 +666,7 @@ void destructs() {
rewriteRun(
kotlin(
"""
@Suppress("UNUSED_VARIABLE")
fun foo() {
val ( a , b , c ) = Triple ( 1 , 2 , 3 )
}
Expand Down Expand Up @@ -732,44 +724,64 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
);
}

@SuppressWarnings("CanBePrimaryConstructorProperty")
@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/374")
@Test
void privateToThisModifier() {
rewriteRun(
kotlin(
"""
@file:Suppress("UNUSED_VARIABLE")
class A<in T>(t: T) {
private val t: T = t // visibility for t is PRIVATE_TO_THIS
fun test() {
val x: T = t // correct
val y: T = this.t // also correct
}
}
"""
)
);
}

@Test
void variableTypes() {
rewriteRun(
kotlin(
"""
@file:Suppress("UNUSED_VARIABLE")
val foo1: Int = 42
class Foo(val foo2: Int) {
val foo3: Int = 42
fun m(foo4: Int) {
val use: Int = foo4
}
}
""", spec -> spec.afterRecipe(cu -> {
new KotlinIsoVisitor<Integer>() {
@Override
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, Integer integer) {
switch (variable.getSimpleName()) {
case "foo1": {
assertThat(variable.getVariableType().toString()).isEqualTo("openRewriteFile0Kt{name=foo1,type=kotlin.Int}");
break;
}
case "foo2": {
assertThat(variable.getVariableType().toString()).isEqualTo("Foo{name=foo2,type=kotlin.Int}");
break;
}
case "foo3": {
assertThat(variable.getVariableType().toString()).isEqualTo("Foo{name=foo3,type=kotlin.Int}");
break;
}
case "foo4": {
assertThat(variable.getVariableType().toString()).isEqualTo("Foo{name=m,return=kotlin.Unit,parameters=[kotlin.Int]}{name=foo4,type=kotlin.Int}");
break;
}
}
return super.visitVariable(variable, integer);
}
}.visit(cu, 0);
})
""", spec -> spec.afterRecipe(cu -> new KotlinIsoVisitor<Integer>() {
@Override
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, Integer integer) {
switch (variable.getSimpleName()) {
case "foo1": {
assertThat(variable.getVariableType().toString()).isEqualTo("openRewriteFile0Kt{name=foo1,type=kotlin.Int}");
break;
}
case "foo2": {
assertThat(variable.getVariableType().toString()).isEqualTo("Foo{name=foo2,type=kotlin.Int}");
break;
}
case "foo3": {
assertThat(variable.getVariableType().toString()).isEqualTo("Foo{name=foo3,type=kotlin.Int}");
break;
}
case "foo4": {
assertThat(variable.getVariableType().toString()).isEqualTo("Foo{name=m,return=kotlin.Unit,parameters=[kotlin.Int]}{name=foo4,type=kotlin.Int}");
break;
}
}
return super.visitVariable(variable, integer);
}
}.visit(cu, 0))
)
);
}
Expand All @@ -779,6 +791,7 @@ void nullJavaClassifierType() {
rewriteRun(
spec -> spec.parser(KotlinParser.builder().classpath("javapoet","compile-testing")),
kotlin(
//language=none turn off language inspection, since the test does not have access to the class path.
"""
package org.openrewrite.kotlin
Expand Down

0 comments on commit ce614e3

Please sign in to comment.