Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Comment thread
leonardo-pilastri-sonarsource marked this conversation as resolved.
/** 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;
Comment thread
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;
}
}
}
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);
}
}
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) {
}
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) {
Comment thread
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()));
}
}
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();
}
}
Loading
Loading