Skip to content

Commit e4b2a8e

Browse files
refactor: Unwrap else block after return or throw statement (#798)
Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.staticanalysis.UnwrapElseAfterReturn?organizationId=ODQ2MGExMTUtNDg0My00N2EwLTgzMGMtNGE1NGExMTBmZDkw Co-authored-by: Moderne <[email protected]>
1 parent 7b4fb61 commit e4b2a8e

22 files changed

+80
-54
lines changed

src/main/java/org/openrewrite/java/migrate/JREThrowableFinalMethods.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
8484
JavaType.Method myAddSuppressed = mt.withName("myAddSuppressed");
8585
return md.withName(md.getName().withSimpleName("myAddSuppressed").withType(myAddSuppressed))
8686
.withMethodType(myAddSuppressed);
87-
} else if (METHOD_GETSUPPRESSED.matches(md, classDeclaration)) {
87+
}
88+
if (METHOD_GETSUPPRESSED.matches(md, classDeclaration)) {
8889
JavaType.Method myGetSuppressed = mt.withName("myGetSuppressed");
8990
return md.withName(md.getName().withSimpleName("myGetSuppressed").withType(myGetSuppressed))
9091
.withMethodType(myGetSuppressed);

src/main/java/org/openrewrite/java/migrate/JpaCacheProperties.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,14 @@ private void getDataCacheProps(Xml.Tag puNode, SharedDataHolder sdh) {
289289
if (propVal != null) {
290290
if ("false".equalsIgnoreCase(propVal)) {
291291
return "NONE";
292-
} else if ("true".equalsIgnoreCase(propVal)) {
292+
}
293+
if ("true".equalsIgnoreCase(propVal)) {
293294
return "ALL";
294-
} else if (propVal.matches("(?i:true)\\(ExcludedTypes=.*")) {
295+
}
296+
if (propVal.matches("(?i:true)\\(ExcludedTypes=.*")) {
295297
return "DISABLE_SELECTIVE";
296-
} else if (propVal.matches("(?i:true)\\(Types=.*")) {
298+
}
299+
if (propVal.matches("(?i:true)\\(Types=.*")) {
297300
return "ENABLE_SELECTIVE";
298301
}
299302
}
@@ -305,7 +308,8 @@ private void getDataCacheProps(Xml.Tag puNode, SharedDataHolder sdh) {
305308
private @Nullable String convertScmValue(String scmValue) {
306309
if ("NONE".equals(scmValue)) {
307310
return "false";
308-
} else if ("ALL".equals(scmValue)) {
311+
}
312+
if ("ALL".equals(scmValue)) {
309313
return "true";
310314
}
311315
// otherwise, don't process it

src/main/java/org/openrewrite/java/migrate/UseJavaUtilBase64.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ public J visitNewClass(J.NewClass newClass, ExecutionContext ctx) {
129129
.build()
130130
.apply(updateCursor(c), c.getCoordinates().replace());
131131

132-
} else if (newBase64Decoder.matches(c)) {
132+
}
133+
if (newBase64Decoder.matches(c)) {
133134
return JavaTemplate.builder(useMimeCoder ? "Base64.getMimeDecoder()" : "Base64.getDecoder()")
134135
.contextSensitive()
135136
.imports("java.util.Base64")

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewArrayList.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,15 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
7676
maybeRemoveImport("com.google.common.collect.Lists");
7777
maybeAddImport("java.util.ArrayList");
7878
return newArrayList.apply(getCursor(), method.getCoordinates().replace());
79-
} else if (NEW_ARRAY_LIST_ITERABLE.matches(method) && method.getArguments().size() == 1 &&
79+
}
80+
if (NEW_ARRAY_LIST_ITERABLE.matches(method) && method.getArguments().size() == 1 &&
8081
TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
8182
maybeRemoveImport("com.google.common.collect.Lists");
8283
maybeAddImport("java.util.ArrayList");
8384
return newArrayListCollection.apply(getCursor(), method.getCoordinates().replace(),
8485
method.getArguments().get(0));
85-
} else if (NEW_ARRAY_LIST_CAPACITY.matches(method)) {
86+
}
87+
if (NEW_ARRAY_LIST_CAPACITY.matches(method)) {
8688
maybeRemoveImport("com.google.common.collect.Lists");
8789
maybeAddImport("java.util.ArrayList");
8890
return newArrayListCapacity.apply(getCursor(), method.getCoordinates().replace(),

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewCopyOnWriteArrayList.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
6363
.imports("java.util.concurrent.CopyOnWriteArrayList")
6464
.build()
6565
.apply(getCursor(), method.getCoordinates().replace());
66-
} else if (NEW_ARRAY_LIST_ITERABLE.matches(method) && method.getArguments().size() == 1 &&
67-
TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
66+
}
67+
if (NEW_ARRAY_LIST_ITERABLE.matches(method) && method.getArguments().size() == 1 &&
68+
TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
6869
maybeRemoveImport("com.google.common.collect.Lists");
6970
maybeAddImport("java.util.concurrent.CopyOnWriteArrayList");
7071
return JavaTemplate.builder("new CopyOnWriteArrayList<>(#{any(java.util.Collection)})")

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaListsNewLinkedList.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
6363
.imports("java.util.LinkedList")
6464
.build()
6565
.apply(getCursor(), method.getCoordinates().replace());
66-
} else if (NEW_LINKED_LIST_ITERABLE.matches(method) && method.getArguments().size() == 1 &&
67-
TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
66+
}
67+
if (NEW_LINKED_LIST_ITERABLE.matches(method) && method.getArguments().size() == 1 &&
68+
TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
6869
maybeRemoveImport("com.google.common.collect.Lists");
6970
maybeAddImport("java.util.LinkedList");
7071
return JavaTemplate.builder("new LinkedList<>(#{any(java.util.Collection)})")

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewHashMap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
6262
.imports("java.util.HashMap")
6363
.build()
6464
.apply(getCursor(), method.getCoordinates().replace());
65-
} else if (NEW_HASH_MAP_WITH_MAP.matches(method)) {
65+
}
66+
if (NEW_HASH_MAP_WITH_MAP.matches(method)) {
6667
maybeRemoveImport("com.google.common.collect.Maps");
6768
maybeAddImport("java.util.HashMap");
6869
return JavaTemplate.builder("new HashMap<>(#{any(java.util.Map)})")

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewLinkedHashMap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
6262
.imports("java.util.LinkedHashMap")
6363
.build()
6464
.apply(getCursor(), method.getCoordinates().replace());
65-
} else if (NEW_LINKED_HASH_MAP_WITH_MAP.matches(method)) {
65+
}
66+
if (NEW_LINKED_HASH_MAP_WITH_MAP.matches(method)) {
6667
maybeRemoveImport("com.google.common.collect.Maps");
6768
maybeAddImport("java.util.LinkedHashMap");
6869
return JavaTemplate.builder("new LinkedHashMap<>(#{any(java.util.Map)})")

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaMapsNewTreeMap.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,17 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
6464
.imports("java.util.TreeMap")
6565
.build()
6666
.apply(getCursor(), method.getCoordinates().replace());
67-
} else if (NEW_TREE_MAP_WITH_COMPARATOR.matches(method)) {
67+
}
68+
if (NEW_TREE_MAP_WITH_COMPARATOR.matches(method)) {
6869
maybeRemoveImport("com.google.common.collect.Maps");
6970
maybeAddImport("java.util.TreeMap");
7071
return JavaTemplate.builder("new TreeMap<>(#{any(java.util.Comparator)})")
7172
.contextSensitive()
7273
.imports("java.util.TreeMap")
7374
.build()
7475
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
75-
} else if (NEW_TREE_MAP_WITH_MAP.matches(method)) {
76+
}
77+
if (NEW_TREE_MAP_WITH_MAP.matches(method)) {
7678
maybeRemoveImport("com.google.common.collect.Maps");
7779
maybeAddImport("java.util.TreeMap");
7880
return JavaTemplate.builder("new TreeMap<>(#{any(java.util.Map)})")

src/main/java/org/openrewrite/java/migrate/guava/NoGuavaSetsNewHashSet.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,22 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
6262
.imports("java.util.HashSet")
6363
.build()
6464
.apply(getCursor(), method.getCoordinates().replace());
65-
} else if (method.getArguments().size() == 1 && TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
65+
}
66+
if (method.getArguments().size() == 1 && TypeUtils.isAssignableTo("java.util.Collection", method.getArguments().get(0).getType())) {
6667
return JavaTemplate.builder("new HashSet<>(#{any(java.util.Collection)})")
6768
.contextSensitive()
6869
.imports("java.util.HashSet")
6970
.build()
7071
.apply(getCursor(), method.getCoordinates().replace(), method.getArguments().get(0));
71-
} else {
72-
maybeAddImport("java.util.Arrays");
73-
JavaTemplate newHashSetVarargs = JavaTemplate.builder("new HashSet<>(Arrays.asList(" + method.getArguments().stream().map(a -> "#{any()}").collect(Collectors.joining(",")) + "))")
74-
.contextSensitive()
75-
.imports("java.util.Arrays")
76-
.imports("java.util.HashSet")
77-
.build();
78-
return newHashSetVarargs.apply(getCursor(), method.getCoordinates().replace(),
79-
method.getArguments().toArray());
8072
}
73+
maybeAddImport("java.util.Arrays");
74+
JavaTemplate newHashSetVarargs = JavaTemplate.builder("new HashSet<>(Arrays.asList(" + method.getArguments().stream().map(a -> "#{any()}").collect(Collectors.joining(",")) + "))")
75+
.contextSensitive()
76+
.imports("java.util.Arrays")
77+
.imports("java.util.HashSet")
78+
.build();
79+
return newHashSetVarargs.apply(getCursor(), method.getCoordinates().replace(),
80+
method.getArguments().toArray());
8181
}
8282
return super.visitMethodInvocation(method, ctx);
8383
}

0 commit comments

Comments
 (0)