Skip to content

Commit ffd7b11

Browse files
SONARJAVA-7253 Do not run Spring context collection in SQ for IDE or in projects without Spring dependencies (#6423)
1 parent 9a448a4 commit ffd7b11

16 files changed

Lines changed: 261 additions & 47 deletions

File tree

‎its/plugin/tests/src/test/java/com/sonar/it/java/JspTest.java‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,6 @@ public void should_transpile_jsp() throws Exception {
110110
Telemetry java.language.version: 8
111111
Telemetry java.module_count: 1
112112
Telemetry java.scanner_app: ScannerMaven
113-
Telemetry java.spring.bean_count: \\d+
114-
Telemetry java.spring.bean_name_count: \\d+
115-
Telemetry java.spring.component_scan_package_count: \\d+
116-
Telemetry java.spring.context_checks_time_ms: \\d+
117-
Telemetry java.spring.context_model_gathering_time_ms: \\d+
118-
Telemetry java.spring.context_model_size_bytes: \\d+
119-
Telemetry java.spring.injection_point_count: \\d+
120113
"""));
121114
}
122115
}

‎its/plugin/tests/src/test/java/com/sonar/it/java/suite/SonarLintIntegrationTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public void foo() {
7171

7272
List<RaisedIssueDto> issues = analyzeFileAndGetIssues(inputFileDto.getUri());
7373

74+
assertThat(baseDir.resolve(".sonar/spring-context-model.json")).doesNotExist();
75+
7476
assertThat(issues)
7577
.extracting(
7678
RaisedIssueDto::getRuleKey,

‎its/scanner-integration-tests/src/test/java/org/sonar/java/it/spring/SpringTelemetryTest.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.sonarsource.scanner.integrationtester.dsl.ScannerOutputReader;
2020
import java.nio.file.Path;
21+
import java.util.List;
2122
import org.junit.jupiter.api.Test;
2223
import org.sonar.java.it.ScannerIntegrationAbstractTest;
2324

@@ -91,6 +92,11 @@ class SpringTelemetryTest extends ScannerIntegrationAbstractTest {
9192
void spring_context_model_is_reported_as_telemetry() {
9293
var project = analyzeProject(Path.of("ambiguous-dependencies-should-be-resolved"), "S9352").getProject();
9394

95+
assertThat(projectBaseDir.resolve(".sonar/spring-context-model.json")).exists();
96+
for (String module : List.of("module-common", "module-a", "module-b", "app")) {
97+
assertThat(projectBaseDir.resolve(module).resolve(".sonar/spring-context-model.json")).doesNotExist();
98+
}
99+
94100
assertThat(telemetryValue(project, "java.spring.bean_count"))
95101
.isEqualTo(Integer.toString(EXPECTED_BEAN_COUNT));
96102
assertThat(telemetryValue(project, "java.spring.bean_name_count"))
@@ -109,6 +115,16 @@ void spring_context_model_is_reported_as_telemetry() {
109115
.isLessThan(MAXIMUM_EXPECTED_CONTEXT_MODEL_SIZE_BYTES);
110116
}
111117

118+
@Test
119+
void spring_context_is_not_collected_or_saved_without_spring_dependencies() {
120+
var project = analyzeProject(Path.of("no-spring-dependencies"), "S9352").getProject();
121+
122+
assertThat(projectBaseDir.resolve(".sonar/spring-context-model.json")).doesNotExist();
123+
assertThat(project.getTelemetryEntry("java.spring.bean_count")).isNull();
124+
assertThat(project.getTelemetryEntry("java.spring.context_model_gathering_time_ms")).isNull();
125+
assertThat(project.getTelemetryEntry("java.spring.context_checks_time_ms")).isNull();
126+
}
127+
112128
private static String telemetryValue(ScannerOutputReader.AnalyzedProject project, String key) {
113129
ScannerOutputReader.TelemetryEntry entry = project.getTelemetryEntry(key);
114130
assertThat(entry).as("telemetry entry %s", key).isNotNull();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.example</groupId>
7+
<artifactId>no-spring-dependencies</artifactId>
8+
<version>1.0.0</version>
9+
</parent>
10+
<artifactId>app</artifactId>
11+
</project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package example;
2+
3+
public class Service {
4+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.example</groupId>
6+
<artifactId>no-spring-dependencies</artifactId>
7+
<version>1.0.0</version>
8+
<packaging>pom</packaging>
9+
<modules>
10+
<module>app</module>
11+
</modules>
12+
<properties>
13+
<maven.compiler.release>17</maven.compiler.release>
14+
</properties>
15+
</project>

‎java-frontend/src/main/java/org/sonar/java/model/springcontext/SpringContextGatheringModel.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
* <p>The structure is serialized to JSON and saved to a file at the end of the analysis, so it can be restored and
4040
* reused in later analyses when {@code sonar.java.springContext.model.path} is explicitly set. Restored data for
4141
* unvisited files is retained to provide whole-project context during partial analyses.</p>
42+
*
43+
* <p>Collection is enabled when Spring dependencies are detected in any analyzed module outside SonarLint.
44+
* This state belongs to the current analysis and is neither persisted nor restored.</p>
4245
*/
4346
@ScannerSide
4447
@SonarLintSide
@@ -76,6 +79,15 @@ public Set<String> packages() {
7679
*/
7780
private final Map<String, Map<String, InputFileData>> filesData = new HashMap<>();
7881
private boolean restored;
82+
private boolean enabled;
83+
84+
public boolean isEnabled() {
85+
return enabled;
86+
}
87+
88+
public void enable() {
89+
enabled = true;
90+
}
7991

8092
/**
8193
* Returns the file data indexed by module key and file key.

‎java-frontend/src/main/java/org/sonar/java/model/springcontext/SpringContextModelGatherer.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ protected SpringContextModelGatherer(SpringContextGatheringModel springContextGa
7878

7979
@Override
8080
public boolean isCompatibleWithDependencies(Function<String, Optional<Version>> dependencyFinder) {
81-
return dependencyFinder.apply("spring-context")
82-
.or(() -> dependencyFinder.apply("spring-beans"))
83-
.or(() -> dependencyFinder.apply("spring-boot-starter"))
84-
.or(() -> dependencyFinder.apply("spring-boot-starter-web"))
85-
.isPresent();
81+
return SpringContextModelGatherers.hasSpringDependency(dependencyFinder);
8682
}
8783

8884
@Override

‎java-frontend/src/main/java/org/sonar/java/model/springcontext/SpringContextModelGatherers.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
package org.sonar.java.model.springcontext;
1818

1919
import java.util.List;
20+
import java.util.Optional;
21+
import java.util.function.Function;
2022
import org.sonar.java.telemetry.Telemetry;
23+
import org.sonar.plugins.java.api.Version;
2124

2225
/**
2326
* Registry of all {@link SpringContextModelGatherer} visitors that collect data for the {@link SpringContextModel}
@@ -32,6 +35,14 @@ private SpringContextModelGatherers() {
3235
// utility class, should not be instantiated
3336
}
3437

38+
public static boolean hasSpringDependency(Function<String, Optional<Version>> dependencyFinder) {
39+
return dependencyFinder.apply("spring-context")
40+
.or(() -> dependencyFinder.apply("spring-beans"))
41+
.or(() -> dependencyFinder.apply("spring-boot-starter"))
42+
.or(() -> dependencyFinder.apply("spring-boot-starter-web"))
43+
.isPresent();
44+
}
45+
3546
/**
3647
* Returns all gatherers that contribute data to the {@link SpringContextModel}.
3748
*

‎java-frontend/src/test/java/org/sonar/java/serialization/SpringContextGatheringModelTypeAdapterTest.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ void round_trips_module_and_file_data_without_serializing_input_files() {
4444
var bean = new BeanDefinitionHolder.InputFileData("bean", "example.Bean", BeanDefinitionKind.STEREOTYPE, "example", new TextSpan(1, 0, 1, 4),
4545
false, ProfileExpression.UNCONDITIONAL, null, Map.of(), Set.of("example.Bean"));
4646
var model = new SpringContextGatheringModel();
47+
model.enable();
4748
model.collectBeans("module-a", "file-a", INPUT_FILE, List.of(bean));
4849
model.collectPackages("module-a", "file-a", INPUT_FILE, Set.of("example"));
4950
model.collectBeans("module-a", "file-b", INPUT_FILE, List.of());
5051
model.collectPackages("module-b", "file-c", INPUT_FILE, Set.of("other"));
5152

5253
String json = GSON.toJson(model);
5354

55+
assertThat(JsonParser.parseString(json).getAsJsonObject().keySet()).containsExactly("filesData");
56+
5457
var filesData = JsonParser.parseString(json).getAsJsonObject().getAsJsonObject("filesData");
5558
assertThat(filesData.keySet()).containsExactlyInAnyOrder("module-a", "module-b");
5659
var file = filesData.getAsJsonObject("module-a").getAsJsonObject("file-a");
@@ -59,6 +62,7 @@ void round_trips_module_and_file_data_without_serializing_input_files() {
5962
assertThat(file.getAsJsonArray("packages").get(0).getAsString()).isEqualTo("example");
6063

6164
var restored = GSON.fromJson(json, SpringContextGatheringModel.class);
65+
assertThat(restored.isEnabled()).isFalse();
6266
assertThat(restored.filesData().get("module-a")).containsOnlyKeys("file-a", "file-b");
6367
assertThat(restored.filesData().get("module-b")).containsOnlyKeys("file-c");
6468
assertThat(restored.filesData().get("module-a").get("file-a").beans()).containsExactly(bean);

0 commit comments

Comments
 (0)