Skip to content

Commit 31312fc

Browse files
SONARJAVA-6205 Add an agentic focused quality profile for Java
1 parent 560700b commit 31312fc

File tree

7 files changed

+692
-13
lines changed

7 files changed

+692
-13
lines changed
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.Set;
20+
import javax.annotation.Nullable;
21+
import org.sonar.api.rule.RuleKey;
22+
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
23+
import org.sonar.plugins.java.api.ProfileRegistrar;
24+
import org.sonarsource.api.sonarlint.SonarLintSide;
25+
26+
@SonarLintSide
27+
public class JavaAgenticWayProfile implements BuiltInQualityProfilesDefinition {
28+
private final ProfileRegistrar[] profileRegistrars;
29+
30+
/**
31+
* Constructor used by Pico container (SC) when no ProfileRegistrar are available
32+
*/
33+
public JavaAgenticWayProfile() {
34+
this(null);
35+
}
36+
37+
public JavaAgenticWayProfile(@Nullable ProfileRegistrar[] profileRegistrars) {
38+
this.profileRegistrars = profileRegistrars;
39+
}
40+
41+
@Override
42+
public void define(Context context) {
43+
NewBuiltInQualityProfile agenticWay = context.createBuiltInQualityProfile("AI Quality Profile", Java.KEY);
44+
Set<RuleKey> ruleKeys = QualityProfileUtils.registerRulesFromJson(
45+
"/org/sonar/l10n/java/rules/java/Agentic_way_profile.json",
46+
this.profileRegistrars
47+
);
48+
49+
ruleKeys.forEach(ruleKey -> agenticWay.activateRule(ruleKey.repository(), ruleKey.rule()));
50+
agenticWay.done();
51+
}
52+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public void define(Context context) {
6262
list.addAll(SurefireExtensions.getExtensions());
6363
list.add(DroppedPropertiesSensor.class);
6464
list.add(JavaSonarWayProfile.class);
65+
list.add(JavaAgenticWayProfile.class);
6566
list.add(ClasspathForMain.class);
6667

6768
ExternalReportExtensions.define(context);

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+
}

0 commit comments

Comments
 (0)