Skip to content
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

Replace week year (YYYY) with year (yyyy) in date formats #129

Merged
merged 17 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>(..)");
AlekSimpson marked this conversation as resolved.
Show resolved Hide resolved
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.
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.
Show resolved Hide resolved

return JavaTemplate.builder("new SimpleDateFormat(#{any(java.lang.String)})")
AlekSimpson marked this conversation as resolved.
Show resolved Hide resolved
.contextSensitive()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure about the contextSensitive here; what made you add that?

.build()
.apply(getCursor(), nc.getCoordinates().replace(), newFormat);
}

return nc;
}
};
}
}
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.
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.
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.
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);
}
}
"""
)
);
}
}
Loading