SONARJAVA-5163 Create rule S7198: Path.of should be preferred to Paths.get (#4670)

This commit is contained in:
github-actions[bot] 2025-02-26 16:50:20 +01:00 committed by GitHub
parent 1dc40b1a64
commit afc4f2638a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,26 @@
{
"title": "\"Path.of\" should be preferred to \"Paths.get\"",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"java11"
],
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-7198",
"sqKey": "S7198",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "targeted",
"code": {
"impacts": {
"MAINTAINABILITY": "MEDIUM",
"RELIABILITY": "LOW",
"SECURITY": "LOW"
},
"attribute": "CONVENTIONAL"
}
}

View File

@ -0,0 +1,29 @@
== Why is this an issue?
`Path.of()` provides a modern and consistent approach to creating Path instances, aligning with factory methods such as `List.of()` and `Set.of()`.
Additionally, `Paths.get()` may be deprecated in future versions of Java.
== How to fix it
Replace `Paths.get()` with `Path.of()`.
=== Code examples
==== Noncompliant code example
[source,java,diff-id=1,diff-type=noncompliant]
----
Path path = Paths.get("my/file.txt"); // non compliant
----
==== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
Path path = Path.of("my/file.txt"); // compliant
----
== Resources
=== Documentation
- Java Documentation - https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Paths.html[Paths]

View File

@ -0,0 +1,2 @@
{
}