From: Marcono1234 Date: Tue, 23 Jan 2024 23:59:14 +0000 (+0100) Subject: NO-JIRA Make Gson usage more type-safe X-Git-Tag: 10.4.0.87286~96 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=29994d2a4ffa03fa205d7ed674cc29cc2efb5edb;p=sonarqube.git NO-JIRA Make Gson usage more type-safe Uses `Gson.fromJson(..., TypeToken)` instead of `fromJson(..., Type)` Also replaces some usage of the deprecated `JsonParser()` constructor. --- diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationClientImpl.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationClientImpl.java index 8e855dbd19a..b730be0f32d 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationClientImpl.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationClientImpl.java @@ -73,9 +73,12 @@ public class GithubApplicationClientImpl implements GithubApplicationClient { protected static final String WRITE_PERMISSION_NAME = "write"; protected static final String READ_PERMISSION_NAME = "read"; protected static final String FAILED_TO_REQUEST_BEGIN_MSG = "Failed to request "; - private static final Type REPOSITORY_TEAM_LIST_TYPE = TypeToken.getParameterized(List.class, GsonRepositoryTeam.class).getType(); - private static final Type REPOSITORY_COLLABORATORS_LIST_TYPE = TypeToken.getParameterized(List.class, GsonRepositoryCollaborator.class).getType(); - private static final Type ORGANIZATION_LIST_TYPE = TypeToken.getParameterized(List.class, GithubBinding.GsonInstallation.class).getType(); + private static final TypeToken> REPOSITORY_TEAM_LIST_TYPE = new TypeToken<>() { + }; + private static final TypeToken> REPOSITORY_COLLABORATORS_LIST_TYPE = new TypeToken<>() { + }; + private static final TypeToken> ORGANIZATION_LIST_TYPE = new TypeToken<>() { + }; protected final GithubApplicationHttpClient githubApplicationHttpClient; protected final GithubAppSecurity appSecurity; private final GitHubSettings gitHubSettings; diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/GitlabApplicationClient.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/GitlabApplicationClient.java index 661cb439d55..4db9851c99d 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/GitlabApplicationClient.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/GitlabApplicationClient.java @@ -58,8 +58,10 @@ import static java.nio.charset.StandardCharsets.UTF_8; public class GitlabApplicationClient { private static final Logger LOG = LoggerFactory.getLogger(GitlabApplicationClient.class); private static final Gson GSON = new Gson(); - private static final Type GITLAB_GROUP = TypeToken.getParameterized(List.class, GsonGroup.class).getType(); - private static final Type GITLAB_USER = TypeToken.getParameterized(List.class, GsonUser.class).getType(); + private static final TypeToken> GITLAB_GROUP = new TypeToken<>() { + }; + private static final TypeToken> GITLAB_USER = new TypeToken<>() { + }; protected static final String PRIVATE_TOKEN = "Private-Token"; private static final String GITLAB_GROUPS_MEMBERS_ENDPOINT = "/groups/%s/members"; diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/Project.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/Project.java index f2721a47be3..c9bd8e664df 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/Project.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/gitlab/Project.java @@ -75,7 +75,7 @@ public class Project { public static List parseJsonArray(String json) { Gson gson = new Gson(); return gson.fromJson(json, new TypeToken>() { - }.getType()); + }); } public long getId() { diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/GenericPaginatedHttpClientImplTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/GenericPaginatedHttpClientImplTest.java index 1d3785333ef..54ba48fa24d 100644 --- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/GenericPaginatedHttpClientImplTest.java +++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/GenericPaginatedHttpClientImplTest.java @@ -54,7 +54,8 @@ public class GenericPaginatedHttpClientImplTest { private static final String ENDPOINT = "/test-endpoint"; - private static final Type STRING_LIST_TYPE = TypeToken.getParameterized(List.class, String.class).getType(); + private static final TypeToken> STRING_LIST_TYPE = new TypeToken<>() { + }; private Gson gson = new Gson(); diff --git a/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GsonEmail.java b/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GsonEmail.java index f8f0076bdbd..57f73236311 100644 --- a/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GsonEmail.java +++ b/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GsonEmail.java @@ -58,9 +58,8 @@ public class GsonEmail { } public static List parse(String json) { - Type collectionType = new TypeToken>() { - }.getType(); Gson gson = new Gson(); - return gson.fromJson(json, collectionType); + return gson.fromJson(json, new TypeToken>() { + }); } } diff --git a/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GsonTeam.java b/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GsonTeam.java index 84137f98499..cd5bd25a938 100644 --- a/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GsonTeam.java +++ b/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GsonTeam.java @@ -52,10 +52,9 @@ public class GsonTeam { } public static List parse(String json) { - Type collectionType = new TypeToken>() { - }.getType(); Gson gson = new Gson(); - return gson.fromJson(json, collectionType); + return gson.fromJson(json, new TypeToken>() { + }); } public static class GsonOrganization { diff --git a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GsonGroup.java b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GsonGroup.java index be8a5810334..aaf940d1475 100644 --- a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GsonGroup.java +++ b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GsonGroup.java @@ -55,9 +55,9 @@ public class GsonGroup { } static List parse(String json) { - Type collectionType = new TypeToken>() {}.getType(); Gson gson = new Gson(); - return gson.fromJson(json, collectionType); + return gson.fromJson(json, new TypeToken>() { + }); } } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/qualityprofile/QPMeasureData.java b/server/sonar-server-common/src/main/java/org/sonar/server/qualityprofile/QPMeasureData.java index d2d8ea1a62c..4b3866d3ee6 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/qualityprofile/QPMeasureData.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/qualityprofile/QPMeasureData.java @@ -48,7 +48,7 @@ public class QPMeasureData { } public static QPMeasureData fromJson(String json) { - return new QPMeasureData(StreamSupport.stream(new JsonParser().parse(json).getAsJsonArray().spliterator(), false) + return new QPMeasureData(StreamSupport.stream(JsonParser.parseString(json).getAsJsonArray().spliterator(), false) .map(jsonElement -> { JsonObject jsonProfile = jsonElement.getAsJsonObject(); return new QualityProfile( diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/OAuth2AuthenticationParametersImpl.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/OAuth2AuthenticationParametersImpl.java index a02b3e3c364..d65e483a221 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/OAuth2AuthenticationParametersImpl.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/authentication/OAuth2AuthenticationParametersImpl.java @@ -20,11 +20,10 @@ package org.sonar.server.authentication; import com.google.common.base.Strings; -import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; import java.io.UnsupportedEncodingException; -import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -53,8 +52,8 @@ public class OAuth2AuthenticationParametersImpl implements OAuth2AuthenticationP */ private static final String RETURN_TO_PARAMETER = "return_to"; - private static final Type JSON_MAP_TYPE = new TypeToken>() { - }.getType(); + private static final TypeToken> JSON_MAP_TYPE = new TypeToken<>() { + }; @Override public void init(HttpRequest request, HttpResponse response) { diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ws/TreeActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ws/TreeActionIT.java index b13f6d31c10..cbd822d74ac 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ws/TreeActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ws/TreeActionIT.java @@ -565,8 +565,7 @@ public class TreeActionIT { db.components().insertSnapshot(projectData.getMainBranchComponent()); Date now = new Date(); - JsonParser jsonParser = new JsonParser(); - JsonElement jsonTree = jsonParser.parseString(IOUtils.toString(getClass().getResource("tree-example.json"), UTF_8)); + JsonElement jsonTree = JsonParser.parseString(IOUtils.toString(getClass().getResource("tree-example.json"), UTF_8)); JsonArray components = jsonTree.getAsJsonObject().getAsJsonArray("components"); for (int i = 0; i < components.size(); i++) { JsonElement componentAsJsonElement = components.get(i); diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/issue/ws/SearchActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/issue/ws/SearchActionIT.java index 64359dda0dd..9060e79de8f 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/issue/ws/SearchActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/issue/ws/SearchActionIT.java @@ -1358,7 +1358,7 @@ public class SearchActionIT { .setParam("asc", "false") .execute(); - JsonElement parse = new JsonParser().parse(response.getInput()); + JsonElement parse = JsonParser.parseString(response.getInput()); assertThat(parse.getAsJsonObject().get("issues").getAsJsonArray()) .extracting(o -> o.getAsJsonObject().get("key").getAsString()) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SetAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SetAction.java index 0a1a8c56cf2..769d44b1671 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SetAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SetAction.java @@ -24,7 +24,6 @@ import com.google.common.collect.ListMultimap; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; -import java.lang.reflect.Type; import java.util.Collections; import java.util.List; import java.util.Map; @@ -70,6 +69,8 @@ public class SetAction implements SettingsWsAction { private static final Collector COMMA_JOINER = Collectors.joining(","); private static final String MSG_NO_EMPTY_VALUE = "A non empty value must be provided"; private static final int VALUE_MAXIMUM_LENGTH = 4000; + private static final TypeToken> MAP_TYPE_TOKEN = new TypeToken<>() { + }; private final PropertyDefinitions propertyDefinitions; private final DbClient dbClient; @@ -305,11 +306,9 @@ public class SetAction implements SettingsWsAction { } private static Map readOneFieldValues(String json, String key) { - Type type = new TypeToken>() { - }.getType(); Gson gson = GsonHelper.create(); try { - return gson.fromJson(json, type); + return gson.fromJson(json, MAP_TYPE_TOKEN); } catch (JsonSyntaxException e) { throw BadRequestException.create(format("JSON '%s' does not respect expected format for setting '%s'. Ex: {\"field1\":\"value1\", \"field2\":\"value2\"}", json, key)); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java index 64f32c7fbee..7c536195a8c 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java @@ -189,8 +189,7 @@ public class DefaultScannerWsClient implements ScannerWsClient { @CheckForNull private static String tryParseAsJsonError(String responseContent) { try { - JsonParser parser = new JsonParser(); - JsonObject obj = parser.parse(responseContent).getAsJsonObject(); + JsonObject obj = JsonParser.parseString(responseContent).getAsJsonObject(); JsonArray errors = obj.getAsJsonArray("errors"); List errorMessages = new ArrayList<>(); for (JsonElement e : errors) { diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/JsonAssert.java b/sonar-testing-harness/src/main/java/org/sonar/test/JsonAssert.java index 299bbc9ceeb..a24ca30b8e0 100644 --- a/sonar-testing-harness/src/main/java/org/sonar/test/JsonAssert.java +++ b/sonar-testing-harness/src/main/java/org/sonar/test/JsonAssert.java @@ -123,7 +123,7 @@ public class JsonAssert { } private static String pretty(String json) { - JsonElement gson = new JsonParser().parse(json); + JsonElement gson = JsonParser.parseString(json); return new GsonBuilder().setPrettyPrinting().serializeNulls().create().toJson(gson); } }