Skip to content

Commit 492f648

Browse files
committed
AboutJavaVersion
1 parent 23c25bb commit 492f648

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
@NonNullApi
17+
@NonNullFields
18+
package org.openrewrite.java.migrate.search;
19+
20+
import org.openrewrite.internal.lang.NonNullApi;
21+
import org.openrewrite.internal.lang.NonNullFields;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 org.junit.jupiter.api.Test;
19+
import org.openrewrite.java.marker.JavaVersion;
20+
import org.openrewrite.test.RewriteTest;
21+
22+
import java.util.UUID;
23+
24+
import static org.openrewrite.java.Assertions.java;
25+
26+
public class AboutJavaVersionTest implements RewriteTest {
27+
28+
@Test
29+
void aboutJavaVersion() {
30+
rewriteRun(
31+
spec -> spec.recipe(new AboutJavaVersion(null)),
32+
java(
33+
//language=java
34+
"""
35+
class Test {
36+
}
37+
""",
38+
//language=java
39+
"""
40+
/*~~(Java version: 11)~~>*/class Test {
41+
}
42+
""",
43+
spec -> spec.markers(new JavaVersion(UUID.randomUUID(), "me", "me", "11.0.15+10", "11.0.15+10"))
44+
)
45+
);
46+
}
47+
}

0 commit comments

Comments
 (0)