Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ private JavaTemplate makeNewMethod(String newClass) {
}
return JavaTemplate
.builder(methodInvocationTemplate)
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().dependsOn(methodStub))
.imports(newClass)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2026 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.kotlin;

import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.java.ChangeStaticFieldToMethod;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.kotlin.Assertions.kotlin;

class ChangeStaticFieldToMethodTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new ChangeStaticFieldToMethod(
"java.util.Collections",
"EMPTY_LIST",
"java.nio.charset.StandardCharsets",
"UTF_8",
"name"
));
}

@Test
void migratesQualifiedField() {
rewriteRun(
kotlin(
"""
import java.util.Collections

class A {
fun test(): Any = Collections.EMPTY_LIST
}
""",
"""
import java.nio.charset.StandardCharsets

class A {
fun test(): Any = StandardCharsets.UTF_8.name()
}
"""
)
);
}

@Test
void migratesStaticImportedField() {
rewriteRun(
kotlin(
"""
import java.util.Collections.EMPTY_LIST

class A {
fun test(): Any = EMPTY_LIST
}
""",
"""
import java.nio.charset.StandardCharsets

class A {
fun test(): Any = StandardCharsets.UTF_8.name()
}
"""
)
);
}

@Issue("https://github.com/moderneinc/customer-requests/issues/2924")
@Test
void migratesArgumentInGenericMethod() {
rewriteRun(
kotlin(
"""
import java.util.Collections

open class Base

class A : Base() {
private val other = A()

private fun <T> doGetResponse(clazz: Class<T>): T? {
val result = java.util.Objects.toString(Collections.EMPTY_LIST)
return null
}

fun `should do the thing`() {
}
}
""",
"""
import java.nio.charset.StandardCharsets

open class Base

class A : Base() {
private val other = A()

private fun <T> doGetResponse(clazz: Class<T>): T? {
val result = java.util.Objects.toString(StandardCharsets.UTF_8.name())
return null
}

fun `should do the thing`() {
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright 2026 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.kotlin;

import org.junit.jupiter.api.Test;
import org.openrewrite.java.ReplaceConstantWithAnotherConstant;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.kotlin.Assertions.kotlin;

class ReplaceConstantWithAnotherConstantTest implements RewriteTest {

@Test
void replaceConstantOnSameOwner() {
rewriteRun(
spec -> spec.recipe(new ReplaceConstantWithAnotherConstant("java.io.File.pathSeparator", "java.io.File.separator")),
kotlin(
"""
import java.io.File

class Test {
fun foo() {
println(File.pathSeparator)
println(java.io.File.pathSeparator)
}
}
""",
"""
import java.io.File

class Test {
fun foo() {
println(File.separator)
println(File.separator)
}
}
"""
)
);
}

@Test
void replaceConstantOnAnotherOwner() {
rewriteRun(
spec -> spec.recipe(new ReplaceConstantWithAnotherConstant("java.io.File.pathSeparator", "java.util.jar.JarFile.MANIFEST_NAME")),
kotlin(
"""
import java.io.File

class Test {
fun foo() {
println(File.pathSeparator)
}
}
""",
"""
import java.util.jar.JarFile

class Test {
fun foo() {
println(JarFile.MANIFEST_NAME)
}
}
"""
)
);
}

@Test
void replaceImportedConstant() {
rewriteRun(
spec -> spec.recipe(new ReplaceConstantWithAnotherConstant("java.io.File.pathSeparator", "java.util.jar.JarFile.MANIFEST_NAME")),
kotlin(
"""
import java.io.File.pathSeparator

class Test {
fun foo() {
println(pathSeparator)
}
}
""",
"""
import java.util.jar.JarFile.MANIFEST_NAME

class Test {
fun foo() {
println(MANIFEST_NAME)
}
}
"""
)
);
}
}
Loading