SONARJAVA-7095 Save and restore the Spring context gathering model between analyses - #6289
aurelien-coet-sonarsource wants to merge 4 commits into
Conversation
02259e8 to
c74f349
Compare
15913cd to
3a5cee8
Compare
This comment has been minimized.
This comment has been minimized.
3a5cee8 to
5945fce
Compare
ff0e310 to
c28b35f
Compare
8598201 to
2fe8eeb
Compare
asya-vorobeva
left a comment
There was a problem hiding this comment.
Generally LGTM. Comments about documentation / tests
| @@ -90,7 +91,7 @@ | |||
|
|
|||
| class JavaSensorTest { | |||
There was a problem hiding this comment.
Here we should have some tests for restoring of our model. I'd suggest to configure real json file in src/test/resources and use it for loading model. Thus we'll also have our format for model's file documented.
| assertThat(context.allIssues()).hasSize(1); | ||
| assertThat(context.allIssues().iterator().next().primaryLocation().inputComponent()).isEqualTo(analyzedFile); | ||
| var savedModel = SpringContextModelPersistence.load(tempDir.resolve(MODEL_PATH)); | ||
| assertThat(savedModel.filesData().get(MODULE_KEY)).hasSize(4); |
There was a problem hiding this comment.
Here I suggest the same approach as in JavaSensorTest. Let's create real json files in src/test/resources to compare resulting files with them. It will be much easier to read and understand.
| .containsEntry("java.spring.bean_count", "2") | ||
| .containsEntry("java.spring.component_scan_package_count", "0"); | ||
| var savedModel = SpringContextModelPersistence.load(tempDir.resolve(MODEL_PATH)); | ||
| assertThat(savedModel.filesData().get(MODULE_KEY)).containsOnlyKeys("currentService", "consumer"); |
There was a problem hiding this comment.
The same here and for all other tests that deal with the new persistence output file.
| TelemetryKey.JAVA_SPRING_CONTEXT_MODEL_GATHERING_TIME_MS, | ||
| TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - buildingStartTime)); | ||
| recordSpringTelemetry(springContextModel); | ||
| SpringContextModelPersistence.configuredPath(context, context.fileSystem().baseDir()) |
There was a problem hiding this comment.
Let's add little comment here to explain why we're doing it
| PerformanceMeasure.Duration sensorDuration = createPerformanceMeasureReport(context); | ||
|
|
||
| sonarComponents.setSensorContext(context); | ||
| if (!springContextGatheringModel.isRestored()) { |
There was a problem hiding this comment.
Let's add a little explaining comment here
| @ScannerSide | ||
| @SonarLintSide | ||
| @JsonAdapter(SpringContextGatheringModelTypeAdapter.class) | ||
| public class SpringContextGatheringModel { |
There was a problem hiding this comment.
Let's extend JavaDoc for this file. We should clarify for what purposes and how do we use this class. Currently it's shared between two different responsibilities: caching for incremental analysis and a3s context.
| @@ -65,10 +69,22 @@ public void describe(SensorDescriptor descriptor) { | |||
|
|
|||
| @Override | |||
| public void execute(SensorContext context) { | |||
There was a problem hiding this comment.
This method needs JavaDoc cooment, too many different things are happening here.
| .description("Path to the JSON file used to restore Spring context data at the start of analysis and save it afterward. " | ||
| + "When unset, no file is read or written. Relative paths use the root project directory.") | ||
| .category(JavaConstants.JAVA_CATEGORY) | ||
| .subCategory("General") |
There was a problem hiding this comment.
Maybe we should use here something more specific, e.g. "Frameworks"?
Code Review ✅ Approved 5 closed / 5 findings🟡 Medium risk · Persists Spring context data across analyses and changes incremental issue-model construction. Saves and restores the Spring context gathering model between analyses to preserve unchanged files and their beans. Five issues were addressed: stale beans from deleted/renamed files, missing root-project path test coverage, premature file renaming before flush, unconditional model saving that fails analysis on write errors, and default-on restoration that fails when the model file is unreadable. ✅ 5 closed✅ Bug: Restored model never drops deleted/renamed files, keeping stale beans
✅ Quality: Root-project path test never exercises root-project resolution
✅ Bug: Temp file is renamed over the model before the writer is flushed
✅ Bug: Saving the model always runs, and any write error fails the analysis
✅ Bug: Default-on model restore makes an unreadable model file fail all analyses
Review coverage🧪 Functional validation 2 of 2 objectives covered 📋 Rules No rules evaluated Cross-repo coverage 3 repositories selected Cross-repo inspection is incomplete. Unread code may contain additional impacts. 🤖 Auto-approval Not enabled · Set up Implementation Status ✅ 2 of 2 objectives covered✅ SONARJAVA-7095 - 2 of 2 objectives coveredThis PR covers the implementation of on-disk serialization and deserialization for the Spring context model at the start and end of analyses. ✅ 2 covered here
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|




Summary by Gitar
SpringContextModelPersistenceto save and loadSpringContextGatheringModelacross analysessonar.java.springContext.model.pathproperty to configure spring context model persistence file pathrestoreFrom,removeUnvisitedFiles, and lifecycle support inSpringContextGatheringModelto preserve unchanged files between analysesThis will update automatically on new commits.