jeudi 4 novembre 2021

How to care for Sonar S1142 in Java factory?

In case I have a "classic" Java Factory method with if statements that do nothing but returning a sub class, I get the Sonar issue S1142 in case of more than 3 return statements.

Example:

public static AbstractJsonErrorHandler getErrorHandler(final Throwable ex, final ErrorService errorService,
            final String originalMsg) {

    if (ex instanceof final MismatchedInputException mex) {
        return new JsonMismatchHandler(mex, errorService);
    }

    if (ex instanceof final JsonEOFException jex) {
        return new JsonEofErrorHandler(jex, errorService);
    }

    if (ex instanceof final JsonParseException jex) {
        return new JsonSyntacticalErrorHandler(jex, errorService);
    }

    if (ex instanceof MalformedURLException) {
        return new JsonSemanticErrorHandler(errorService, originalMsg);
    }

    return new JsonNotReadableErrorHandler((HttpMessageNotReadableException) ex, errorService);
}

What would be the solution?

Aucun commentaire:

Enregistrer un commentaire