Skip to content

Commit 7abd4c2

Browse files
committed
Use K2Compiler via JvmFirPipeline to support Kotlin v2
1 parent bdac8e0 commit 7abd4c2

21 files changed

Lines changed: 475 additions & 347 deletions

rewrite-core/src/main/java/org/openrewrite/Parser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ public Input(Path path, Supplier<InputStream> source) {
148148
this(path, FileAttributes.fromPath(path), source, false);
149149
}
150150

151+
public Input(Path path, Supplier<InputStream> source, boolean synthetic) {
152+
this(path, FileAttributes.fromPath(path), source, synthetic);
153+
}
154+
151155
public Input(Path path, @Nullable FileAttributes fileAttributes, Supplier<InputStream> source) {
152156
this(path, fileAttributes, source, false);
153157
}

rewrite-gradle/src/main/java/org/openrewrite/gradle/DependencyConstraintToRule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
408408
("\n" +
409409
"configurations.all {\n" +
410410
" resolutionStrategy.eachDependency { details ->}\n" +
411-
"}").getBytes(StandardCharsets.UTF_8)))
411+
"}").getBytes(StandardCharsets.UTF_8)),
412+
true)
412413
), null, ctx)
413414
.map(K.CompilationUnit.class::cast)
414415
.map(k -> (J.Block) k.getStatements().get(0))

rewrite-gradle/src/main/java/org/openrewrite/gradle/UpgradeTransitiveDependencyVersion.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ private static Optional<JavaSourceFile> parseAsGradle(String snippet, boolean is
136136
.computeIfAbsent(snippet, s -> GradleParser.builder().build().parseInputs(singleton(
137137
new Parser.Input(
138138
Paths.get("build.gradle" + (isKotlinDsl ? ".kts" : "")),
139-
() -> new ByteArrayInputStream(snippet.getBytes(StandardCharsets.UTF_8))
139+
() -> new ByteArrayInputStream(snippet.getBytes(StandardCharsets.UTF_8)),
140+
true
140141
)), null, ctx)
141142
.findFirst()
142143
.map(maybeCu -> {

rewrite-gradle/src/main/java/org/openrewrite/gradle/internal/AddDependencyVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ private J.MethodInvocation dependenciesDeclaration(ExecutionContext ctx) {
340340
J.MethodInvocation dependencies;
341341
ctx.putMessage(ExecutionContext.REQUIRE_PRINT_EQUALS_INPUT, false);
342342
if (isKotlinDsl) {
343-
dependencies = (J.MethodInvocation) ((J.Block) GRADLE_PARSER.parseInputs(singletonList(new GradleParser.Input(Paths.get("build.gradle.kts"), () -> new ByteArrayInputStream(template.getBytes(StandardCharsets.UTF_8)))), null, ctx)
343+
dependencies = (J.MethodInvocation) ((J.Block) GRADLE_PARSER.parseInputs(singletonList(new GradleParser.Input(Paths.get("build.gradle.kts"), () -> new ByteArrayInputStream(template.getBytes(StandardCharsets.UTF_8)), true)), null, ctx)
344344
.findFirst()
345345
.map(K.CompilationUnit.class::cast)
346346
.orElseThrow(() -> new IllegalArgumentException("Could not parse as Gradle"))
@@ -367,7 +367,7 @@ private J.MethodInvocation dependencyDeclaration(J.Block body, ExecutionContext
367367
J.MethodInvocation dependency;
368368
ctx.putMessage(ExecutionContext.REQUIRE_PRINT_EQUALS_INPUT, false);
369369
if (isKotlinDsl) {
370-
dependency = (J.MethodInvocation) ((J.Block) GRADLE_PARSER.parseInputs(singletonList(new GradleParser.Input(Paths.get("build.gradle.kts"), () -> new ByteArrayInputStream(template.getBytes(StandardCharsets.UTF_8)))), null, ctx)
370+
dependency = (J.MethodInvocation) ((J.Block) GRADLE_PARSER.parseInputs(singletonList(new GradleParser.Input(Paths.get("build.gradle.kts"), () -> new ByteArrayInputStream(template.getBytes(StandardCharsets.UTF_8)), true)), null, ctx)
371371
.findFirst()
372372
.map(K.CompilationUnit.class::cast)
373373
.orElseThrow(() -> new IllegalArgumentException("Could not parse as Gradle"))

rewrite-kotlin/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
kotlin("jvm") version "2.2.21"
88
}
99

10-
val kotlinVersion = "1.9.25"
10+
val kotlinVersion = "2.2.21"
1111

1212
dependencies {
1313
compileOnly(project(":rewrite-core"))
@@ -37,8 +37,8 @@ java {
3737

3838
tasks.withType<KotlinCompile>().configureEach {
3939
compilerOptions {
40-
apiVersion = KotlinVersion.KOTLIN_1_9
41-
languageVersion = KotlinVersion.KOTLIN_1_9
40+
apiVersion = KotlinVersion.KOTLIN_2_2
41+
languageVersion = KotlinVersion.KOTLIN_2_2
4242
jvmTarget.set(if (name.contains("Test")) JvmTarget.JVM_21 else JvmTarget.JVM_1_8)
4343
}
4444
}

0 commit comments

Comments
 (0)