|
18 | 18 | import org.openrewrite.*; |
19 | 19 | import org.openrewrite.java.JavaIsoVisitor; |
20 | 20 | import org.openrewrite.java.MethodMatcher; |
21 | | -import org.openrewrite.java.search.UsesType; |
| 21 | +import org.openrewrite.java.search.UsesMethod; |
22 | 22 | import org.openrewrite.java.tree.J; |
23 | 23 |
|
24 | 24 | import java.util.Set; |
@@ -49,78 +49,63 @@ public Set<String> getTags() { |
49 | 49 | public TreeVisitor<?, ExecutionContext> getVisitor() { |
50 | 50 | return Preconditions.check( |
51 | 51 | Preconditions.or( |
52 | | - new UsesType<>("java.util.Date", false), |
53 | | - new UsesType<>("java.time.format.DateTimeFormatter", false), |
54 | | - new UsesType<>("java.text.SimpleDateFormat", false) |
| 52 | + new UsesMethod<>(SIMPLE_DATE_FORMAT_CONSTRUCTOR_MATCHER), |
| 53 | + new UsesMethod<>(OF_PATTERN_MATCHER) |
55 | 54 | ), |
56 | | - new ReplaceWeekYearVisitor() |
57 | | - ); |
58 | | - } |
59 | | - |
60 | | - private static class ReplaceWeekYearVisitor extends JavaIsoVisitor<ExecutionContext> { |
61 | | - @Override |
62 | | - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) { |
63 | | - if (OF_PATTERN_MATCHER.matches(mi)) { |
64 | | - getCursor().putMessage("KEY", mi); |
65 | | - } |
66 | | - |
67 | | - return super.visitMethodInvocation(mi, ctx); |
68 | | - } |
69 | | - |
70 | | - @Override |
71 | | - public J.NewClass visitNewClass(J.NewClass nc, ExecutionContext ctx) { |
72 | | - if (SIMPLE_DATE_FORMAT_CONSTRUCTOR_MATCHER.matches(nc)) { |
73 | | - getCursor().putMessage("KEY", nc); |
74 | | - } |
75 | | - |
76 | | - return super.visitNewClass(nc, ctx); |
77 | | - } |
78 | | - |
79 | | - @Override |
80 | | - public J.Literal visitLiteral(J.Literal li, ExecutionContext ctx) { |
81 | | - if (li.getValue() instanceof String) { |
82 | | - Cursor c = getCursor().dropParentWhile(is -> is instanceof J.Parentheses || !(is instanceof Tree)); |
83 | | - if (c.getMessage("KEY") != null) { |
84 | | - Object value = li.getValue(); |
85 | | - |
86 | | - if (value == null) { |
87 | | - return li; |
| 55 | + new JavaIsoVisitor<ExecutionContext>() { |
| 56 | + @Override |
| 57 | + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) { |
| 58 | + if (OF_PATTERN_MATCHER.matches(mi)) { |
| 59 | + getCursor().putMessage("KEY", mi); |
| 60 | + } |
| 61 | + return super.visitMethodInvocation(mi, ctx); |
88 | 62 | } |
89 | 63 |
|
90 | | - String newValue = replaceY(value.toString()); |
| 64 | + @Override |
| 65 | + public J.NewClass visitNewClass(J.NewClass nc, ExecutionContext ctx) { |
| 66 | + if (SIMPLE_DATE_FORMAT_CONSTRUCTOR_MATCHER.matches(nc)) { |
| 67 | + getCursor().putMessage("KEY", nc); |
| 68 | + } |
| 69 | + return super.visitNewClass(nc, ctx); |
| 70 | + } |
91 | 71 |
|
92 | | - if (newValue.equals(value.toString())) { |
| 72 | + @Override |
| 73 | + public J.Literal visitLiteral(J.Literal li, ExecutionContext ctx) { |
| 74 | + if (li.getValue() instanceof String) { |
| 75 | + Cursor c = getCursor().dropParentWhile(is -> is instanceof J.Parentheses || !(is instanceof Tree)); |
| 76 | + if (c.getMessage("KEY") != null) { |
| 77 | + Object value = li.getValue(); |
| 78 | + String newValue = replaceY(value.toString()); |
| 79 | + if (!newValue.equals(value.toString())) { |
| 80 | + return li.withValueSource("\"" + newValue + "\"").withValue(newValue); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
93 | 84 | return li; |
94 | 85 | } |
95 | 86 |
|
96 | | - return li.withValueSource("\"" + newValue + "\"").withValue(newValue); |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - return li; |
101 | | - } |
102 | | - |
103 | | - public static String replaceY(String input) { |
104 | | - StringBuilder output = new StringBuilder(); |
105 | | - boolean insideQuotes = false; |
106 | | - |
107 | | - for (int i = 0; i < input.length(); i++) { |
108 | | - char currentChar = input.charAt(i); |
109 | | - char nextChar = (i < input.length() - 1) ? input.charAt(i + 1) : '\0'; |
110 | | - |
111 | | - if (currentChar == '\'') { |
112 | | - insideQuotes = !insideQuotes; |
113 | | - output.append(currentChar); |
114 | | - } else if (currentChar == 'Y' && !insideQuotes) { |
115 | | - output.append('y'); |
116 | | - } else if (currentChar == 'Y' && nextChar == '\'') { |
117 | | - output.append(currentChar); |
118 | | - } else { |
119 | | - output.append(currentChar); |
| 87 | + private String replaceY(String input) { |
| 88 | + StringBuilder output = new StringBuilder(); |
| 89 | + boolean insideQuotes = false; |
| 90 | + |
| 91 | + for (int i = 0; i < input.length(); i++) { |
| 92 | + char currentChar = input.charAt(i); |
| 93 | + if (currentChar == '\'') { |
| 94 | + insideQuotes = !insideQuotes; |
| 95 | + output.append(currentChar); |
| 96 | + } else if (currentChar == 'Y' && !insideQuotes) { |
| 97 | + output.append('y'); |
| 98 | + } else if (currentChar == 'w' && !insideQuotes) { |
| 99 | + return input; |
| 100 | + } else { |
| 101 | + output.append(currentChar); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return output.toString(); |
| 106 | + } |
120 | 107 | } |
121 | | - } |
122 | | - |
123 | | - return output.toString(); |
124 | | - } |
| 108 | + ); |
125 | 109 | } |
| 110 | + |
126 | 111 | } |
0 commit comments