Skip to content

Commit d720f80

Browse files
authored
Replace Paths.get with Path.of (#519)
The `java.nio.file.Paths.get` method was introduced in Java SE 7. The `java.nio.file.Path.of` method was introduced in Java SE 11. This recipe replaces all usages of `Paths.get` with `Path.of` for consistency.
1 parent 8376fac commit d720f80

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/main/resources/META-INF/rewrite/java-version-11.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ recipeList:
5050
- org.openrewrite.java.migrate.logging.JavaLoggingAPIs
5151
- org.openrewrite.java.migrate.lombok.UpdateLombokToJava11
5252
- org.openrewrite.java.migrate.net.JavaNetAPIs
53+
- org.openrewrite.java.migrate.nio.file.PathsGetToPathOf
5354
- org.openrewrite.java.migrate.sql.JavaSqlAPIs
5455
- org.openrewrite.java.migrate.javax.JavaxLangModelUtil
5556
- org.openrewrite.java.migrate.javax.JavaxManagementMonitorAPIs
@@ -265,3 +266,19 @@ recipeList:
265266
methodPattern: java.lang.Thread destroy()
266267
- org.openrewrite.java.migrate.RemoveMethodInvocation:
267268
methodPattern: java.lang.Thread stop(java.lang.Throwable)
269+
---
270+
type: specs.openrewrite.org/v1beta/recipe
271+
name: org.openrewrite.java.migrate.nio.file.PathsGetToPathOf
272+
displayName: Replace `Paths.get` with `Path.of`
273+
description: >-
274+
The `java.nio.file.Paths.get` method was introduced in Java SE 7. The `java.nio.file.Path.of` method was introduced in
275+
Java SE 11. This recipe replaces all usages of `Paths.get` with `Path.of` for consistency.
276+
tags:
277+
- java11
278+
recipeList:
279+
- org.openrewrite.java.ChangeMethodTargetToStatic:
280+
methodPattern: java.nio.file.Paths get(..)
281+
fullyQualifiedTargetTypeName: java.nio.file.Path
282+
- org.openrewrite.java.ChangeMethodName:
283+
methodPattern: java.nio.file.Path get(..)
284+
newMethodName: of
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2024 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.nio.file;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.openrewrite.java.Assertions.java;
24+
25+
class PathsGetToPathOfTest implements RewriteTest {
26+
@Override
27+
public void defaults(RecipeSpec spec) {
28+
spec.recipeFromResources("org.openrewrite.java.migrate.Java8toJava11");
29+
}
30+
31+
@Test
32+
@DocumentExample
33+
void convertPathsGetToPathOf() {
34+
rewriteRun(
35+
//language=java
36+
java(
37+
"""
38+
import java.nio.file.Path;
39+
import java.nio.file.Paths;
40+
import java.net.URI;
41+
class A {
42+
Path pathA = Paths.get("path");
43+
Path pathB = Paths.get("path", "subpath");
44+
Path pathC = Paths.get(URI.create("file:///path"));
45+
}
46+
""",
47+
"""
48+
import java.nio.file.Path;
49+
import java.net.URI;
50+
class A {
51+
Path pathA = Path.of("path");
52+
Path pathB = Path.of("path", "subpath");
53+
Path pathC = Path.of(URI.create("file:///path"));
54+
}
55+
"""
56+
)
57+
);
58+
}
59+
}

0 commit comments

Comments
 (0)