Skip to content

Commit dc0b9e7

Browse files
authored
Merge branch 'main' into explicit-this
2 parents e59f110 + f51717c commit dc0b9e7

File tree

8 files changed

+360
-150
lines changed

8 files changed

+360
-150
lines changed

src/main/java/org/openrewrite/staticanalysis/NestedEnumsAreNotStatic.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
import org.openrewrite.internal.ListUtils;
2323
import org.openrewrite.java.JavaIsoVisitor;
2424
import org.openrewrite.java.tree.J;
25-
import org.openrewrite.marker.SearchResult;
2625
import org.openrewrite.staticanalysis.csharp.CSharpFileChecker;
2726

2827
import java.time.Duration;
2928
import java.util.Set;
3029

3130
import static java.util.Collections.singleton;
31+
import static org.openrewrite.java.tree.J.Modifier.Type.Static;
3232

3333
public class NestedEnumsAreNotStatic extends Recipe {
3434
@Override
@@ -53,34 +53,27 @@ public Duration getEstimatedEffortPerOccurrence() {
5353

5454
@Override
5555
public TreeVisitor<?, ExecutionContext> getVisitor() {
56-
TreeVisitor<?, ExecutionContext> preconditions = Preconditions.and(new HasNestedEnum(), Preconditions.not(new CSharpFileChecker<>()));
57-
return Preconditions.check(preconditions, new JavaIsoVisitor<ExecutionContext>() {
56+
return Preconditions.check(Preconditions.not(new CSharpFileChecker<>()), new JavaIsoVisitor<ExecutionContext>() {
5857
@Override
5958
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
6059
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
61-
if (cd.getKind() == J.ClassDeclaration.Kind.Type.Enum && cd.getType() != null && cd.getType().getOwningClass() != null) {
62-
if (J.Modifier.hasModifier(cd.getModifiers(), J.Modifier.Type.Static)) {
63-
cd = maybeAutoFormat(cd,
64-
cd.withModifiers(ListUtils.map(cd.getModifiers(), mod ->
65-
mod.getType() == J.Modifier.Type.Static ? null : mod)),
66-
cd.getName(),
67-
ctx,
68-
getCursor().getParent());
69-
}
60+
if (shouldRemoveStaticModifierFromClass(cd)) {
61+
return maybeAutoFormat(
62+
cd,
63+
cd.withModifiers(ListUtils.filter(cd.getModifiers(), mod -> mod.getType() != Static)),
64+
cd.getName(),
65+
ctx,
66+
getCursor().getParent());
7067
}
7168
return cd;
7269
}
73-
});
74-
}
7570

76-
private static class HasNestedEnum extends JavaIsoVisitor<ExecutionContext> {
77-
@Override
78-
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
79-
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
80-
if (cd.getKind() == J.ClassDeclaration.Kind.Type.Enum && cd.getType() != null && cd.getType().getOwningClass() != null) {
81-
cd = SearchResult.found(cd);
71+
private boolean shouldRemoveStaticModifierFromClass(J.ClassDeclaration cd) {
72+
return cd.getKind() == J.ClassDeclaration.Kind.Type.Enum &&
73+
cd.getType() != null &&
74+
cd.getType().getOwningClass() != null &&
75+
J.Modifier.hasModifier(cd.getModifiers(), Static);
8276
}
83-
return cd;
84-
}
77+
});
8578
}
8679
}

src/main/java/org/openrewrite/staticanalysis/RenameExceptionInEmptyCatch.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.time.Duration;
2727
import java.util.*;
2828

29-
import static java.util.Objects.requireNonNull;
3029
import static java.util.stream.Collectors.toList;
3130

3231
public class RenameExceptionInEmptyCatch extends Recipe {
@@ -53,7 +52,6 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5352
@Override
5453
public J visit(@Nullable Tree tree, ExecutionContext ctx) {
5554
if (tree instanceof JavaSourceFile) {
56-
JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree);
5755
Map<Cursor, Set<String>> variableScopes = new LinkedHashMap<>();
5856
getCursor().putMessage("VARIABLES_KEY", variableScopes);
5957
}

src/main/java/org/openrewrite/staticanalysis/ReplaceDuplicateStringLiterals.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,6 @@ public J visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ct
145145
variableNames.add(variableName);
146146
entry.getValue().forEach(v -> replacements.put(v, variableName));
147147
}
148-
duplicateLiteralInfo = null;
149-
duplicateLiteralsMap = null;
150148
return replacements.isEmpty() ? classDecl :
151149
new ReplaceStringLiterals(classDecl, replacements).visitNonNull(classDecl, ctx, requireNonNull(getCursor().getParent()));
152150
}

src/main/java/org/openrewrite/staticanalysis/ReplaceWeekYearWithYear.java

Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.openrewrite.*;
1919
import org.openrewrite.java.JavaIsoVisitor;
2020
import org.openrewrite.java.MethodMatcher;
21-
import org.openrewrite.java.search.UsesType;
21+
import org.openrewrite.java.search.UsesMethod;
2222
import org.openrewrite.java.tree.J;
2323

2424
import java.util.Set;
@@ -49,78 +49,63 @@ public Set<String> getTags() {
4949
public TreeVisitor<?, ExecutionContext> getVisitor() {
5050
return Preconditions.check(
5151
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)
5554
),
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);
8862
}
8963

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+
}
9171

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+
}
9384
return li;
9485
}
9586

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+
}
120107
}
121-
}
122-
123-
return output.toString();
124-
}
108+
);
125109
}
110+
126111
}

0 commit comments

Comments
 (0)