Skip to content

Commit

Permalink
Fix possible ClassCastException and cleanup warnings in NoRequestMapp…
Browse files Browse the repository at this point in the history
…ingAnnotation
  • Loading branch information
sambsnyd committed Feb 27, 2024
1 parent 9366e4b commit 5ab0147
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,17 @@ public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext ct
requestType.ifPresent(requestMethod -> maybeRemoveImport("org.springframework.web.bind.annotation.RequestMethod." + requestMethod));

// Remove the argument
if (requestMethodArg.isPresent() && methodArgumentHasSingleType(requestMethodArg.get()) && resolvedRequestMappingAnnotationClassName != null) {
if (methodArgumentHasSingleType(requestMethodArg.get())) {
if (a.getArguments() != null) {
a = a.withArguments(ListUtils.map(a.getArguments(), arg -> requestMethodArg.get().equals(arg) ? null : arg));
}
}

// Change the Annotation Type
if (resolvedRequestMappingAnnotationClassName != null) {
maybeAddImport("org.springframework.web.bind.annotation." + resolvedRequestMappingAnnotationClassName);
a = (J.Annotation) new ChangeType("org.springframework.web.bind.annotation.RequestMapping",
"org.springframework.web.bind.annotation." + resolvedRequestMappingAnnotationClassName, false)
.getVisitor().visit(a, ctx, getCursor());
}
maybeAddImport("org.springframework.web.bind.annotation." + resolvedRequestMappingAnnotationClassName);
a = (J.Annotation) new ChangeType("org.springframework.web.bind.annotation.RequestMapping",
"org.springframework.web.bind.annotation." + resolvedRequestMappingAnnotationClassName, false)
.getVisitor().visit(a, ctx, getCursor());

// if there is only one remaining argument now, and it is "path" or "value", then we can drop the key name
if (a != null && a.getArguments() != null && a.getArguments().size() == 1) {
Expand Down Expand Up @@ -142,15 +140,23 @@ private boolean methodArgumentHasSingleType(J.Assignment assignment) {
return newArray.getInitializer() != null && newArray.getInitializer().size() == 1;
}

@Nullable
private String requestMethodType(@Nullable J.Assignment assignment) {
if(assignment == null) {
return null;
}
if (assignment.getAssignment() instanceof J.Identifier) {
return ((J.Identifier) assignment.getAssignment()).getSimpleName();
} else if (assignment.getAssignment() instanceof J.FieldAccess) {
return ((J.FieldAccess) assignment.getAssignment()).getSimpleName();
} else if (methodArgumentHasSingleType(assignment)) {
J.NewArray newArray = (J.NewArray) assignment.getAssignment();
assert newArray.getInitializer() != null;
return ((J.FieldAccess) newArray.getInitializer().get(0)).getSimpleName();
if(assignment.getAssignment() instanceof J.NewArray) {
J.NewArray newArray = (J.NewArray) assignment.getAssignment();
assert newArray.getInitializer() != null;
return ((J.FieldAccess) newArray.getInitializer().get(0)).getSimpleName();
} else if(assignment.getAssignment() instanceof J.Identifier) {
return ((J.Identifier) assignment.getAssignment()).getSimpleName();
}
}
return null;
}
Expand Down

0 comments on commit 5ab0147

Please sign in to comment.