-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23c25bb
commit 492f648
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/main/java/org/openrewrite/java/migrate/search/AboutJavaVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.java.migrate.search; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Option; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.internal.lang.Nullable; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.marker.JavaVersion; | ||
import org.openrewrite.java.search.UsesType; | ||
import org.openrewrite.java.tree.JavaSourceFile; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class AboutJavaVersion extends Recipe { | ||
@Option(required = false, | ||
description = "Only mark the Java version when this type is in use.", | ||
example = "lombok.val") | ||
@Nullable | ||
String whenUsesType; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "List calculated information about Java version on source files"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return super.getDescription(); | ||
} | ||
|
||
@Override | ||
protected @Nullable TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() { | ||
return whenUsesType == null ? null : new UsesType<>(whenUsesType); | ||
} | ||
|
||
@Override | ||
protected TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public JavaSourceFile visitJavaSourceFile(JavaSourceFile cu, ExecutionContext executionContext) { | ||
return cu.getMarkers().findFirst(JavaVersion.class) | ||
.map(version -> (JavaSourceFile) cu.withMarkers(cu.getMarkers() | ||
.searchResult("Java version: " + version.getMajorVersion()))) | ||
.orElse(cu); | ||
} | ||
}; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/openrewrite/java/migrate/search/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.java.migrate.search; | ||
|
||
import org.openrewrite.internal.lang.NonNullApi; | ||
import org.openrewrite.internal.lang.NonNullFields; |
47 changes: 47 additions & 0 deletions
47
src/test/java/org/openrewrite/java/migrate/search/AboutJavaVersionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.java.migrate.search; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.java.marker.JavaVersion; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
public class AboutJavaVersionTest implements RewriteTest { | ||
|
||
@Test | ||
void aboutJavaVersion() { | ||
rewriteRun( | ||
spec -> spec.recipe(new AboutJavaVersion(null)), | ||
java( | ||
//language=java | ||
""" | ||
class Test { | ||
} | ||
""", | ||
//language=java | ||
""" | ||
/*~~(Java version: 11)~~>*/class Test { | ||
} | ||
""", | ||
spec -> spec.markers(new JavaVersion(UUID.randomUUID(), "me", "me", "11.0.15+10", "11.0.15+10")) | ||
) | ||
); | ||
} | ||
} |