diff --git a/src/main/java/org/openrewrite/kotlin/KotlinParser.java b/src/main/java/org/openrewrite/kotlin/KotlinParser.java index 4e12f5142..0bae3fb48 100644 --- a/src/main/java/org/openrewrite/kotlin/KotlinParser.java +++ b/src/main/java/org/openrewrite/kotlin/KotlinParser.java @@ -286,6 +286,18 @@ public Builder classpathFromResources(ExecutionContext ctx, String... classpath) return this; } + /** + * This is an internal API which is subject to removal or change. + */ + public Builder addClasspathEntry(Path classpath) { + if (this.classpath.isEmpty()) { + this.classpath = Collections.singletonList(classpath); + } else { + this.classpath.add(classpath); + } + return this; + } + public Builder typeCache(JavaTypeCache typeCache) { this.typeCache = typeCache; return this; diff --git a/src/main/java/org/openrewrite/kotlin/KotlinTemplate.java b/src/main/java/org/openrewrite/kotlin/KotlinTemplate.java new file mode 100644 index 000000000..6972cc96c --- /dev/null +++ b/src/main/java/org/openrewrite/kotlin/KotlinTemplate.java @@ -0,0 +1,123 @@ +/* + * Copyright 2023 the original author or authors. + *
+ * 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 + *
+ * https://www.apache.org/licenses/LICENSE-2.0 + *
+ * 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.openrewrite.Cursor;
+import org.openrewrite.internal.StringUtils;
+import org.openrewrite.java.JavaTemplate;
+import org.openrewrite.java.internal.template.Substitutions;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.JavaCoordinates;
+import org.openrewrite.kotlin.internal.template.KotlinSubstitutions;
+import org.openrewrite.kotlin.internal.template.KotlinTemplateParser;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.function.Consumer;
+
+public class KotlinTemplate extends JavaTemplate {
+ private KotlinTemplate(boolean contextSensitive, KotlinParser.Builder parser, String code, Set
+ * 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
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.internal.template;
+
+import org.openrewrite.Cursor;
+import org.openrewrite.java.internal.template.BlockStatementTemplateGenerator;
+import org.openrewrite.java.tree.Expression;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.JavaCoordinates;
+
+import java.util.Set;
+
+public class KotlinBlockStatementTemplateGenerator extends BlockStatementTemplateGenerator {
+ public KotlinBlockStatementTemplateGenerator(Set
+ * 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
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.internal.template;
+
+import org.openrewrite.java.internal.template.Substitutions;
+import org.openrewrite.java.tree.JavaType;
+
+public class KotlinSubstitutions extends Substitutions {
+ public KotlinSubstitutions(String code, Object[] parameters) {
+ super(code, parameters);
+ }
+
+ @Override
+ protected String newObjectParameter(String fqn, int index) {
+ return "__P__./*__p" + index + "__*/p<" + fqn + ">()";
+ }
+
+ @Override
+ protected String newPrimitiveParameter(String fqn, int index) {
+ return newObjectParameter(fqn, index);
+ }
+
+ @Override
+ protected String newArrayParameter(JavaType elemType, int dimensions, int index) {
+ // generate literal of the form: `arrayOf(arrayOf
+ * 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
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.internal.template;
+
+import org.openrewrite.java.internal.template.AnnotationTemplateGenerator;
+import org.openrewrite.java.internal.template.JavaTemplateParser;
+import org.openrewrite.kotlin.KotlinParser;
+
+import java.util.Set;
+import java.util.function.Consumer;
+
+public class KotlinTemplateParser extends JavaTemplateParser {
+ public KotlinTemplateParser(boolean contextSensitive, KotlinParser.Builder parser, Consumer
+ * 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
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+@NonNullApi
+@NonNullFields
+package org.openrewrite.kotlin.internal.template;
+
+import org.openrewrite.internal.lang.NonNullApi;
+import org.openrewrite.internal.lang.NonNullFields;
diff --git a/src/main/java/org/openrewrite/kotlin/marker/package-info.java b/src/main/java/org/openrewrite/kotlin/marker/package-info.java
new file mode 100644
index 000000000..4dd782244
--- /dev/null
+++ b/src/main/java/org/openrewrite/kotlin/marker/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2023 the original author or authors.
+ *
+ * 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
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ */
+@NonNullApi
+@NonNullFields
+package org.openrewrite.kotlin.marker;
+
+import org.openrewrite.internal.lang.NonNullApi;
+import org.openrewrite.internal.lang.NonNullFields;
diff --git a/src/test/java/org/openrewrite/kotlin/KotlinTemplateMatchTest.java b/src/test/java/org/openrewrite/kotlin/KotlinTemplateMatchTest.java
new file mode 100644
index 000000000..71f02cba4
--- /dev/null
+++ b/src/test/java/org/openrewrite/kotlin/KotlinTemplateMatchTest.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2023 the original author or authors.
+ *
+ * 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
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.ExecutionContext;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.marker.SearchResult;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.kotlin.Assertions.kotlin;
+import static org.openrewrite.test.RewriteTest.toRecipe;
+
+public class KotlinTemplateMatchTest implements RewriteTest {
+
+ @Test
+ void matchBinary() {
+ rewriteRun(
+ spec -> spec.recipe(toRecipe(() -> new KotlinVisitor<>() {
+ @Override
+ public J visitBinary(J.Binary binary, ExecutionContext ctx) {
+ return KotlinTemplate.matches("1 == #{any(int)}", getCursor()) ?
+ SearchResult.found(binary) : super.visitBinary(binary, ctx);
+ }
+ })),
+ kotlin(
+ """
+ class Test {
+ val b1 = 1 == 2
+ val b2 = 1 == 3
+
+ val b3 = 2 == 1
+ val b4 = 2 == 2 + 3 /* no match because type is `kotlin.Int` */
+ }
+ """,
+ """
+ class Test {
+ val b1 = /*~~>*/1 == 2
+ val b2 = /*~~>*/1 == 3
+
+ val b3 = 2 == 1
+ val b4 = 2 == 2 + 3 /* no match because type is `kotlin.Int` */
+ }
+ """
+ ));
+ }
+}