Skip to content

Commit 32f5aea

Browse files
refactor: Unwrap else block after return or throw statement (#755)
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 eb5f701 commit 32f5aea

27 files changed

+256
-234
lines changed

src/main/java/org/openrewrite/java/spring/CommentOutSpringPropertyKey.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5757
stopAfterPreVisit();
5858
if (tree instanceof Properties.File) {
5959
return changeProperties.getVisitor().visit(tree, ctx);
60-
} else if (tree instanceof Yaml.Documents) {
60+
}
61+
if (tree instanceof Yaml.Documents) {
6162
return changeYaml.getVisitor().visit(tree, ctx);
6263
}
6364
return tree;

src/main/java/org/openrewrite/java/spring/NoRequestMappingAnnotation.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,27 +143,30 @@ private boolean methodArgumentHasSingleType(J.Assignment assignment) {
143143
}
144144

145145
private @Nullable String requestMethodType(J.@Nullable Assignment assignment) {
146-
if(assignment == null) {
146+
if (assignment == null) {
147147
return null;
148148
}
149149
if (assignment.getAssignment() instanceof J.Identifier) {
150150
return ((J.Identifier) assignment.getAssignment()).getSimpleName();
151-
} else if (assignment.getAssignment() instanceof J.FieldAccess) {
151+
}
152+
if (assignment.getAssignment() instanceof J.FieldAccess) {
152153
return ((J.FieldAccess) assignment.getAssignment()).getSimpleName();
153-
} else if (methodArgumentHasSingleType(assignment)) {
154-
if(assignment.getAssignment() instanceof J.NewArray) {
154+
}
155+
if (methodArgumentHasSingleType(assignment)) {
156+
if (assignment.getAssignment() instanceof J.NewArray) {
155157
J.NewArray newArray = (J.NewArray) assignment.getAssignment();
156158
List<Expression> initializer = newArray.getInitializer();
157-
if(initializer == null || initializer.size() != 1) {
159+
if (initializer == null || initializer.size() != 1) {
158160
return null;
159161
}
160162
Expression methodName = initializer.get(0);
161-
if(methodName instanceof J.Identifier) {
162-
return ((J.Identifier)methodName).getSimpleName();
163-
} else if(methodName instanceof J.FieldAccess) {
163+
if (methodName instanceof J.Identifier) {
164+
return ((J.Identifier) methodName).getSimpleName();
165+
}
166+
if (methodName instanceof J.FieldAccess) {
164167
return ((J.FieldAccess) methodName).getSimpleName();
165168
}
166-
} else if(assignment.getAssignment() instanceof J.Identifier) {
169+
} else if (assignment.getAssignment() instanceof J.Identifier) {
167170
return ((J.Identifier) assignment.getAssignment()).getSimpleName();
168171
}
169172
}

src/main/java/org/openrewrite/java/spring/RenameBean.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ private static BeanSearchResult isBean(Collection<J.Annotation> annotations, Set
205205
Expression assignmentExpr = beanNameAssignment.getAssignment();
206206
if (assignmentExpr instanceof J.Literal) {
207207
return new BeanSearchResult(true, (String) ((J.Literal) assignmentExpr).getValue());
208-
} else if (assignmentExpr instanceof J.NewArray) {
208+
}
209+
if (assignmentExpr instanceof J.NewArray) {
209210
List<Expression> initializers = ((J.NewArray) assignmentExpr).getInitializer();
210211
if (initializers != null) {
211212
for (Expression initExpr : initializers) {
@@ -283,9 +284,11 @@ private boolean annotationParentMatchesBeanType() {
283284

284285
if (annotationParent instanceof J.MethodDeclaration) {
285286
return isRelevantType(((J.MethodDeclaration) annotationParent).getMethodType().getReturnType());
286-
} else if (annotationParent instanceof J.ClassDeclaration) {
287+
}
288+
if (annotationParent instanceof J.ClassDeclaration) {
287289
return isRelevantType(((J.ClassDeclaration) annotationParent).getType());
288-
} else if (annotationParent instanceof J.VariableDeclarations) {
290+
}
291+
if (annotationParent instanceof J.VariableDeclarations) {
289292
return isRelevantType(((J.VariableDeclarations) annotationParent).getType());
290293
}
291294
}
@@ -376,7 +379,8 @@ private Expression replace(Expression assignment, String oldName, String newName
376379
private static boolean contains(Expression assignment, String oldName) {
377380
if (assignment instanceof J.Literal) {
378381
return oldName.equals(((J.Literal) assignment).getValue());
379-
} else if (assignment instanceof J.NewArray) {
382+
}
383+
if (assignment instanceof J.NewArray) {
380384
J.NewArray newArrayAssignment = (J.NewArray) assignment;
381385
if (newArrayAssignment.getInitializer() == null) {
382386
return false;

src/main/java/org/openrewrite/java/spring/UpdateApiManifest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,17 @@ private String getArg(J.Annotation annotation, String key, String defaultValue)
9999
if (argument instanceof J.Literal) {
100100
//noinspection ConstantConditions
101101
return (String) ((J.Literal) argument).getValue();
102-
} else if (argument instanceof J.Assignment) {
102+
}
103+
if (argument instanceof J.Assignment) {
103104
J.Assignment arg = (J.Assignment) argument;
104105
if (((J.Identifier) arg.getVariable()).getSimpleName().equals(key)) {
105106
if (arg.getAssignment() instanceof J.FieldAccess) {
106107
return ((J.FieldAccess) arg.getAssignment()).getSimpleName();
107-
} else if (arg.getAssignment() instanceof J.Identifier) {
108+
}
109+
if (arg.getAssignment() instanceof J.Identifier) {
108110
return ((J.Identifier) arg.getAssignment()).getSimpleName();
109-
} else if (arg.getAssignment() instanceof J.Literal) {
111+
}
112+
if (arg.getAssignment() instanceof J.Literal) {
110113
//noinspection ConstantConditions
111114
return (String) ((J.Literal) arg.getAssignment()).getValue();
112115
}

src/main/java/org/openrewrite/java/spring/batch/MigrateJobParameter.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
9292
multiVariable = new JNewClassOfMap().visitVariableDeclarations(multiVariable, ctx);
9393
maybeAddImport("java.util.Map");
9494
return multiVariable.withTypeExpression(TypeTree.build("Map<String, JobParameter<?>>")
95-
.withPrefix(multiVariable.getTypeExpression().getPrefix()))
95+
.withPrefix(multiVariable.getTypeExpression().getPrefix()))
9696
.withType(JavaType.buildType("java.util.Map"));
97-
} else if (defineMapEntryTypeWithJobParameter(multiVariable.getType())) {
97+
}
98+
if (defineMapEntryTypeWithJobParameter(multiVariable.getType())) {
9899
maybeAddImport("java.util.Map");
99100
return multiVariable.withTypeExpression(TypeTree.build("Map.Entry<String, JobParameter<?>>")
100-
.withPrefix(multiVariable.getTypeExpression().getPrefix()))
101+
.withPrefix(multiVariable.getTypeExpression().getPrefix()))
101102
.withType(JavaType.buildType("java.util.Map.Entry"));
102103
}
103104
return multiVariable;
@@ -130,9 +131,11 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
130131
private @Nullable String typeString(@Nullable JavaType javaType) {
131132
if (javaType instanceof JavaType.Primitive) {
132133
return ((JavaType.Primitive) javaType).name();
133-
} else if (javaType instanceof JavaType.Class) {
134+
}
135+
if (javaType instanceof JavaType.Class) {
134136
return ((JavaType.Class) javaType).getClassName();
135-
} else if (javaType instanceof JavaType.Array) {
137+
}
138+
if (javaType instanceof JavaType.Array) {
136139
return javaType.toString();
137140
}
138141
return null;

src/main/java/org/openrewrite/java/spring/batch/MigrateMethodAnnotatedByBatchAPI.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
6969
method = super.visitMethodDeclaration(method, ctx);
7070
doAfterVisit(new RefineMethod(method));
7171
return method;
72-
} else {
73-
return super.visitMethodDeclaration(method, ctx);
7472
}
73+
return super.visitMethodDeclaration(method, ctx);
7574

7675
}
7776
};

src/main/java/org/openrewrite/java/spring/boot2/AddConfigurationAnnotationIfBeansPresent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ public static boolean isApplicableClass(J.ClassDeclaration classDecl, Cursor cur
8383
for (J.Modifier m : classDecl.getModifiers()) {
8484
if (m.getType() == J.Modifier.Type.Abstract) {
8585
return false;
86-
} else if (m.getType() == J.Modifier.Type.Static) {
86+
}
87+
if (m.getType() == J.Modifier.Type.Static) {
8788
isStatic = true;
8889
}
8990
}

src/main/java/org/openrewrite/java/spring/boot2/ConvertToSecurityDslVisitor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ private Optional<JavaType.Method> createDesiredReplacementForArg(J.MethodInvocat
229229
public boolean isApplicableTopLevelMethodInvocation(J.MethodInvocation m) {
230230
if (isApplicableMethod(m)) {
231231
return true;
232-
} else if (m.getSelect() instanceof J.MethodInvocation) {
232+
}
233+
if (m.getSelect() instanceof J.MethodInvocation) {
233234
return isApplicableTopLevelMethodInvocation((J.MethodInvocation) m.getSelect());
234235
}
235236
return false;

src/main/java/org/openrewrite/java/spring/boot2/MoveAutoConfigurationToImportsFile.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ public Collection<SourceFile> generate(Accumulator acc, ExecutionContext ctx) {
124124

125125
if (!newImportFiles.isEmpty()) {
126126
return newImportFiles;
127-
} else {
128-
return Collections.emptyList();
129127
}
128+
return Collections.emptyList();
130129
}
131130

132131
@Override
@@ -271,9 +270,8 @@ private static SourceFile mergeEntries(SourceFile before, Set<String> configClas
271270

272271
if (merged.size() != original.size()) {
273272
return plainText.withText(String.join("\n", merged));
274-
} else {
275-
return before;
276273
}
274+
return before;
277275
}
278276

279277
@EqualsAndHashCode(callSuper = false)

src/main/java/org/openrewrite/java/spring/boot2/search/EntityIdForRepositoryVisitor.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -144,28 +144,27 @@ private J.ClassDeclaration handleRepoType(J.ClassDeclaration typeDecl) {
144144
int idx = params.indexOf(idType);
145145
if (idx < 0 || astParams == null || astParams.size() <= idx) {
146146
return typeDecl.withName(typeDecl.getName().withMarkers(typeDecl.getName().getMarkers().addIfAbsent(createMarker(domainIdType))));
147-
} else {
148-
astParams.set(idx, astParams.get(idx).withMarkers(astParams.get(idx).getMarkers().addIfAbsent(createMarker(domainIdType))));
149-
return typeDecl.withTypeParameters(astParams);
150147
}
151-
} else {
152-
if (typeDecl.getExtends() != null && repoTypeChain.get(1).equals(typeDecl.getExtends().getType())) {
153-
return typeDecl.withExtends(markTypeParam(typeDecl.getExtends(), idTypeIndex, createMarker(domainIdType)));
154-
} else if (typeDecl.getImplements() != null) {
155-
final int finalIdTypeIndex = idTypeIndex;
156-
final int finalIdTypeIndexInChain = idTypeIndexInChain;
157-
AtomicBoolean hasMarker = new AtomicBoolean(false);
158-
J.ClassDeclaration newTypeDecl = typeDecl.withImplements(ListUtils.map(typeDecl.getImplements(), it -> {
159-
JavaType.FullyQualified interfaceType = TypeUtils.asFullyQualified(it.getType());
160-
if (repoTypeChain.get(1).equals(interfaceType)) {
161-
hasMarker.set(true);
162-
return markTypeParam(it, finalIdTypeIndexInChain == 1 ? finalIdTypeIndex : -1, createMarker(domainIdType));
163-
}
164-
return it;
165-
}));
166-
if (hasMarker.get()) {
167-
return newTypeDecl;
148+
astParams.set(idx, astParams.get(idx).withMarkers(astParams.get(idx).getMarkers().addIfAbsent(createMarker(domainIdType))));
149+
return typeDecl.withTypeParameters(astParams);
150+
}
151+
if (typeDecl.getExtends() != null && repoTypeChain.get(1).equals(typeDecl.getExtends().getType())) {
152+
return typeDecl.withExtends(markTypeParam(typeDecl.getExtends(), idTypeIndex, createMarker(domainIdType)));
153+
}
154+
if (typeDecl.getImplements() != null) {
155+
final int finalIdTypeIndex = idTypeIndex;
156+
final int finalIdTypeIndexInChain = idTypeIndexInChain;
157+
AtomicBoolean hasMarker = new AtomicBoolean(false);
158+
J.ClassDeclaration newTypeDecl = typeDecl.withImplements(ListUtils.map(typeDecl.getImplements(), it -> {
159+
JavaType.FullyQualified interfaceType = TypeUtils.asFullyQualified(it.getType());
160+
if (repoTypeChain.get(1).equals(interfaceType)) {
161+
hasMarker.set(true);
162+
return markTypeParam(it, finalIdTypeIndexInChain == 1 ? finalIdTypeIndex : -1, createMarker(domainIdType));
168163
}
164+
return it;
165+
}));
166+
if (hasMarker.get()) {
167+
return newTypeDecl;
169168
}
170169
}
171170
return typeDecl.withName(typeDecl.getName().withMarkers(typeDecl.getName().getMarkers().addIfAbsent(createMarker(domainIdType))));

0 commit comments

Comments
 (0)