A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/5030 below:

[java] SwitchDensity false positive with pattern matching · Issue #5030 · pmd/pmd · GitHub

Affects PMD Version: 7.2.0

Rule: SwitchDensity

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/pmd/pmd_rules_java_design.html#switchdensity

Description:

Given the following code, there seems to be ~4 statements per switch label but the rule seems to think the density is greater than 10.

Code Sample demonstrating the issue:

private void handleException(RequestScope scope, Throwable exception)
{
	ITransaction sentryTransaction = scope.getSentryTransaction();
	if (exception.getClass() != RequestFailedException.class)
	{
		// No need to report RequestFailedException as it's just used to unwind the stack
		sentryTransaction.setThrowable(exception);
	}
	switch (exception)
	{
		case JsonProcessingException e ->
		{
			log.debug("", e);
			responseToJacksonException(scope, exception);
			sentryTransaction.setStatus(SpanStatus.fromHttpStatusCode(scope.getResponse().getStatus()));
		}
		case RequestFailedException _ ->
		{
			// The exception is just used to unroll the stack after sending a response. No need to log it.
			sentryTransaction.setStatus(SpanStatus.fromHttpStatusCode(scope.getResponse().getStatus()));
		}
		case DataAccessException e ->
		{
			log.error("", e);
			scope.getRequests().sendText(INTERNAL_SERVER_ERROR_500, Exceptions.getDeepestMessage(e),
				HttpFields.EMPTY);
			sentryTransaction.setStatus(SpanStatus.fromHttpStatusCode(scope.getResponse().getStatus()));
		}
		case RuntimeException e ->
		{
			log.error("", e);
			scope.getRequests().sendText(BAD_REQUEST_400, Exceptions.getDeepestMessage(e), HttpFields.EMPTY);
			sentryTransaction.setStatus(SpanStatus.fromHttpStatusCode(scope.getResponse().getStatus()));
		}
		case Error e ->
		{
			scope.getRequests().sendText(INTERNAL_SERVER_ERROR_500, Exceptions.getDeepestMessage(e),
				HttpFields.EMPTY);
			sentryTransaction.setStatus(SpanStatus.fromHttpStatusCode(scope.getResponse().getStatus()));
			throw e;
		}
		case Throwable t ->
		{
			scope.getRequests().sendText(INTERNAL_SERVER_ERROR_500, Exceptions.getDeepestMessage(t),
				HttpFields.EMPTY);
			sentryTransaction.setStatus(SpanStatus.fromHttpStatusCode(scope.getResponse().getStatus()));
			throw WrappedCheckedException.wrap(t);
		}
	}
}

Running PMD through: maven-pmd-plugin 3.22.0


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4