|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0 |
| 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.openrewrite.ExecutionContext; |
| 21 | +import org.openrewrite.Option; |
| 22 | +import org.openrewrite.Recipe; |
| 23 | +import org.openrewrite.TreeVisitor; |
| 24 | +import org.openrewrite.internal.lang.Nullable; |
| 25 | +import org.openrewrite.java.JavaIsoVisitor; |
| 26 | +import org.openrewrite.java.marker.JavaVersion; |
| 27 | +import org.openrewrite.java.search.UsesType; |
| 28 | +import org.openrewrite.java.tree.JavaSourceFile; |
| 29 | + |
| 30 | +@Value |
| 31 | +@EqualsAndHashCode(callSuper = false) |
| 32 | +public class AboutJavaVersion extends Recipe { |
| 33 | + @Option(required = false, |
| 34 | + description = "Only mark the Java version when this type is in use.", |
| 35 | + example = "lombok.val") |
| 36 | + @Nullable |
| 37 | + String whenUsesType; |
| 38 | + |
| 39 | + @Override |
| 40 | + public String getDisplayName() { |
| 41 | + return "List calculated information about Java version on source files"; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public String getDescription() { |
| 46 | + return super.getDescription(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected @Nullable TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() { |
| 51 | + return whenUsesType == null ? null : new UsesType<>(whenUsesType); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected TreeVisitor<?, ExecutionContext> getVisitor() { |
| 56 | + return new JavaIsoVisitor<ExecutionContext>() { |
| 57 | + @Override |
| 58 | + public JavaSourceFile visitJavaSourceFile(JavaSourceFile cu, ExecutionContext executionContext) { |
| 59 | + return cu.getMarkers().findFirst(JavaVersion.class) |
| 60 | + .map(version -> (JavaSourceFile) cu.withMarkers(cu.getMarkers() |
| 61 | + .searchResult("Java version: " + version.getMajorVersion()))) |
| 62 | + .orElse(cu); |
| 63 | + } |
| 64 | + }; |
| 65 | + } |
| 66 | +} |
0 commit comments