-
Notifications
You must be signed in to change notification settings - Fork 93
Replace week year (YYYY) with year (yyyy) in date formats #129
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 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2b6a115
creating new recipe to replace Week Year with Year formats
AlekSimpson 9cf2f77
first test passes
AlekSimpson 355341a
Update src/main/java/org/openrewrite/staticanalysis/ReplaceWeekYearWi…
AlekSimpson 4cbfe7c
Update src/test/java/org/openrewrite/staticanalysis/ReplaceWeekYearWi…
AlekSimpson 974c211
Update src/test/java/org/openrewrite/staticanalysis/ReplaceWeekYearWi…
AlekSimpson c4bd801
Update src/test/java/org/openrewrite/staticanalysis/ReplaceWeekYearWi…
AlekSimpson 5244382
all tests pass, updated tests, changed visitor type, made recipe more…
AlekSimpson 03f886b
added license headers
AlekSimpson 42289b1
updated recipe so that it does not run on all strings
AlekSimpson 71e25a0
updated recipe to be selective with which literals are changed, and a…
AlekSimpson 47b48b5
Update ReplaceWeekYearWithYear.java
AlekSimpson 9b1f649
Update ReplaceWeekYearWithYearTest.java
AlekSimpson 384c8e2
fixed a weird issue
AlekSimpson 7d977be
update
AlekSimpson 5211ebe
removed unneeded DS_Store file from repo
AlekSimpson 658a332
update
AlekSimpson 4507667
Merge remote-tracking branch 'origin/main' into alek/ReplaceWeekYearW…
knutwannheden 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
77 changes: 77 additions & 0 deletions
77
src/main/java/org/openrewrite/staticanalysis/ReplaceWeekYearWithYear.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,77 @@ | ||
| package org.openrewrite.staticanalysis; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.JavaTemplate; | ||
| import org.openrewrite.java.MethodMatcher; | ||
| import org.openrewrite.java.tree.*; | ||
| import org.openrewrite.marker.Markers; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Set; | ||
|
|
||
| import static org.openrewrite.Tree.randomId; | ||
|
|
||
| public class ReplaceWeekYearWithYear extends Recipe { | ||
| private static final MethodMatcher DATE_FORMAT_MATCHER = new MethodMatcher("java.text.SimpleDateFormat <constructor>(..)"); | ||
| private static final MethodMatcher OF_PATTERN_MATCHER = new MethodMatcher("java.time.format.DateTimeFormatter ofPattern(..)"); | ||
AlekSimpson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Week Year (YYYY) should not be used for date formatting"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "For most dates Week Year (YYYY) and Year (yyyy) yield the same results. However, on the last week of" + | ||
| " December and first week of January Week Year could produce unexpected results."; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> getTags() { | ||
| return Collections.singleton("RSPEC-3986"); | ||
| } | ||
|
|
||
| @Override | ||
| public JavaIsoVisitor<ExecutionContext> getVisitor() { | ||
| return new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.NewClass visitNewClass(J.NewClass nc, ExecutionContext ctx) { | ||
| if (!DATE_FORMAT_MATCHER.matches(nc.getConstructorType())) { | ||
| return nc; | ||
| } | ||
|
|
||
| Expression argument = nc.getArguments().get(0); | ||
| String formatString = argument.print(getCursor()); | ||
|
|
||
| if (formatString.equals("\"YYYY/MM/dd\"")) { | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| formatString = formatString.replace('Y', 'y'); | ||
| // remove surrounding quotes around string | ||
| formatString = StringUtils.chop(formatString); | ||
| formatString = formatString.substring(1); | ||
|
|
||
| J.Literal newFormat = new J.Literal( | ||
| randomId(), | ||
| Space.EMPTY, | ||
| Markers.EMPTY, | ||
| formatString, | ||
| "\"" + formatString + "\"", | ||
| null, | ||
| JavaType.Primitive.String | ||
| ); | ||
|
|
||
| System.out.println("getting here"); | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return JavaTemplate.builder("new SimpleDateFormat(#{any(java.lang.String)})") | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .contextSensitive() | ||
|
||
| .build() | ||
| .apply(getCursor(), nc.getCoordinates().replace(), newFormat); | ||
| } | ||
|
|
||
| return nc; | ||
| } | ||
| }; | ||
| } | ||
| } | ||
82 changes: 82 additions & 0 deletions
82
src/test/java/org/openrewrite/staticanalysis/ReplaceWeekYearWithYearTest.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,82 @@ | ||
| package org.openrewrite.staticanalysis; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junitpioneer.jupiter.Issue; | ||
| import org.openrewrite.DocumentExample; | ||
| import org.openrewrite.test.RecipeSpec; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| import static org.openrewrite.java.Assertions.java; | ||
|
|
||
| @SuppressWarnings("SuspiciousDateFormat") | ||
| public class ReplaceWeekYearWithYearTest implements RewriteTest { | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec | ||
| .recipe(new ReplaceWeekYearWithYear()); | ||
| } | ||
|
|
||
| @Test | ||
| @DocumentExample | ||
| @Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/58") | ||
| public void changeSimpleDateFormat() { | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //language=java | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| import java.text.SimpleDateFormat; | ||
| import java.util.Date; | ||
|
|
||
| class Test { | ||
| public void formatDate() { | ||
| Date date = new SimpleDateFormat("yyyy/MM/dd").parse("2015/12/31"); | ||
| String result = new SimpleDateFormat("YYYY/MM/dd").format(date); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| import java.text.SimpleDateFormat; | ||
| import java.util.Date; | ||
|
|
||
| class Test { | ||
| public void formatDate() { | ||
| Date date = new SimpleDateFormat("yyyy/MM/dd").parse("2015/12/31"); | ||
| String result = new SimpleDateFormat("yyyy/MM/dd").format(date); | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void worksWithOfPatternFormatter() { | ||
AlekSimpson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //language=java | ||
| rewriteRun( | ||
| java( | ||
| """ | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.Date; | ||
|
|
||
| class Test { | ||
| public void formatDate() { | ||
| Date date = new SimpleDateFormat("yyyy/MM/dd").parse("2015/12/31"); | ||
| String result = DateTimeFormatter.ofPattern("YYYY/MM/dd").format(date); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.Date; | ||
|
|
||
| class Test { | ||
| public void formatDate() { | ||
| Date date = new SimpleDateFormat("yyyy/MM/dd").parse("2015/12/31"); | ||
| String result = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(date); | ||
| } | ||
| } | ||
| """ | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
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.