-
Notifications
You must be signed in to change notification settings - Fork 724
SONARJAVA-7095 Implement JSON serialization for the common Spring context gathering model #6283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
...nd/src/main/java/org/sonar/java/serialization/SpringContextGatheringModelTypeAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.serialization; | ||
|
|
||
| import com.google.gson.TypeAdapter; | ||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonWriter; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import org.sonar.java.model.springcontext.BeanDefinitionHolder; | ||
| import org.sonar.java.model.springcontext.SpringContextGatheringModel; | ||
|
|
||
| import static org.sonar.java.serialization.JsonUtils.BEANS; | ||
| import static org.sonar.java.serialization.JsonUtils.FILES_DATA; | ||
| import static org.sonar.java.serialization.JsonUtils.PACKAGES; | ||
| import static org.sonar.java.serialization.JsonUtils.missingProperty; | ||
| import static org.sonar.java.serialization.JsonUtils.readStrings; | ||
| import static org.sonar.java.serialization.JsonUtils.required; | ||
| import static org.sonar.java.serialization.JsonUtils.writeStrings; | ||
|
|
||
| /** | ||
| * JSON representation of the Spring context data collected by module and file. Null properties in bean definitions | ||
| * remain explicit so the bean adapter can read them back with Gson's default settings. | ||
| */ | ||
| public final class SpringContextGatheringModelTypeAdapter extends TypeAdapter<SpringContextGatheringModel> { | ||
|
|
||
| private static final SpringContextGatheringModelTypeAdapter INSTANCE = new SpringContextGatheringModelTypeAdapter(); | ||
|
|
||
| private SpringContextGatheringModelTypeAdapter() { | ||
| } | ||
|
|
||
| public static SpringContextGatheringModelTypeAdapter getInstance() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(JsonWriter out, SpringContextGatheringModel model) throws IOException { | ||
| boolean serializeNulls = out.getSerializeNulls(); | ||
| out.setSerializeNulls(true); | ||
| try { | ||
| out.beginObject(); | ||
| out.name(FILES_DATA); | ||
| out.beginObject(); | ||
| for (Map.Entry<String, Map<String, SpringContextGatheringModel.InputFileData>> module : model.filesData().entrySet()) { | ||
| out.name(module.getKey()); | ||
| out.beginObject(); | ||
| for (Map.Entry<String, SpringContextGatheringModel.InputFileData> file : module.getValue().entrySet()) { | ||
| out.name(file.getKey()); | ||
| writeInputFileData(out, file.getValue()); | ||
| } | ||
| out.endObject(); | ||
| } | ||
| out.endObject(); | ||
| out.endObject(); | ||
| } finally { | ||
| out.setSerializeNulls(serializeNulls); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public SpringContextGatheringModel read(JsonReader in) throws IOException { | ||
| var model = new SpringContextGatheringModel(); | ||
| boolean filesDataRead = false; | ||
| in.beginObject(); | ||
| while (in.hasNext()) { | ||
| if (FILES_DATA.equals(in.nextName())) { | ||
| readFilesData(in, model); | ||
| filesDataRead = true; | ||
| } else { | ||
| in.skipValue(); | ||
| } | ||
| } | ||
| in.endObject(); | ||
| if (!filesDataRead) { | ||
| throw missingProperty(FILES_DATA); | ||
| } | ||
| return model; | ||
| } | ||
|
|
||
| private static void writeInputFileData(JsonWriter out, SpringContextGatheringModel.InputFileData fileData) throws IOException { | ||
| out.beginObject(); | ||
| out.name(BEANS); | ||
| out.beginArray(); | ||
| for (BeanDefinitionHolder.InputFileData bean : fileData.beans()) { | ||
| BeanDefinitionHolderTypeAdapter.getInstance().write(out, bean); | ||
| } | ||
| out.endArray(); | ||
| out.name(PACKAGES); | ||
| writeStrings(out, fileData.packages()); | ||
| out.endObject(); | ||
| } | ||
|
|
||
| private static void readFilesData(JsonReader in, SpringContextGatheringModel model) throws IOException { | ||
| in.beginObject(); | ||
| while (in.hasNext()) { | ||
| String moduleKey = in.nextName(); | ||
| model.ensureModule(moduleKey); | ||
| in.beginObject(); | ||
| while (in.hasNext()) { | ||
| readInputFileData(in, model, moduleKey, in.nextName()); | ||
| } | ||
| in.endObject(); | ||
| } | ||
| in.endObject(); | ||
| } | ||
|
|
||
| private static void readInputFileData(JsonReader in, SpringContextGatheringModel model, String moduleKey, String fileKey) throws IOException { | ||
| List<BeanDefinitionHolder.InputFileData> beans = null; | ||
| Set<String> packages = null; | ||
| in.beginObject(); | ||
| while (in.hasNext()) { | ||
| switch (in.nextName()) { | ||
| case BEANS -> beans = readBeans(in); | ||
| case PACKAGES -> packages = readStrings(in); | ||
| default -> in.skipValue(); | ||
| } | ||
| } | ||
| in.endObject(); | ||
| model.collectBeans(moduleKey, fileKey, null, required(beans, BEANS)); | ||
| model.collectPackages(moduleKey, fileKey, null, required(packages, PACKAGES)); | ||
| } | ||
|
|
||
| private static List<BeanDefinitionHolder.InputFileData> readBeans(JsonReader in) throws IOException { | ||
| List<BeanDefinitionHolder.InputFileData> beans = new ArrayList<>(); | ||
| in.beginArray(); | ||
| while (in.hasNext()) { | ||
| beans.add(BeanDefinitionHolderTypeAdapter.getInstance().read(in)); | ||
| } | ||
| in.endArray(); | ||
| return beans; | ||
| } | ||
| } |
107 changes: 107 additions & 0 deletions
107
...rc/test/java/org/sonar/java/serialization/SpringContextGatheringModelTypeAdapterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.serialization; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.JsonParser; | ||
| import java.io.File; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.sonar.api.batch.fs.InputFile; | ||
| import org.sonar.java.TestUtils; | ||
| import org.sonar.java.model.springcontext.BeanDefinitionHolder; | ||
| import org.sonar.java.model.springcontext.ProfileExpression; | ||
| import org.sonar.java.model.springcontext.SpringContextGatheringModel; | ||
| import org.sonar.java.reporting.AnalyzerMessage.TextSpan; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| class SpringContextGatheringModelTypeAdapterTest { | ||
|
|
||
| private static final Gson GSON = new Gson(); | ||
| private static final InputFile INPUT_FILE = TestUtils.inputFile(new File("src/test/files/springcontext/SimpleComponent.java")); | ||
|
|
||
| @Test | ||
| void round_trips_module_and_file_data_without_serializing_input_files() { | ||
| var bean = new BeanDefinitionHolder.InputFileData("bean", "example.Bean", "example", new TextSpan(1, 0, 1, 4), | ||
| false, ProfileExpression.UNCONDITIONAL, null, Map.of(), Set.of("example.Bean")); | ||
| var model = new SpringContextGatheringModel(); | ||
| model.collectBeans("module-a", "file-a", INPUT_FILE, List.of(bean)); | ||
| model.collectPackages("module-a", "file-a", INPUT_FILE, Set.of("example")); | ||
| model.collectBeans("module-a", "file-b", INPUT_FILE, List.of()); | ||
| model.collectPackages("module-b", "file-c", INPUT_FILE, Set.of("other")); | ||
|
|
||
| String json = GSON.toJson(model); | ||
|
|
||
| var filesData = JsonParser.parseString(json).getAsJsonObject().getAsJsonObject("filesData"); | ||
| assertThat(filesData.keySet()).containsExactlyInAnyOrder("module-a", "module-b"); | ||
| var file = filesData.getAsJsonObject("module-a").getAsJsonObject("file-a"); | ||
| assertThat(file.has("inputFile")).isFalse(); | ||
| assertThat(file.getAsJsonArray("beans")).hasSize(1); | ||
| assertThat(file.getAsJsonArray("packages").get(0).getAsString()).isEqualTo("example"); | ||
|
|
||
| var restored = GSON.fromJson(json, SpringContextGatheringModel.class); | ||
| assertThat(restored.filesData().get("module-a")).containsOnlyKeys("file-a", "file-b"); | ||
| assertThat(restored.filesData().get("module-b")).containsOnlyKeys("file-c"); | ||
| assertThat(restored.filesData().get("module-a").get("file-a").beans()).containsExactly(bean); | ||
| assertThat(restored.filesData().get("module-a").get("file-a").packages()).containsExactly("example"); | ||
| assertThat(restored.filesData().get("module-a").get("file-b").beans()).isEmpty(); | ||
| assertThat(restored.filesData().get("module-b").get("file-c").packages()).containsExactly("other"); | ||
| restored.filesData().values().stream() | ||
| .flatMap(files -> files.values().stream()) | ||
| .forEach(data -> assertThat(data.inputFile()).isNull()); | ||
| } | ||
|
|
||
| @Test | ||
| void ignores_input_file_property_when_reading_json() { | ||
| String json = """ | ||
| {"filesData":{"module":{"file":{"inputFile":{"key":"ignored"},"beans":[],"packages":[]}}}} | ||
| """; | ||
|
|
||
| var restored = GSON.fromJson(json, SpringContextGatheringModel.class); | ||
|
|
||
| assertThat(restored.filesData().get("module").get("file").inputFile()).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void preserves_an_empty_module() { | ||
| String json = "{\"filesData\":{\"module\":{}}}"; | ||
|
|
||
| var restored = GSON.fromJson(json, SpringContextGatheringModel.class); | ||
|
|
||
| assertThat(restored.filesData().get("module")).isEmpty(); | ||
| assertThat(GSON.toJson(restored)).isEqualTo(json); | ||
| } | ||
|
|
||
| @Test | ||
| void rejects_missing_file_data() { | ||
| assertThatThrownBy(() -> GSON.fromJson("{}", SpringContextGatheringModel.class)) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessage("Missing JSON property 'filesData'"); | ||
| } | ||
|
|
||
| @Test | ||
| void rejects_missing_InputFileData_field() { | ||
| String missingPackages = "{\"filesData\":{\"module\":{\"file\":{\"beans\":[]}}}}"; | ||
| assertThatThrownBy(() -> GSON.fromJson(missingPackages, SpringContextGatheringModel.class)) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessage("Missing JSON property 'packages'"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.