Skip to content

fix: refactor switch cases and reduce method complexity#21

Open
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260601-010121-1d719375
Open

fix: refactor switch cases and reduce method complexity#21
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260601-010121-1d719375

Conversation

@sonarqube-agent

Copy link
Copy Markdown

This PR was automatically created by the Remediation Agent's Scheduled backlog remediation feature.

This PR fixes 5 SonarQube issues by merging switch case labels using comma-separated syntax and refactoring the JsonTypeFactory.findSpecifiedType method to reduce cognitive complexity, LOC, and nesting levels. These changes improve code maintainability and align the codebase with modern Java best practices.

View Project in SonarCloud


Fixed Issues

java:S6208 - Merge the previous cases into this one using comma-separated label. • INFOView issue

Location: jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/EnumTypeDefinition.java:64

Why is this an issue?

In Java 14 there is a new way to write cases in Switch Statement and Expression when the same action should be performed for different cases. Instead of declaring multiples branches with the same action, you can combine all of them in a single case group, separated with commas. It will result in a more concise code and improved readability.

What changed

This hunk merges the separate case FLOAT: and case DOUBLE: labels into a single comma-separated case FLOAT, DOUBLE: label. This directly fixes the code smell where multiple switch cases with the same action should be combined into a single comma-separated case group, as reported at line 64 of EnumTypeDefinition.java.

--- a/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/EnumTypeDefinition.java
+++ b/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/EnumTypeDefinition.java
@@ -63,2 +63,1 @@ public class EnumTypeDefinition extends SimpleTypeDefinition {
-          case FLOAT:
-          case DOUBLE:
+          case FLOAT, DOUBLE:
java:S6208 - Merge the previous cases into this one using comma-separated label. • INFOView issue

Location: jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonPrimitiveType.java:67

Why is this an issue?

In Java 14 there is a new way to write cases in Switch Statement and Expression when the same action should be performed for different cases. Instead of declaring multiples branches with the same action, you can combine all of them in a single case group, separated with commas. It will result in a more concise code and improved readability.

What changed

This hunk merges the separate case INT:, case LONG:, case SHORT:, and case BYTE: labels into a single comma-separated case INT, LONG, SHORT, BYTE: label. This directly fixes the code smell about multiple switch cases that perform the same action needing to be combined into a single comma-separated case group, as recommended for Java 14+.

--- a/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonPrimitiveType.java
+++ b/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonPrimitiveType.java
@@ -64,4 +64,1 @@ public class JsonPrimitiveType implements JsonType {
-      case INT:
-      case LONG:
-      case SHORT:
-      case BYTE: //todo: verify 'byte' serialization?
+      case INT, LONG, SHORT, BYTE: //todo: verify 'byte' serialization?
java:S6208 - Merge the previous cases into this one using comma-separated label. • INFOView issue

Location: jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java:70

Why is this an issue?

In Java 14 there is a new way to write cases in Switch Statement and Expression when the same action should be performed for different cases. Instead of declaring multiples branches with the same action, you can combine all of them in a single case group, separated with commas. It will result in a more concise code and improved readability.

What changed

This hunk removes the inline switch statement (which had separate fall-through cases like case NUMBER: / case NUMBER_FLOAT: and case STRING: / case SCALAR:) from the main method and moves the adapted-type check up. This reduces the LOC, complexity, nesting, and variable count of the Brain Method. It also eliminates the old-style fall-through cases that triggered the warnings about merging cases using comma-separated labels.

--- a/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java
+++ b/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java
@@ -62,22 +57,3 @@ public class JsonTypeFactory {
-      JsonFormat format = accessor.getAnnotation(JsonFormat.class);
-      if (format != null) {
-        switch (format.shape()) {
-          case ARRAY:
-            return KnownJsonType.ARRAY;
-          case BOOLEAN:
-            return KnownJsonType.BOOLEAN;
-          case NUMBER:
-          case NUMBER_FLOAT:
-            return KnownJsonType.NUMBER;
-          case NUMBER_INT:
-            return KnownJsonType.WHOLE_NUMBER;
-          case OBJECT:
-            return KnownJsonType.OBJECT;
-          case STRING:
-          case SCALAR:
-            return KnownJsonType.STRING;
-          case ANY:
-          default:
-            //fall through...
-        }
-      }
+    if (adaptable.isAdapted()) {
+      return getJsonType(adaptable.getAdapterType().getAdaptingType(), context);
+    }
java:S6208 - Merge the previous cases into this one using comma-separated label. • INFOView issue

Location: jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java:77

Why is this an issue?

In Java 14 there is a new way to write cases in Switch Statement and Expression when the same action should be performed for different cases. Instead of declaring multiples branches with the same action, you can combine all of them in a single case group, separated with commas. It will result in a more concise code and improved readability.

What changed

This hunk removes the inline switch statement (which had separate fall-through cases like case NUMBER: / case NUMBER_FLOAT: and case STRING: / case SCALAR:) from the main method and moves the adapted-type check up. This reduces the LOC, complexity, nesting, and variable count of the Brain Method. It also eliminates the old-style fall-through cases that triggered the warnings about merging cases using comma-separated labels.

--- a/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java
+++ b/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java
@@ -62,22 +57,3 @@ public class JsonTypeFactory {
-      JsonFormat format = accessor.getAnnotation(JsonFormat.class);
-      if (format != null) {
-        switch (format.shape()) {
-          case ARRAY:
-            return KnownJsonType.ARRAY;
-          case BOOLEAN:
-            return KnownJsonType.BOOLEAN;
-          case NUMBER:
-          case NUMBER_FLOAT:
-            return KnownJsonType.NUMBER;
-          case NUMBER_INT:
-            return KnownJsonType.WHOLE_NUMBER;
-          case OBJECT:
-            return KnownJsonType.OBJECT;
-          case STRING:
-          case SCALAR:
-            return KnownJsonType.STRING;
-          case ANY:
-          default:
-            //fall through...
-        }
-      }
+    if (adaptable.isAdapted()) {
+      return getJsonType(adaptable.getAdapterType().getAdaptingType(), context);
+    }
java:S6541 - A "Brain Method" was detected. Refactor it to reduce at least one of the following metrics: LOC from 77 to 64, Complexity from 30 to 14, Nesting Level from 7 to 2, Number of Variables from 19 to 6. • INFOView issue

Location: jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java:49

Why is this an issue?

This issue is raised when Sonar considers that a method is a 'Brain Method'.
A Brain Method is a method that tends to centralize its owner’s class logic and generally performs too many operations. This can include checking too many conditions, using lots of variables, and ultimately making it difficult to understand, maintain and reuse.
It is characterized by high LOC number, high cyclomatic and cognitive complexity, and a large number of variables being used.

What changed

This hunk begins the refactoring of the findSpecifiedType method (a Brain Method) by extracting the TypeHint logic into a separate helper method findSpecifiedTypeForAccessor. It also uses pattern matching for instanceof, reducing code lines and variables in the main method.

--- a/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java
+++ b/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java
@@ -50,10 +50,4 @@ public class JsonTypeFactory {
-    JsonType jsonType = null;
-
-    if (adaptable instanceof Accessor) {
-      Accessor accessor = (Accessor) adaptable;
-      TypeHint typeHint = accessor.getAnnotation(TypeHint.class);
-      if (typeHint != null) {
-        TypeMirror hint = TypeHintUtils.getTypeHint(typeHint, context.getContext().getProcessingEnvironment(), null);
-        if (hint != null) {
-          return getJsonType(hint, context);
-        }
+    if (adaptable instanceof Accessor accessor) {
+      JsonType accessorType = findSpecifiedTypeForAccessor(accessor, context);
+      if (accessorType != null) {
+        return accessorType;

Have a suggestion or found an issue? Share your feedback here.


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZZmth6t2HDYqP_Xynw9 for java:S6208 rule
- AZZmth7p2HDYqP_XynyL for java:S6208 rule
- AZZmth6y2HDYqP_XynxQ for java:S6541 rule
- AZZmth6y2HDYqP_XynxJ for java:S6208 rule
- AZZmth6y2HDYqP_XynxK for java:S6208 rule

Generated by SonarQube Agent (task: f000afe8-2c7a-404f-b2ec-e9f1820c8ab0)
@sonarqubecloud

sonarqubecloud Bot commented Jun 1, 2026

Copy link
Copy Markdown

SonarQube reviewer guide

Review in SonarQube

Summary: Refactor switch statements to use modern Java syntax (pattern matching and switch expressions) and extract complex logic into helper methods for improved readability.

Review Focus: The major refactoring in JsonTypeFactory.java extracts a 98-line method into four focused helper methods. Verify that the logic flow and return paths remain equivalent, especially around null handling and the fallthrough behavior in the switch statement. Pay particular attention to the new findTypeFromJsonFormat method which uses a switch expression instead of traditional switch-case.

Start review at: jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/types/JsonTypeFactory.java. This file contains the most substantial changes with significant refactoring that impacts the core type detection logic. The extraction of helper methods should simplify maintenance, but requires careful verification that behavior is preserved, particularly around control flow and null returns.

💬 Please send your feedback

Quality Gate Passed Quality Gate passed

Issues
6 New issues
0 Accepted issues
0 New dependency risks

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant