-
Notifications
You must be signed in to change notification settings - Fork 101
Migrate to Process#waitFor(Duration)
as part of Java 25 upgrade
#840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
c75e03e
Added initial recipe
JohannisK 7163546
Fix
JohannisK e663582
polish
JohannisK c25c0c1
polish
JohannisK 92d52fa
Update src/main/java/org/openrewrite/java/migrate/lang/MigrateProcess…
JohannisK c54b45b
Update src/test/java/org/openrewrite/java/migrate/lang/MigrateProcess…
JohannisK ac1f7ed
Update src/test/java/org/openrewrite/java/migrate/lang/MigrateProcess…
JohannisK 9cebb30
Update src/test/java/org/openrewrite/java/migrate/lang/MigrateProcess…
JohannisK 07859b1
polish
JohannisK 4cea965
polish
JohannisK 9d3cafa
Add java 25
JohannisK a3b3ccc
Update src/main/java/org/openrewrite/java/migrate/lang/MigrateProcess…
JohannisK 8d58545
Update src/main/java/org/openrewrite/java/migrate/lang/MigrateProcess…
JohannisK 46fe2c9
Update src/main/java/org/openrewrite/java/migrate/lang/MigrateProcess…
JohannisK 3e4c545
Update src/main/java/org/openrewrite/java/migrate/lang/MigrateProcess…
JohannisK 2a4a308
Add `rewrite-java-25` runtime dependency
timtebeek d3ddefe
Override toolchain to use Java 25 for tests that need it
timtebeek 710ced9
Merge branch 'main' into java25_process_wait_for_duration
timtebeek 9e1c682
Also override `java_version` for receive-pr.yml
timtebeek eeccb46
Do not add recipe twice, as it will run twice
timtebeek 9d576b9
Apply formatter to tests
timtebeek 35b3d89
Remove imports before adding new ones, return from template.apply
timtebeek 6043b30
Use markers to set Java versions in tests instead
timtebeek 49f7a15
Add test that no changes are made when Duration already present
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
122 changes: 122 additions & 0 deletions
122
src/main/java/org/openrewrite/java/migrate/lang/MigrateProcessWaitForDuration.java
This file contains hidden or 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,122 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license | ||
* <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.lang; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.search.UsesJavaVersion; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.staticanalysis.SimplifyDurationCreationUnits; | ||
|
||
public class MigrateProcessWaitForDuration extends Recipe { | ||
|
||
private static final MethodMatcher PROCESS_WAIT_FOR_MATCHER = new MethodMatcher("java.lang.Process waitFor(long, java.util.concurrent.TimeUnit)"); | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Use `Process#waitFor(Duration)`"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Use `Process#waitFor(Duration)` instead of `Process#waitFor(long, TimeUnit)` in Java 25 or higher."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check(new UsesJavaVersion<>(25), new JavaVisitor<ExecutionContext>() { | ||
@Override | ||
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); | ||
|
||
if (PROCESS_WAIT_FOR_MATCHER.matches(mi)) { | ||
Expression valueArg = mi.getArguments().get(0); | ||
Expression unitArg = mi.getArguments().get(1); | ||
String timeUnitName = getTimeUnitName(unitArg); | ||
String durationMethod = getDurationMethod(timeUnitName); | ||
|
||
boolean isSimpleValue = valueArg instanceof J.Literal || valueArg instanceof J.Identifier; | ||
|
||
maybeRemoveImport("java.util.concurrent.TimeUnit"); | ||
maybeRemoveImport("java.util.concurrent.TimeUnit." + timeUnitName); | ||
maybeAddImport("java.time.Duration"); | ||
maybeAddImport("java.time.temporal.ChronoUnit"); | ||
|
||
doAfterVisit(new SimplifyDurationCreationUnits().getVisitor()); | ||
|
||
if (isSimpleValue && "MICROSECONDS".equals(timeUnitName)) { | ||
return JavaTemplate.builder("Duration.of(#{any(long)}, ChronoUnit.MICROS)") | ||
.imports("java.time.Duration", "java.time.temporal.ChronoUnit") | ||
.build() | ||
.apply(getCursor(), mi.getCoordinates().replaceArguments(), valueArg); | ||
} | ||
if (isSimpleValue && durationMethod != null) { | ||
return JavaTemplate.builder("Duration." + durationMethod + "(#{any(long)})") | ||
.imports("java.time.Duration") | ||
.build() | ||
.apply(getCursor(), mi.getCoordinates().replaceArguments(), valueArg); | ||
} | ||
return JavaTemplate.builder("Duration.of(#{any(long)}, #{any(java.util.concurrent.TimeUnit)}.toChronoUnit())") | ||
.imports("java.time.Duration") | ||
.build() | ||
.apply(getCursor(), mi.getCoordinates().replaceArguments(), valueArg, unitArg); | ||
} | ||
return mi; | ||
} | ||
|
||
private @Nullable String getTimeUnitName(Expression timeUnitArg) { | ||
if (timeUnitArg instanceof J.FieldAccess) { | ||
J.FieldAccess fa = (J.FieldAccess) timeUnitArg; | ||
return fa.getSimpleName(); | ||
} | ||
if (timeUnitArg instanceof J.Identifier) { | ||
J.Identifier id = (J.Identifier) timeUnitArg; | ||
return id.getSimpleName(); | ||
} | ||
return null; | ||
} | ||
|
||
private @Nullable String getDurationMethod(@Nullable String timeUnitName) { | ||
if (timeUnitName == null) { | ||
return null; | ||
} | ||
switch (timeUnitName) { | ||
case "NANOSECONDS": | ||
return "ofNanos"; | ||
case "MILLISECONDS": | ||
return "ofMillis"; | ||
case "SECONDS": | ||
return "ofSeconds"; | ||
case "MINUTES": | ||
return "ofMinutes"; | ||
case "HOURS": | ||
return "ofHours"; | ||
case "DAYS": | ||
return "ofDays"; | ||
default: | ||
return null; | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.