-
Notifications
You must be signed in to change notification settings - Fork 727
SONARJAVA-6442 Introduce SpringContextModelGatherer and SpringContextModelSensor #5654
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
Merged
leonardo-pilastri-sonarsource
merged 7 commits into
epic-SONARJAVA-6237
from
lp/draft-spring-context-gatherers
Jun 5, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
743955a
Introduce SpringContextModelGatherer and SpringContextModelSensor
leonardo-pilastri-sonarsource f8db4d7
Review changes
leonardo-pilastri-sonarsource 09056d3
Added some tests
leonardo-pilastri-sonarsource 1be2243
Review changes
leonardo-pilastri-sonarsource df93fb8
Fix QG
leonardo-pilastri-sonarsource 239fb7d
Improve coverage
leonardo-pilastri-sonarsource 6f2b7c8
Fix issue after rebase
leonardo-pilastri-sonarsource 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
42 changes: 42 additions & 0 deletions
42
...frontend/src/main/java/org/sonar/java/model/springcontext/SpringContextModelGatherer.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,42 @@ | ||
| /* | ||
| * 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.model.springcontext; | ||
|
|
||
| import org.sonar.java.ast.visitors.SubscriptionVisitor; | ||
| import org.sonar.java.model.DefaultModuleScannerContext; | ||
| import org.sonar.plugins.java.api.ModuleScannerContext; | ||
| import org.sonar.plugins.java.api.internal.EndOfAnalysis; | ||
|
|
||
| /** | ||
| * Base class for visitors that need to gather data in the SpringContextModel at the end of the analysis. | ||
| * Extending classes will have to implement the AST visitor pattern, gather relevant spring-related data, and store it | ||
| * in the SpringContextModel at the of a module analysis. | ||
| */ | ||
| public abstract class SpringContextModelGatherer extends SubscriptionVisitor implements EndOfAnalysis { | ||
|
|
||
| @Override | ||
| public final void endOfAnalysis(ModuleScannerContext context) { | ||
| var defaultModuleContext = (DefaultModuleScannerContext) context; | ||
| gatherSpringContextData(context, defaultModuleContext.getSpringContextModel()); | ||
| } | ||
|
leonardo-pilastri-sonarsource marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Method called at the end of the analysis of a module, allowing to store gathered data in the SpringContextModel. | ||
| */ | ||
| public abstract void gatherSpringContextData(ModuleScannerContext context, SpringContextModel springContextModel); | ||
|
|
||
| } | ||
34 changes: 34 additions & 0 deletions
34
...rontend/src/main/java/org/sonar/java/model/springcontext/SpringContextModelGatherers.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,34 @@ | ||
| /* | ||
| * 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.model.springcontext; | ||
|
|
||
| import java.util.List; | ||
| import org.sonar.plugins.java.api.JavaCheck; | ||
|
|
||
| public class SpringContextModelGatherers { | ||
|
|
||
| private SpringContextModelGatherers() { | ||
| // utility class, should not be instantiated | ||
| } | ||
|
|
||
| public static List<JavaCheck> getAllGatherers() { | ||
| return List.of( | ||
| // example: new SampleSpringContextModelGatherer() | ||
| ); | ||
| } | ||
|
|
||
| } |
73 changes: 73 additions & 0 deletions
73
...tend/src/test/java/org/sonar/java/model/springcontext/SpringContextModelGathererTest.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,73 @@ | ||
| /* | ||
| * 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.model.springcontext; | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
|
|
||
| import java.io.File; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.sonar.api.batch.fs.InputFile; | ||
| import org.sonar.api.batch.sensor.internal.SensorContextTester; | ||
| import org.sonar.java.SonarComponents; | ||
| import org.sonar.java.TestUtils; | ||
| import org.sonar.java.model.JParserTestUtils; | ||
| import org.sonar.java.model.VisitorsBridge; | ||
| import org.sonar.plugins.java.api.JavaCheck; | ||
| import org.sonar.plugins.java.api.ModuleScannerContext; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class SpringContextModelGathererTest { | ||
|
|
||
| private final SpringContextModel model = new SpringContextModel(); | ||
|
|
||
| @Test | ||
| void testGatherSpringContextData() { | ||
| getSpringModelAfterVisitingFile("src/test/files/model/SimpleClass.java", new SampleGatherer()); | ||
| assertThat(model.getTypeToBeanNamesIndex().getNamesForType("com.example.MyService")).containsExactly("myServiceBean"); | ||
| } | ||
|
|
||
| private void getSpringModelAfterVisitingFile(String filePath, JavaCheck check) { | ||
| File file = new File(filePath); | ||
| InputFile inputFile = TestUtils.inputFile(file); | ||
| var compilationUnit = JParserTestUtils.parse(file); | ||
| SensorContextTester sensorContextTester = SensorContextTester.create(new File("")); | ||
| var sonarComponents = new SonarComponents(null, null, null, null, null, null); | ||
| sonarComponents.setSensorContext(sensorContextTester); | ||
| sonarComponents.setSpringContextModel(model); | ||
|
|
||
| VisitorsBridge visitorsBridge = new VisitorsBridge(List.of(check), new ArrayList<>(), sonarComponents); | ||
| visitorsBridge.setCurrentFile(inputFile); | ||
| visitorsBridge.visitFile(compilationUnit, false); | ||
| visitorsBridge.endOfAnalysis(); | ||
| } | ||
|
|
||
| class SampleGatherer extends SpringContextModelGatherer { | ||
|
|
||
| @Override | ||
| public void gatherSpringContextData(ModuleScannerContext context, SpringContextModel springContextModel) { | ||
| springContextModel.getTypeToBeanNamesIndex().addBeanForType("com.example.MyService", "myServiceBean"); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Tree.Kind> nodesToVisit() { | ||
| return List.of(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
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.