Skip to content

Commit 0dab089

Browse files
Fixes and refactoring
1 parent 5144ae0 commit 0dab089

File tree

5 files changed

+64
-59
lines changed

5 files changed

+64
-59
lines changed

sonar-java-plugin/src/main/java/org/sonar/plugins/java/JavaAgenticWayProfile.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,15 @@
1616
*/
1717
package org.sonar.plugins.java;
1818

19-
import java.util.HashSet;
2019
import java.util.Set;
21-
import java.util.stream.Collectors;
2220
import javax.annotation.Nullable;
2321
import org.sonar.api.rule.RuleKey;
2422
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
25-
import org.sonar.java.GeneratedCheckList;
2623
import org.sonar.plugins.java.api.ProfileRegistrar;
27-
import org.sonarsource.analyzer.commons.BuiltInQualityProfileJsonLoader;
2824
import org.sonarsource.api.sonarlint.SonarLintSide;
2925

3026
@SonarLintSide
3127
public class JavaAgenticWayProfile implements BuiltInQualityProfilesDefinition {
32-
private static final String AGENTIC_WAY_PATH = "/org/sonar/l10n/java/rules/java/Agentic_way_profile.json";
33-
3428
private final ProfileRegistrar[] profileRegistrars;
3529

3630
/**
@@ -47,20 +41,12 @@ public JavaAgenticWayProfile(@Nullable ProfileRegistrar[] profileRegistrars) {
4741
@Override
4842
public void define(Context context) {
4943
NewBuiltInQualityProfile agenticWay = context.createBuiltInQualityProfile("Agentic way", Java.KEY);
50-
Set<RuleKey> ruleKeys = new HashSet<>(sonarJavaAgenticWayRuleKeys());
51-
if (profileRegistrars != null) {
52-
for (ProfileRegistrar profileRegistrar : profileRegistrars) {
53-
profileRegistrar.register(ruleKeys::addAll);
54-
}
55-
}
44+
Set<RuleKey> ruleKeys = QualityProfileUtils.registerRulesFromJson(
45+
"/org/sonar/l10n/java/rules/java/Agentic_way_profile.json",
46+
this.profileRegistrars
47+
);
5648

5749
ruleKeys.forEach(ruleKey -> agenticWay.activateRule(ruleKey.repository(), ruleKey.rule()));
5850
agenticWay.done();
5951
}
60-
61-
static Set<RuleKey> sonarJavaAgenticWayRuleKeys() {
62-
return BuiltInQualityProfileJsonLoader.loadActiveKeysFromJsonProfile(AGENTIC_WAY_PATH).stream()
63-
.map(rule -> RuleKey.of(GeneratedCheckList.REPOSITORY_KEY, rule))
64-
.collect(Collectors.toSet());
65-
}
6652
}

sonar-java-plugin/src/main/java/org/sonar/plugins/java/JavaSonarWayProfile.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@
2626
import org.slf4j.LoggerFactory;
2727
import org.sonar.api.rule.RuleKey;
2828
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
29-
import org.sonar.java.GeneratedCheckList;
3029
import org.sonar.java.annotations.VisibleForTesting;
3130
import org.sonar.plugins.java.api.ProfileRegistrar;
32-
import org.sonarsource.analyzer.commons.BuiltInQualityProfileJsonLoader;
3331
import org.sonarsource.api.sonarlint.SonarLintSide;
3432

3533
/**
@@ -66,12 +64,10 @@ public JavaSonarWayProfile(@Nullable ProfileRegistrar[] profileRegistrars) {
6664
@Override
6765
public void define(Context context) {
6866
NewBuiltInQualityProfile sonarWay = context.createBuiltInQualityProfile("Sonar way", Java.KEY);
69-
Set<RuleKey> ruleKeys = new HashSet<>(sonarJavaSonarWayRuleKeys());
70-
if (profileRegistrars != null) {
71-
for (ProfileRegistrar profileRegistrar : profileRegistrars) {
72-
profileRegistrar.register(ruleKeys::addAll);
73-
}
74-
}
67+
Set<RuleKey> ruleKeys = QualityProfileUtils.registerRulesFromJson(
68+
SONAR_WAY_PATH,
69+
this.profileRegistrars
70+
);
7571

7672
// Former activation mechanism, it should be removed once sonar-security and sonar-dataflow-bug-detection
7773
// support the new mechanism:
@@ -89,9 +85,7 @@ public void define(Context context) {
8985
}
9086

9187
static Set<RuleKey> sonarJavaSonarWayRuleKeys() {
92-
return BuiltInQualityProfileJsonLoader.loadActiveKeysFromJsonProfile(SONAR_WAY_PATH).stream()
93-
.map(rule -> RuleKey.of(GeneratedCheckList.REPOSITORY_KEY, rule))
94-
.collect(Collectors.toSet());
88+
return QualityProfileUtils.loadRuleKeys(SONAR_WAY_PATH);
9589
}
9690

9791
@VisibleForTesting
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012-2025 SonarSource Sàrl
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonar.plugins.java;
18+
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
import java.util.stream.Collectors;
22+
import javax.annotation.Nullable;
23+
import org.sonar.api.rule.RuleKey;
24+
import org.sonar.java.GeneratedCheckList;
25+
import org.sonar.plugins.java.api.ProfileRegistrar;
26+
import org.sonarsource.analyzer.commons.BuiltInQualityProfileJsonLoader;
27+
28+
class QualityProfileUtils {
29+
private QualityProfileUtils() {
30+
/* This utility class should not be instantiated */
31+
}
32+
33+
static Set<RuleKey> registerRulesFromJson(
34+
String pathToJsonProfile,
35+
@Nullable ProfileRegistrar[] profileRegistrars) {
36+
37+
Set<RuleKey> ruleKeys = new HashSet<>(loadRuleKeys(pathToJsonProfile));
38+
if (profileRegistrars != null) {
39+
for (ProfileRegistrar profileRegistrar : profileRegistrars) {
40+
profileRegistrar.register(ruleKeys::addAll);
41+
}
42+
}
43+
44+
return ruleKeys;
45+
}
46+
47+
static Set<RuleKey> loadRuleKeys(final String pathToJsonProfile) {
48+
return BuiltInQualityProfileJsonLoader.loadActiveKeysFromJsonProfile(pathToJsonProfile).stream()
49+
.map(rule -> RuleKey.of(GeneratedCheckList.REPOSITORY_KEY, rule))
50+
.collect(Collectors.toSet());
51+
}
52+
}

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/Agentic_way_profile.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,6 @@
468468
"S8447",
469469
"S8450",
470470
"S8465",
471-
"S8469",
472-
"S8471"
471+
"S8469"
473472
]
474473
}

sonar-java-plugin/src/test/java/org/sonar/plugins/java/JavaAgenticWayProfileTest.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,16 @@
1717
package org.sonar.plugins.java;
1818

1919

20-
import java.util.ArrayList;
21-
import java.util.List;
2220
import java.util.Map;
2321
import org.junit.jupiter.api.Test;
24-
import org.sonar.api.rule.RuleKey;
2522
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
26-
import org.sonar.plugins.java.api.ProfileRegistrar;
2723

2824
import static org.assertj.core.api.Assertions.assertThat;
2925

3026
class JavaAgenticWayProfileTest {
31-
@Test
32-
void rule_count_as_expected() {
33-
JavaAgenticWayProfile profile = new JavaAgenticWayProfile();
34-
assertThat(profile.sonarJavaAgenticWayRuleKeys()).hasSize(469);
35-
}
3627

3728
@Test
38-
void profile_is_not_registered_as_default() {
29+
void profile_is_registered_as_expected() {
3930
JavaAgenticWayProfile profile = new JavaAgenticWayProfile();
4031
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
4132
profile.define(context);
@@ -45,23 +36,6 @@ void profile_is_not_registered_as_default() {
4536
assertThat(profilesPerLanguages.get("java")).containsOnlyKeys("Agentic way");
4637
BuiltInQualityProfilesDefinition.BuiltInQualityProfile actualProfile = profilesPerLanguages.get("java").get("Agentic way");
4738
assertThat(actualProfile.isDefault()).isFalse();
48-
assertThat(actualProfile.rules()).hasSize(469);
49-
}
50-
51-
@Test
52-
void profile_is_not_registered_as_default_across_profile_registrars() {
53-
final List<RuleKey> ruleKeys = new ArrayList<>();
54-
ProfileRegistrar emptyProfileRegistrar =
55-
registrarContext -> registrarContext.registerDefaultQualityProfileRules(ruleKeys);
56-
57-
JavaAgenticWayProfile profile = new JavaAgenticWayProfile(new ProfileRegistrar[]{emptyProfileRegistrar});
58-
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
59-
profile.define(context);
60-
61-
Map<String, Map<String, BuiltInQualityProfilesDefinition.BuiltInQualityProfile>> profilesPerLanguages = context.profilesByLanguageAndName();
62-
assertThat(profilesPerLanguages).containsOnlyKeys("java");
63-
assertThat(profilesPerLanguages.get("java")).containsOnlyKeys("Agentic way");
64-
BuiltInQualityProfilesDefinition.BuiltInQualityProfile actualProfile = profilesPerLanguages.get("java").get("Agentic way");
65-
assertThat(actualProfile.isDefault()).isFalse();
39+
assertThat(actualProfile.rules()).hasSize(468);
6640
}
6741
}

0 commit comments

Comments
 (0)