-
Notifications
You must be signed in to change notification settings - Fork 92
Prefer System getProperty() over getenv()
#679
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
timtebeek
merged 11 commits into
openrewrite:main
from
Akanksha928:fix/use-system-getproperty
Aug 21, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7007691
Prefer System getProperty() over getenv()
Akanksha928 30f1ec2
Merge branch 'main' into fix/use-system-getproperty
timtebeek 91c11bf
Apply suggestions from code review
timtebeek d6059b1
Make PR comment changes
Akanksha928 0559fe0
Apply suggestions from code review
timtebeek 2815425
Merge branch 'main' into fix/use-system-getproperty
Akanksha928 aac848c
extend recipe to support multiple System.getenv()
Akanksha928 e185702
Polish
timtebeek 1b86edd
Add markdown in displayName and description
timtebeek 8c2aeed
Merge branch 'main' into fix/use-system-getproperty
timtebeek bf71860
Add to common-static-analysis.yml
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
75 changes: 75 additions & 0 deletions
75
src/main/java/org/openrewrite/staticanalysis/PreferSystemGetPropertyOverGetenv.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,75 @@ | ||
| /* | ||
| * 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.staticanalysis; | ||
|
|
||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Preconditions; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.MethodMatcher; | ||
| import org.openrewrite.java.search.UsesMethod; | ||
| import org.openrewrite.java.tree.J; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class PreferSystemGetPropertyOverGetenv extends Recipe { | ||
|
|
||
| private static final MethodMatcher GETENV = new MethodMatcher("java.lang.System getenv(java.lang.String)"); | ||
| private static final Map<String, String> ENV_TO_PROPERTY = new HashMap<>(); | ||
|
|
||
| static { | ||
| ENV_TO_PROPERTY.put("USER", "user.name"); | ||
| ENV_TO_PROPERTY.put("USERNAME", "user.name"); | ||
| ENV_TO_PROPERTY.put("HOME", "user.home"); | ||
| ENV_TO_PROPERTY.put("USERPROFILE", "user.home"); | ||
| ENV_TO_PROPERTY.put("TMPDIR", "java.io.tmpdir"); | ||
| ENV_TO_PROPERTY.put("TEMP", "java.io.tmpdir"); | ||
| ENV_TO_PROPERTY.put("TMP", "java.io.tmpdir"); | ||
| } | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Prefer `System.getProperty(\"user.home\")` over `System.getenv(\"HOME\")`"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Replaces `System.getenv(\"HOME\")` with `System.getProperty(\"user.home\")` for better portability."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return Preconditions.check(new UsesMethod<>(GETENV), new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
| if (GETENV.matches(method) && method.getArguments().get(0) instanceof J.Literal) { | ||
| String value = (String) ((J.Literal) method.getArguments().get(0)).getValue(); | ||
| String replacementProperty = ENV_TO_PROPERTY.get(value); | ||
| if (replacementProperty != null) { | ||
| return JavaTemplate.apply( | ||
| "System.getProperty(\"" + replacementProperty + "\")", | ||
| getCursor(), | ||
| method.getCoordinates().replace()); | ||
| } | ||
| } | ||
| return super.visitMethodInvocation(method, ctx); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
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
66 changes: 66 additions & 0 deletions
66
src/test/java/org/openrewrite/staticanalysis/PreferSystemGetPropertyOverGetenvTest.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,66 @@ | ||
| /* | ||
| * 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.staticanalysis; | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.openrewrite.DocumentExample; | ||
| import org.openrewrite.test.RecipeSpec; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| import static org.openrewrite.java.Assertions.java; | ||
|
|
||
| class PreferSystemGetPropertyOverGetenvTest implements RewriteTest { | ||
|
|
||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec.recipe(new PreferSystemGetPropertyOverGetenv()); | ||
| } | ||
|
|
||
| @DocumentExample | ||
| @Test | ||
timtebeek marked this conversation as resolved.
Show resolved
Hide resolved
Akanksha928 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| void replacesMultipleEnvVariables() { | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| class A { | ||
| void test() { | ||
| String user = System.getenv("USER"); | ||
| String username = System.getenv("USERNAME"); | ||
| String home = System.getenv("HOME"); | ||
| String profile = System.getenv("USERPROFILE"); | ||
| String temp = System.getenv("TEMP"); | ||
| String tmpdir = System.getenv("TMPDIR"); | ||
| String tmp = System.getenv("TMP"); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| class A { | ||
| void test() { | ||
| String user = System.getProperty("user.name"); | ||
| String username = System.getProperty("user.name"); | ||
| String home = System.getProperty("user.home"); | ||
| String profile = System.getProperty("user.home"); | ||
| String temp = System.getProperty("java.io.tmpdir"); | ||
| String tmpdir = System.getProperty("java.io.tmpdir"); | ||
| String tmp = System.getProperty("java.io.tmpdir"); | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
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.