|
| 1 | +/* |
| 2 | + * Copyright 2026 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Moderne Source Available License (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://docs.moderne.io/licensing/moderne-source-available-license |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.migrate.search; |
| 17 | + |
| 18 | +import lombok.EqualsAndHashCode; |
| 19 | +import lombok.Value; |
| 20 | +import org.jspecify.annotations.Nullable; |
| 21 | +import org.openrewrite.ExecutionContext; |
| 22 | +import org.openrewrite.Option; |
| 23 | +import org.openrewrite.ScanningRecipe; |
| 24 | +import org.openrewrite.SourceFile; |
| 25 | +import org.openrewrite.Tree; |
| 26 | +import org.openrewrite.TreeVisitor; |
| 27 | +import org.openrewrite.java.marker.JavaProject; |
| 28 | +import org.openrewrite.marker.SearchResult; |
| 29 | + |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.Optional; |
| 32 | +import java.util.Set; |
| 33 | + |
| 34 | +@EqualsAndHashCode(callSuper = false) |
| 35 | +@Value |
| 36 | +public class ModuleHasKotlinSource extends ScanningRecipe<Set<JavaProject>> { |
| 37 | + |
| 38 | + @Option(displayName = "Invert marking", |
| 39 | + description = "If `true`, marks files in modules that do *not* contain Kotlin sources. Defaults to `false`.", |
| 40 | + required = false) |
| 41 | + @Nullable |
| 42 | + Boolean invertMarking; |
| 43 | + |
| 44 | + @Override |
| 45 | + public String getDisplayName() { |
| 46 | + return "Module has Kotlin source files"; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String getDescription() { |
| 51 | + return "Marks all files in modules that contain at least one Kotlin source file (`.kt`). " + |
| 52 | + "Intended as a precondition to scope recipes to projects that actually compile Kotlin, " + |
| 53 | + "as opposed to projects that merely pick up `kotlin-stdlib` transitively."; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Set<JavaProject> getInitialValue(ExecutionContext ctx) { |
| 58 | + return new HashSet<>(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public TreeVisitor<?, ExecutionContext> getScanner(Set<JavaProject> acc) { |
| 63 | + return new TreeVisitor<Tree, ExecutionContext>() { |
| 64 | + @Override |
| 65 | + public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { |
| 66 | + if (tree instanceof SourceFile) { |
| 67 | + SourceFile sourceFile = (SourceFile) tree; |
| 68 | + if (sourceFile.getSourcePath().toString().endsWith(".kt")) { |
| 69 | + sourceFile.getMarkers().findFirst(JavaProject.class).ifPresent(acc::add); |
| 70 | + } |
| 71 | + } |
| 72 | + return tree; |
| 73 | + } |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public TreeVisitor<?, ExecutionContext> getVisitor(Set<JavaProject> acc) { |
| 79 | + return new TreeVisitor<Tree, ExecutionContext>() { |
| 80 | + @Override |
| 81 | + public Tree visit(@Nullable Tree tree, ExecutionContext ctx) { |
| 82 | + assert tree != null; |
| 83 | + boolean shouldInvert = Boolean.TRUE.equals(invertMarking); |
| 84 | + Optional<JavaProject> maybeJp = tree.getMarkers().findFirst(JavaProject.class); |
| 85 | + if (!maybeJp.isPresent()) { |
| 86 | + if (shouldInvert) { |
| 87 | + return SearchResult.found(tree, "No module, so vacuously has no Kotlin source"); |
| 88 | + } |
| 89 | + return tree; |
| 90 | + } |
| 91 | + JavaProject jp = maybeJp.get(); |
| 92 | + if (shouldInvert && !acc.contains(jp)) { |
| 93 | + return SearchResult.found(tree, "Module does not have Kotlin source"); |
| 94 | + } |
| 95 | + if (!shouldInvert && acc.contains(jp)) { |
| 96 | + return SearchResult.found(tree, "Module has Kotlin source"); |
| 97 | + } |
| 98 | + return tree; |
| 99 | + } |
| 100 | + }; |
| 101 | + } |
| 102 | +} |
0 commit comments