Hi, I’m currently using 7.9.6 LTS version SonarQube template provided on the official github rules writing tutorial - Writing Custom Java Rules 101
I am writing custom rules in java and trying to obtain the value(s) inside a Spring Boot Annotation.
For example: @RequestMapping(path = “/123”)
I browse through a similar case - Setting up custom sonar rule for Annotations , and I believe I am facing the same issue as mentioned in that topic.
Currently, My code for obtaining the value in the @RequestMapping Annotation is
@Override
public void visitMethod(MethodTree tree) {
String name = "RequestMapping";
boolean isHavingMandatoryAnnotation = Boolean.FALSE;
for (SymbolMetadata.AnnotationInstance annotation : tree.symbol().metadata().annotations()){
if (annotation.symbol().name().equals(name)) {
List<SymbolMetadata.AnnotationValue> valuesForAnnotation = annotation.symbol().metadata().valuesForAnnotation("org.springframework.web.bind.annotation.RequestMapping");
Boolean isPathCorrect = valuesForAnnotation.stream()
.filter(annotationValue -> "path".equals(annotationValue.name()))
.anyMatch(annotationValue -> "123".equals(annotationValue.value()));
if (isPathCorrect) {
isHavingMandatoryAnnotation = Boolean.TRUE;
}
}
}
if (!isHavingMandatoryAnnotation) {
context.reportIssue(this, tree.simpleName(), String.format("Mandatory Annotation not set @%s", name));
}
super.visitMethod(tree);
}
I realize I get null value returned for this line:
List<SymbolMetadata.AnnotationValue> valuesForAnnotation = annotation.symbol().metadata().valuesForAnnotation("org.springframework.web.bind.annotation.RequestMapping");
which is similar to this post
The link hyperlink for the provided solution is not available anymore.
May I know the possible cause and solution to my issue?
Thank you,