From c72809b66b1b3b1e9c3d0b3cbf40f74d972ac5be Mon Sep 17 00:00:00 2001 From: novalisdenahi Date: Thu, 22 Oct 2026 17:27:59 +0200 Subject: [PATCH] SonarQube fixes --- src/main/java/com/configcat/Config.java | 5 +++- .../java/com/configcat/ConfigCatClient.java | 18 ++++++++----- .../java/com/configcat/ConfigCatHooks.java | 2 ++ .../java/com/configcat/ConfigFetcher.java | 16 ++++++------ src/main/java/com/configcat/Entry.java | 8 +++--- .../com/configcat/EvaluationErrorCode.java | 2 +- .../java/com/configcat/NullConfigCache.java | 1 + .../com/configcat/OverrideDataSource.java | 1 + src/main/java/com/configcat/PollingModes.java | 4 +++ .../java/com/configcat/RefreshErrorCode.java | 2 +- .../com/configcat/UserAttributeConverter.java | 4 +++ .../com/configcat/DataGovernanceTest.java | 26 +++++++++---------- 12 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/configcat/Config.java b/src/main/java/com/configcat/Config.java index 6d09e63a..c4a0ca9b 100644 --- a/src/main/java/com/configcat/Config.java +++ b/src/main/java/com/configcat/Config.java @@ -37,7 +37,10 @@ public Segment[] getSegments() { public Map getEntries() { // NOTE: Deserializing a JSON like '{ "f": null }' overwrites entries with null. // However, we want to treat null as an empty map in that case too. - return entries != null ? entries : (entries = new HashMap<>()); + if(entries == null) { + entries = new HashMap<>(); + } + return entries; } boolean isEmpty() { diff --git a/src/main/java/com/configcat/ConfigCatClient.java b/src/main/java/com/configcat/ConfigCatClient.java index d3d6c929..a6c3749a 100644 --- a/src/main/java/com/configcat/ConfigCatClient.java +++ b/src/main/java/com/configcat/ConfigCatClient.java @@ -46,11 +46,7 @@ private ConfigCatClient(String sdkKey, Options options) { createHttpClient(options.httpOptions()), this.logger, sdkKey, - !options.isBaseURLCustom() - ? options.dataGovernance == DataGovernance.GLOBAL - ? BASE_URL_GLOBAL - : BASE_URL_EU - : options.baseUrl, + determineBaseUrl(options.baseUrl, options.dataGovernance, options.isBaseURLCustom()), options.isBaseURLCustom(), options.pollingMode.getPollingIdentifier()); @@ -288,8 +284,9 @@ public CompletableFuture>> getAllValueDetailsAsyn Map settings = settingResult.settings(); List> result = new ArrayList<>(); - for (String key : settings.keySet()) { - Setting setting = settings.get(key); + for (Map.Entry entry : settings.entrySet()) { + String key = entry.getKey(); + Setting setting = entry.getValue(); EvaluationDetails evaluationDetails = this.evaluateObject(this.classBySettingType(setting.getType()), setting, key, effectiveUser, settingResult.fetchTime(), settings); @@ -725,6 +722,13 @@ private static boolean isValidKey(final String sdkKey, final boolean isCustomBas return splitSDKKey.length == 3 && splitSDKKey[0].equals(Constants.SDK_KEY_PREFIX) && splitSDKKey[1].length() == Constants.SDK_KEY_SECTION_LENGTH && splitSDKKey[2].length() == Constants.SDK_KEY_SECTION_LENGTH; } + private static String determineBaseUrl(String baseUrl, DataGovernance dataGovernance, boolean isBaseUrlCustom) { + if (isBaseUrlCustom) { + return baseUrl; + } + return dataGovernance == DataGovernance.GLOBAL ? BASE_URL_GLOBAL : BASE_URL_EU; + } + private EvaluationDetails evaluateObject(Class classOfT, Setting setting, String key, User effectiveUser, Long fetchTime, Map settings) { EvaluationResult evaluationResult = this.rolloutEvaluator.evaluate(setting, key, effectiveUser, settings, new EvaluateLogger(this.clientLogLevel)); EvaluationDetails details = new EvaluationDetails<>( diff --git a/src/main/java/com/configcat/ConfigCatHooks.java b/src/main/java/com/configcat/ConfigCatHooks.java index 23580a63..20e64dc6 100644 --- a/src/main/java/com/configcat/ConfigCatHooks.java +++ b/src/main/java/com/configcat/ConfigCatHooks.java @@ -46,6 +46,8 @@ public void addOnClientReady(Consumer callback) { * onReady event fires when the auto polling's maxInitWaitTimeInSeconds is reached. * * @param callback the method to call when the event fires. + * + * @deprecated Use {@link #addOnClientReady(Consumer)} instead. */ @Deprecated public void addOnClientReady(Runnable callback) { diff --git a/src/main/java/com/configcat/ConfigFetcher.java b/src/main/java/com/configcat/ConfigFetcher.java index 3cffd811..1a0b6fe9 100644 --- a/src/main/java/com/configcat/ConfigFetcher.java +++ b/src/main/java/com/configcat/ConfigFetcher.java @@ -32,9 +32,9 @@ class ConfigFetcher implements Closeable { private String url; enum RedirectMode { - NoRedirect, - ShouldRedirect, - ForceRedirect + NO_REDIRECT, + SHOULD_REDIRECT, + FORCE_REDIRECT } ConfigFetcher(OkHttpClient httpClient, @@ -75,16 +75,16 @@ private CompletableFuture executeFetchAsync(int executionCount, S int redirect = config.getPreferences().getRedirect(); // we have a custom url set, and we didn't get a forced redirect - if (this.urlIsCustom && redirect != RedirectMode.ForceRedirect.ordinal()) { + if (this.urlIsCustom && redirect != RedirectMode.FORCE_REDIRECT.ordinal()) { return CompletableFuture.completedFuture(fetchResponse); } this.url = newUrl; - if (redirect == RedirectMode.NoRedirect.ordinal()) { // no redirect + if (redirect == RedirectMode.NO_REDIRECT.ordinal()) { // no redirect return CompletableFuture.completedFuture(fetchResponse); } else { // redirect - if (redirect == RedirectMode.ShouldRedirect.ordinal()) { + if (redirect == RedirectMode.SHOULD_REDIRECT.ordinal()) { this.logger.warn(3002, ConfigCatLogMessages.DATA_GOVERNANCE_IS_OUT_OF_SYNC_WARN); } @@ -272,14 +272,14 @@ public void close() throws IOException { } Request getRequest(String etag) { - String url = this.url + "/configuration-files/" + this.sdkKey + "/" + Constants.CONFIG_JSON_NAME; + String requestUrl = this.url + "/configuration-files/" + this.sdkKey + "/" + Constants.CONFIG_JSON_NAME; Request.Builder builder = new Request.Builder() .addHeader("X-ConfigCat-UserAgent", "ConfigCat-Java/" + this.mode + "-" + Constants.VERSION); if (etag != null && !etag.isEmpty()) builder.addHeader("If-None-Match", etag); - return builder.url(url).build(); + return builder.url(requestUrl).build(); } /** diff --git a/src/main/java/com/configcat/Entry.java b/src/main/java/com/configcat/Entry.java index d9530ad0..d3aa208f 100644 --- a/src/main/java/com/configcat/Entry.java +++ b/src/main/java/com/configcat/Entry.java @@ -23,16 +23,16 @@ public String getCacheString() { } public Entry withFetchTime(long fetchTime) { - String cacheString = getCacheString(); - int fetchTimeIndex = cacheString.indexOf("\n"); + String entryCacheString = getCacheString(); + int fetchTimeIndex = entryCacheString.indexOf("\n"); if (fetchTimeIndex == -1) { return this; } - int eTagIndex = cacheString.indexOf("\n", fetchTimeIndex + 1); + int eTagIndex = entryCacheString.indexOf("\n", fetchTimeIndex + 1); if (eTagIndex == -1) { return this; } - String configJson = cacheString.substring(eTagIndex + 1); + String configJson = entryCacheString.substring(eTagIndex + 1); return new Entry(getConfig(), getETag(), configJson, fetchTime); } diff --git a/src/main/java/com/configcat/EvaluationErrorCode.java b/src/main/java/com/configcat/EvaluationErrorCode.java index 69ab0d5b..7f535255 100644 --- a/src/main/java/com/configcat/EvaluationErrorCode.java +++ b/src/main/java/com/configcat/EvaluationErrorCode.java @@ -32,7 +32,7 @@ public enum EvaluationErrorCode implements ErrorCode { */ SETTING_KEY_MISSING(1001); - public final int code; + private final int code; EvaluationErrorCode(int code) { this.code = code; diff --git a/src/main/java/com/configcat/NullConfigCache.java b/src/main/java/com/configcat/NullConfigCache.java index 4f780dee..30ab0b81 100644 --- a/src/main/java/com/configcat/NullConfigCache.java +++ b/src/main/java/com/configcat/NullConfigCache.java @@ -12,5 +12,6 @@ protected String read(String key) { @Override protected void write(String key, String value) { + // Null cache does not store anything. } } diff --git a/src/main/java/com/configcat/OverrideDataSource.java b/src/main/java/com/configcat/OverrideDataSource.java index fece330c..9e1ab787 100644 --- a/src/main/java/com/configcat/OverrideDataSource.java +++ b/src/main/java/com/configcat/OverrideDataSource.java @@ -12,6 +12,7 @@ public Map getLocalConfiguration() { @Override public void close() throws IOException { + // No resources to close in this implementation. } } diff --git a/src/main/java/com/configcat/PollingModes.java b/src/main/java/com/configcat/PollingModes.java index ab5a58c3..51691642 100644 --- a/src/main/java/com/configcat/PollingModes.java +++ b/src/main/java/com/configcat/PollingModes.java @@ -5,6 +5,10 @@ */ public final class PollingModes { + private PollingModes() { + // Private constructor to prevent instantiation. + } + private static final int DEFAULT_AUTO_POLL_INTERVAL_IN_SECONDS = 60; private static final int DEFAULT_MAX_INIT_WAIT_TIME_IN_SECONDS = 5; private static final int DEFAULT_CACHE_REFRESH_INTERVAL_IN_SECONDS = 60; diff --git a/src/main/java/com/configcat/RefreshErrorCode.java b/src/main/java/com/configcat/RefreshErrorCode.java index e59e749a..ffa200fe 100644 --- a/src/main/java/com/configcat/RefreshErrorCode.java +++ b/src/main/java/com/configcat/RefreshErrorCode.java @@ -41,7 +41,7 @@ public enum RefreshErrorCode implements ErrorCode { /** Client initialization could not complete within `maxInitWaitTimeSeconds`. */ CLIENT_INIT_TIMED_OUT(4200); - public final int code; + private final int code; RefreshErrorCode(int code) { this.code = code; diff --git a/src/main/java/com/configcat/UserAttributeConverter.java b/src/main/java/com/configcat/UserAttributeConverter.java index e6f44136..3e5b7ba5 100644 --- a/src/main/java/com/configcat/UserAttributeConverter.java +++ b/src/main/java/com/configcat/UserAttributeConverter.java @@ -9,6 +9,10 @@ final class UserAttributeConverter { + private UserAttributeConverter() { + // Private constructor to prevent instantiation. + } + public static String userAttributeToString(Object userAttribute) { if (userAttribute == null) { return null; diff --git a/src/test/java/com/configcat/DataGovernanceTest.java b/src/test/java/com/configcat/DataGovernanceTest.java index 024a5ba8..a31d30c0 100644 --- a/src/test/java/com/configcat/DataGovernanceTest.java +++ b/src/test/java/com/configcat/DataGovernanceTest.java @@ -12,7 +12,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class DataGovernanceTest { - private static final String JsonTemplate = "{ p: { u: \"%s\", r: %d, s: \"test-salt\" }, f: {} }"; + private static final String JSON_TEMPLATE = "{ p: { u: \"%s\", r: %d, s: \"test-salt\" }, f: {} }"; private final ConfigCatLogger logger = new ConfigCatLogger(LoggerFactory.getLogger(DataGovernanceTest.class)); @Test @@ -21,7 +21,7 @@ public void shouldStayOnGivenUrl() throws IOException, ExecutionException, Inter String url = server.url("/").toString(); ConfigFetcher fetcher = this.createFetcher(url, false); // Arrange - String body = String.format(JsonTemplate, url, 0); + String body = String.format(JSON_TEMPLATE, url, 0); server.enqueue(new MockResponse().setResponseCode(200).setBody(body)); // Act @@ -42,7 +42,7 @@ public void shouldStayOnSameUrl() throws IOException, ExecutionException, Interr String url = server.url("/").toString(); ConfigFetcher fetcher = this.createFetcher(url, false); // Arrange - String body = String.format(JsonTemplate, url, 1); + String body = String.format(JSON_TEMPLATE, url, 1); server.enqueue(new MockResponse().setResponseCode(200).setBody(body)); // Act @@ -64,7 +64,7 @@ public void shouldStayOnSameUrlEvenWithForce() throws IOException, ExecutionExce ConfigFetcher fetcher = this.createFetcher(url, false); // Arrange - String body = String.format(JsonTemplate, url, 2); + String body = String.format(JSON_TEMPLATE, url, 2); server.enqueue(new MockResponse().setResponseCode(200).setBody(body)); // Act @@ -88,10 +88,10 @@ public void shouldRedirectToAnotherServer() throws IOException, ExecutionExcepti ConfigFetcher fetcher = this.createFetcher(firstServerUrl, false); // Arrange - String firstBody = String.format(JsonTemplate, secondServerUrl, 1); + String firstBody = String.format(JSON_TEMPLATE, secondServerUrl, 1); firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody)); - String secondBody = String.format(JsonTemplate, secondServerUrl, 0); + String secondBody = String.format(JSON_TEMPLATE, secondServerUrl, 0); secondServer.enqueue(new MockResponse().setResponseCode(200).setBody(secondBody)); // Act @@ -118,10 +118,10 @@ public void shouldRedirectToAnotherServerWhenForced() throws IOException, Execut ConfigFetcher fetcher = this.createFetcher(firstServerUrl, false); // Arrange - String firstBody = String.format(JsonTemplate, secondServerUrl, 2); + String firstBody = String.format(JSON_TEMPLATE, secondServerUrl, 2); firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody)); - String secondBody = String.format(JsonTemplate, secondServerUrl, 0); + String secondBody = String.format(JSON_TEMPLATE, secondServerUrl, 0); secondServer.enqueue(new MockResponse().setResponseCode(200).setBody(secondBody)); // Act @@ -148,11 +148,11 @@ public void shouldBreakTheRedirectLoop() throws IOException, ExecutionException, ConfigFetcher fetcher = this.createFetcher(firstServerUrl, false); // Arrange - String firstBody = String.format(JsonTemplate, secondServerUrl, 1); + String firstBody = String.format(JSON_TEMPLATE, secondServerUrl, 1); firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody)); firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody)); - String secondBody = String.format(JsonTemplate, firstServerUrl, 1); + String secondBody = String.format(JSON_TEMPLATE, firstServerUrl, 1); secondServer.enqueue(new MockResponse().setResponseCode(200).setBody(secondBody)); // Act @@ -179,7 +179,7 @@ public void shouldRespectCustomUrlWhenNotForced() throws IOException, ExecutionE ConfigFetcher fetcher = this.createFetcher(firstServerUrl, true); // Arrange - String firstBody = String.format(JsonTemplate, secondServerUrl, 1); + String firstBody = String.format(JSON_TEMPLATE, secondServerUrl, 1); firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody)); firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody)); @@ -216,10 +216,10 @@ public void shouldNotRespectCustomUrlWhenForced() throws IOException, ExecutionE ConfigFetcher fetcher = this.createFetcher(firstServerUrl, true); // Arrange - String firstBody = String.format(JsonTemplate, secondServerUrl, 2); + String firstBody = String.format(JSON_TEMPLATE, secondServerUrl, 2); firstServer.enqueue(new MockResponse().setResponseCode(200).setBody(firstBody)); - String secondBody = String.format(JsonTemplate, secondServerUrl, 0); + String secondBody = String.format(JSON_TEMPLATE, secondServerUrl, 0); secondServer.enqueue(new MockResponse().setResponseCode(200).setBody(secondBody)); // Act