You need to optimize this code:
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import javax.ws.rs.CookieParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
...
private String valueParam(Annotation a) {
String value = "";
if (a.annotationType() == QueryParam.class) {
value = ((QueryParam) a).value();
} else if (a.annotationType() == PathParam.class) {
value = ((PathParam) a).value();
} else if (a.annotationType() == CookieParam.class) {
value = ((CookieParam) a).value();
} else if (a.annotationType() == HeaderParam.class) {
value = ((HeaderParam) a).value();
} else if (a.annotationType() == MatrixParam.class) {
value = ((MatrixParam) a).value();
} else if (a.annotationType() == FormParam.class) {
value = ((FormParam) a).value();
}
return value;
}
SonarQube complains about the complexity of this method.
It is not so easy to change, because we need to check the type of annotation before getting its value!
Note. The trap is in the Annotation interface, which does not have a value () method.
PS: This code is based on this example (code example 4)
source
share