Skip to content

Commit

Permalink
Add isStatic to mapToFlagsBitmap in KotlinTypeMapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
traceyyoshima committed Dec 4, 2023
1 parent 323ff90 commit e4d16f2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/main/kotlin/org/openrewrite/kotlin/KotlinTypeMapping.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.fir.analysis.checkers.toRegularClassSymbol
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.impl.FirOuterClassTypeParameterRef
import org.jetbrains.kotlin.fir.declarations.utils.isLocal
import org.jetbrains.kotlin.fir.declarations.utils.isStatic
import org.jetbrains.kotlin.fir.declarations.utils.modality
import org.jetbrains.kotlin.fir.declarations.utils.visibility
import org.jetbrains.kotlin.fir.expressions.*
Expand Down Expand Up @@ -334,7 +335,7 @@ class KotlinTypeMapping(
if (clazz == null) {
clazz = Class(
null,
mapToFlagsBitmap(firClass.visibility, firClass.modality()),
mapToFlagsBitmap(firClass.visibility, firClass.modality(), firClass.isStatic),
fqn,
mapKind(firClass.classKind),
null, null, null, null, null, null, null
Expand Down Expand Up @@ -496,7 +497,7 @@ class KotlinTypeMapping(
}
val method = Method(
null,
mapToFlagsBitmap(function.visibility, function.modality),
mapToFlagsBitmap(function.visibility, function.modality, function.isStatic),
null,
if (function.symbol is FirConstructorSymbol) "<constructor>" else methodName(function),
null,
Expand Down Expand Up @@ -667,8 +668,8 @@ class KotlinTypeMapping(
val method = Method(
null,
when (sym) {
is FirConstructorSymbol -> mapToFlagsBitmap(sym.visibility, sym.modality)
is FirNamedFunctionSymbol -> mapToFlagsBitmap(sym.visibility, sym.modality)
is FirConstructorSymbol -> mapToFlagsBitmap(sym.visibility, sym.modality, sym.isStatic)
is FirNamedFunctionSymbol -> mapToFlagsBitmap(sym.visibility, sym.modality, sym.isStatic)
else -> {
throw UnsupportedOperationException("Unsupported method symbol: ${sym.javaClass.name}")
}
Expand Down Expand Up @@ -828,7 +829,7 @@ class KotlinTypeMapping(
fun variableType(variable: FirVariable, parent: Any?, signature: String): Variable {
val vt = Variable(
null,
mapToFlagsBitmap(variable.visibility, variable.modality),
mapToFlagsBitmap(variable.visibility, variable.modality, variable.isStatic),
variableName(variable.name.asString()),
null, null, null
)
Expand Down Expand Up @@ -1229,7 +1230,7 @@ class KotlinTypeMapping(
}
}

private fun mapToFlagsBitmap(visibility: Visibility, modality: Modality?): Long {
private fun mapToFlagsBitmap(visibility: Visibility, modality: Modality?, isStatic: Boolean): Long {
var bitMask: Long = 0
when (visibility.name.lowercase()) {
"public" -> bitMask += 1L
Expand All @@ -1251,6 +1252,9 @@ class KotlinTypeMapping(
else -> throw UnsupportedOperationException("Unsupported modality: ${modality.name.lowercase()}")
}
}
if (isStatic) {
bitMask += 1L shl 3
}
return bitMask
}

Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1080,5 +1080,32 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, In
)
);
}

@SuppressWarnings("HasPlatformType")
@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/483")
@Test
void isStaticFlag() {
//noinspection RemoveRedundantBackticks,RemoveRedundantQualifierName
rewriteRun(
kotlin(
"val i = Integer.valueOf(1)",
spec -> spec.afterRecipe(cu -> {
AtomicBoolean found = new AtomicBoolean(false);
MethodMatcher matcher = new MethodMatcher("java.lang.Integer valueOf(..)");
new KotlinIsoVisitor<Integer>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integer integer) {
if (matcher.matches(method)) {
assertThat(Flag.hasFlags(method.getMethodType().getFlagsBitMap(), Flag.Static)).isTrue();
found.set(true);
}
return super.visitMethodInvocation(method, integer);
}
}.visit(cu, 0);
assertThat(found.get()).isTrue();
})
)
);
}
}
}

0 comments on commit e4d16f2

Please sign in to comment.