Skip to content

Commit 36c93a5

Browse files
committed
PLUGINAPI-180 New API for sonar-resolve code annotations
1 parent 7136815 commit 36c93a5

3 files changed

Lines changed: 175 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Sonar Plugin API
3+
* Copyright (C) 2009-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 GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.api.issue;
21+
22+
import java.util.Set;
23+
import javax.annotation.Nullable;
24+
import org.sonar.api.rule.RuleKey;
25+
26+
/**
27+
* Holds per-line annotation data parsed from {@code //sonar-resolve} comments.
28+
* @since 13.5
29+
*/
30+
public final class SonarResolveData {
31+
32+
public enum Status {
33+
ACCEPT,
34+
FALSE_POSITIVE
35+
}
36+
37+
private final Set<RuleKey> ruleKeys;
38+
private final String comment;
39+
private final Status status;
40+
41+
/**
42+
* @param ruleKeys the set of rule keys targeted by the annotation
43+
* @param comment the justification comment
44+
* @param status the target status, or {@code null} to default to {@link Status#ACCEPT}
45+
*/
46+
public SonarResolveData(Set<RuleKey> ruleKeys, String comment, @Nullable Status status) {
47+
this.ruleKeys = ruleKeys;
48+
this.comment = comment;
49+
this.status = status != null ? status : Status.ACCEPT;
50+
}
51+
52+
/**
53+
* @return the set of rule keys targeted by the annotation
54+
*/
55+
public Set<RuleKey> ruleKeys() {
56+
return ruleKeys;
57+
}
58+
59+
/**
60+
* @return the justification comment
61+
*/
62+
public String comment() {
63+
return comment;
64+
}
65+
66+
/**
67+
* @return the target status
68+
*/
69+
public Status status() {
70+
return status;
71+
}
72+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Sonar Plugin API
3+
* Copyright (C) 2009-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 GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.api.issue;
21+
22+
import java.util.Map;
23+
import org.sonar.api.batch.fs.InputFile;
24+
import org.sonar.api.scanner.ScannerSide;
25+
26+
/**
27+
* Registers {@code //sonar-resolve} annotations found in source files.
28+
* Can be called multiple times per file; entries are merged.
29+
* @since 13.5
30+
*/
31+
@ScannerSide
32+
public abstract class SonarResolveFilter {
33+
34+
/**
35+
* @param inputFile the file containing the annotations
36+
* @param annotationsByLine map of 1-based line numbers to their annotation data
37+
*/
38+
public abstract SonarResolveFilter sonarResolveInFile(
39+
InputFile inputFile, Map<Integer, SonarResolveData> annotationsByLine);
40+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Sonar Plugin API
3+
* Copyright (C) 2009-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 GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.api.issue;
21+
22+
import java.util.Set;
23+
import org.junit.Test;
24+
import org.sonar.api.rule.RuleKey;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
public class SonarResolveDataTest {
29+
30+
@Test
31+
public void constructor_shouldStoreFields() {
32+
Set<RuleKey> ruleKeys = Set.of(RuleKey.of("java", "S123"));
33+
SonarResolveData data = new SonarResolveData(ruleKeys, "reason", SonarResolveData.Status.FALSE_POSITIVE);
34+
35+
assertThat(data.ruleKeys()).isEqualTo(ruleKeys);
36+
assertThat(data.comment()).isEqualTo("reason");
37+
assertThat(data.status()).isEqualTo(SonarResolveData.Status.FALSE_POSITIVE);
38+
}
39+
40+
@Test
41+
public void constructor_whenStatusIsNull_shouldDefaultToAccept() {
42+
SonarResolveData data = new SonarResolveData(Set.of(RuleKey.of("java", "S123")), "reason", null);
43+
44+
assertThat(data.status()).isEqualTo(SonarResolveData.Status.ACCEPT);
45+
}
46+
47+
@Test
48+
public void constructor_whenStatusIsAccept_shouldKeepAccept() {
49+
SonarResolveData data = new SonarResolveData(Set.of(RuleKey.of("java", "S123")), "reason", SonarResolveData.Status.ACCEPT);
50+
51+
assertThat(data.status()).isEqualTo(SonarResolveData.Status.ACCEPT);
52+
}
53+
54+
@Test
55+
public void constructor_shouldSupportMultipleRuleKeys() {
56+
Set<RuleKey> ruleKeys = Set.of(RuleKey.of("java", "S123"), RuleKey.of("java", "S456"));
57+
SonarResolveData data = new SonarResolveData(ruleKeys, "comment", null);
58+
59+
assertThat(data.ruleKeys()).containsExactlyInAnyOrder(
60+
RuleKey.of("java", "S123"),
61+
RuleKey.of("java", "S456"));
62+
}
63+
}

0 commit comments

Comments
 (0)