-
Notifications
You must be signed in to change notification settings - Fork 727
SONARJAVA-6410 Create SpringContextModel and related classes #5646
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
asya-vorobeva
merged 5 commits into
epic-SONARJAVA-6237
from
asya/basic-spring-context-model
Jun 1, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6526406
Add Spring context model classes with Javadoc
asya-vorobeva 587b5a4
Add package-info.java for springcontext package
asya-vorobeva 045a452
Fixing issues
asya-vorobeva b544e83
Fixing issues
asya-vorobeva b9055ad
Merge remote-tracking branch 'origin/asya/basic-spring-context-model'…
asya-vorobeva 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
154 changes: 154 additions & 0 deletions
154
java-frontend/src/main/java/org/sonar/java/model/springcontext/BeanDefinitionHolder.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,154 @@ | ||
| /* | ||
| * 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.ArrayList; | ||
| import java.util.List; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Immutable representation of a Spring bean definition discovered during project scanning. | ||
| * | ||
| * <p>Captures the bean's fully-qualified type, the module and package it belongs to, | ||
| * its source {@link BeanLocation location}, and optional metadata such as active profiles, | ||
| * dependency names, and whether the bean is marked as {@code @Primary}. | ||
| * | ||
| * <p>Use {@link Builder} to construct instances: | ||
| * <pre>{@code | ||
| * BeanDefinitionHolder bean = new BeanDefinitionHolder.Builder(type, module, pkg, location) | ||
| * .profiles("prod") | ||
| * .primary() | ||
| * .build(); | ||
| * }</pre> | ||
| * | ||
| * @see BeanDefinitionRegistry | ||
| * @see BeanLocation | ||
| */ | ||
| public class BeanDefinitionHolder { | ||
| /** Fully-qualified class name of the bean. */ | ||
| private final String type; | ||
|
|
||
| /** Module in which the bean is declared. */ | ||
| private final String module; | ||
|
|
||
| /** Package of the bean's declaring class. */ | ||
| private final String beanPackage; | ||
|
|
||
| /** Source location where the bean definition appears. */ | ||
| private final BeanLocation location; | ||
|
|
||
| /** Names of other beans this bean depends on. */ | ||
| private List<String> dependingBeans; | ||
|
|
||
| /** Comma-separated Spring profile expressions under which this bean is active, or {@code null} if unconditional. */ | ||
| @Nullable | ||
| private String profiles; | ||
|
|
||
| /** Whether the bean is marked as {@code @Primary}, making it the preferred candidate for autowiring. */ | ||
| private boolean isPrimary = false; | ||
|
|
||
| private BeanDefinitionHolder(String type, String module, String beanPackage, BeanLocation location) { | ||
| this.type = type; | ||
| this.module = module; | ||
| this.beanPackage = beanPackage; | ||
| this.location = location; | ||
| } | ||
|
|
||
| private void setDependingBeans(List<String> beansList) { | ||
| this.dependingBeans = beansList; | ||
| } | ||
|
|
||
| private void setProfiles(@Nullable String profiles) { | ||
| this.profiles = profiles; | ||
| } | ||
|
|
||
| private void setPrimary() { | ||
| this.isPrimary = true; | ||
| } | ||
|
|
||
| public String getType() { | ||
| return type; | ||
| } | ||
|
|
||
| public String getModule() { | ||
| return module; | ||
| } | ||
|
|
||
| public String getBeanPackage() { | ||
| return beanPackage; | ||
| } | ||
|
|
||
| public BeanLocation getLocation() { | ||
| return location; | ||
| } | ||
|
|
||
| public List<String> getDependingBeans() { | ||
| return dependingBeans; | ||
| } | ||
|
|
||
| @Nullable | ||
| public String getProfiles() { | ||
| return profiles; | ||
| } | ||
|
|
||
| public boolean isPrimary() { | ||
| return isPrimary; | ||
| } | ||
|
|
||
| public static class Builder { | ||
| private final String type; | ||
| private final String module; | ||
| private final String beanPackage; | ||
| private final BeanLocation location; | ||
| private List<String> dependingBeans = new ArrayList<>(); | ||
| @Nullable | ||
| private String profiles; | ||
| private boolean isPrimary = false; | ||
|
|
||
| public Builder(String type, String module, String beanPackage, BeanLocation location) { | ||
| this.type = type; | ||
| this.module = module; | ||
| this.beanPackage = beanPackage; | ||
| this.location = location; | ||
| } | ||
|
|
||
| public Builder dependingBeans(List<String> beansList) { | ||
| this.dependingBeans = beansList; | ||
| return this; | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| public Builder profiles(@Nullable String profiles) { | ||
| this.profiles = profiles; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder primary() { | ||
| this.isPrimary = true; | ||
| return this; | ||
| } | ||
|
|
||
| public BeanDefinitionHolder build() { | ||
| BeanDefinitionHolder holder = new BeanDefinitionHolder(type, module, beanPackage, location); | ||
| holder.setDependingBeans(List.copyOf(dependingBeans)); | ||
| holder.setProfiles(profiles); | ||
| if (isPrimary) { | ||
| holder.setPrimary(); | ||
| } | ||
| return holder; | ||
| } | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
java-frontend/src/main/java/org/sonar/java/model/springcontext/BeanDefinitionRegistry.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,48 @@ | ||
| /* | ||
| * 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.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Tracks {@link BeanDefinitionHolder bean definitions} collected during Spring context scanning, | ||
| * indexed by bean name. | ||
| * | ||
| * <p>Multiple definitions registered under the same name are preserved rather than overwritten, | ||
| * allowing callers to detect and report duplicate bean declarations. | ||
| * | ||
| * @see BeanDefinitionHolder | ||
| */ | ||
| public class BeanDefinitionRegistry { | ||
| /** | ||
| * Maps bean names to their corresponding list of {@link BeanDefinitionHolder} instances. | ||
| * A list is used as the value to capture duplicate bean definitions under the same name, | ||
| * which is an invalid state that should be reported during analysis. | ||
| */ | ||
| private final Map<String, List<BeanDefinitionHolder>> beanDefinitions = new HashMap<>(); | ||
|
|
||
| public List<BeanDefinitionHolder> getByName(String beanName) { | ||
| return beanDefinitions.getOrDefault(beanName, List.of()); | ||
| } | ||
|
|
||
| public void addBeanDefinition(String beanName, BeanDefinitionHolder beanDefinition) { | ||
| beanDefinitions.computeIfAbsent(beanName, k -> new ArrayList<>()).add(beanDefinition); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
java-frontend/src/main/java/org/sonar/java/model/springcontext/BeanLocation.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,29 @@ | ||
| /* | ||
| * 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.api.batch.fs.InputFile; | ||
| import org.sonar.java.reporting.AnalyzerMessage; | ||
|
|
||
| /** | ||
| * Source location of a Spring bean definition within the project. | ||
| * | ||
| * @param inputFile the file in which the bean is declared | ||
| * @param mainLocation the precise text span of the bean declaration, used for reporting issues | ||
| */ | ||
| public record BeanLocation(InputFile inputFile, AnalyzerMessage.TextSpan mainLocation) { | ||
| } |
56 changes: 56 additions & 0 deletions
56
...ontend/src/main/java/org/sonar/java/model/springcontext/EntityClassToPropertiesIndex.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,56 @@ | ||
| /* | ||
| * 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.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Index mapping JPA / Hibernate {@code @Entity} class names to their associated | ||
| * key-value properties collected during scanning. | ||
| * | ||
| * <p>Multiple properties can be registered for the same entity class. | ||
| * Lookup returns an empty set for entity classes that have no registered properties. | ||
| */ | ||
| public class EntityClassToPropertiesIndex { | ||
| /** Properties indexed by fully-qualified {@code @Entity} class name. */ | ||
| private final Map<String, Set<Map.Entry<String, String>>> propertiesByEntityClass = new HashMap<>(); | ||
|
|
||
| /** | ||
| * Registers a property for the given {@code @Entity} class. | ||
| * | ||
| * @param entityClass fully-qualified name of the {@code @Entity} class | ||
| * @param propertyKey the property name | ||
| * @param propertyValue the property value | ||
| */ | ||
| public void addProperty(String entityClass, String propertyKey, String propertyValue) { | ||
|
leonardo-pilastri-sonarsource marked this conversation as resolved.
|
||
| propertiesByEntityClass.computeIfAbsent(entityClass, k -> new HashSet<>()).add(Map.entry(propertyKey, propertyValue)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns an immutable set of all properties registered for the given {@code @Entity} class. | ||
| * | ||
| * @param entityClass fully-qualified name of the {@code @Entity} class | ||
| * @return an unmodifiable set of key-value property entries, or an empty set if none were registered | ||
| */ | ||
| public Set<Map.Entry<String, String>> getPropertiesForEntity(String entityClass) { | ||
| return Collections.unmodifiableSet(propertiesByEntityClass.getOrDefault(entityClass, Set.of())); | ||
| } | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
java-frontend/src/main/java/org/sonar/java/model/springcontext/ProjectPackageScan.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,62 @@ | ||
| /* | ||
| * 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.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Tracks the packages registered for Spring component scanning, grouped by module. | ||
| * | ||
| * <p>Corresponds to packages declared via {@code @ComponentScan} (or equivalent) and | ||
| * collected during project analysis. Each module may declare multiple scanned packages. | ||
| */ | ||
| public class ProjectPackageScan { | ||
| /** Scanned package names indexed by module name. */ | ||
| private final Map<String, Set<String>> packagesScannedBySpringPerModule = new HashMap<>(); | ||
|
|
||
| /** | ||
| * Registers a package as scanned by Spring for the given module. | ||
| * | ||
| * @param module the module in which the component scan is configured | ||
| * @param packageName the package name declared for scanning | ||
| */ | ||
| public void addPackage(String module, String packageName) { | ||
| packagesScannedBySpringPerModule.computeIfAbsent(module, k -> new HashSet<>()).add(packageName); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the set of packages scanned by Spring for the given module. | ||
| * | ||
| * @param module the module name | ||
| * @return the scanned package names, or an empty set if none were registered | ||
| */ | ||
| public Set<String> getPackagesForModule(String module) { | ||
| return packagesScannedBySpringPerModule.getOrDefault(module, Set.of()); | ||
| } | ||
|
|
||
| /** | ||
| * Returns all modules that have at least one registered scanned package. | ||
| * | ||
| * @return the set of module names | ||
| */ | ||
| public Set<String> getModules() { | ||
| return packagesScannedBySpringPerModule.keySet(); | ||
| } | ||
| } |
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.