Skip to content

Commit 5ecbf60

Browse files
committed
SONARJAVA-1299 ignores parameters annotated with @observes
1 parent 5a206e7 commit 5ecbf60

4 files changed

Lines changed: 26 additions & 5 deletions

File tree

java-checks/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<version>1.3</version>
163163
<type>jar</type>
164164
</artifactItem>
165+
<artifactItem>
166+
<groupId>javax</groupId>
167+
<artifactId>javaee-api</artifactId>
168+
<version>7.0</version>
169+
</artifactItem>
165170
</artifactItems>
166171
<outputDirectory>${project.build.directory}/test-jars</outputDirectory>
167172
</configuration>

java-checks/src/main/java/org/sonar/java/checks/unused/UnusedMethodParameterCheck.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.sonar.java.model.declaration.MethodTreeImpl;
3131
import org.sonar.java.tag.Tag;
3232
import org.sonar.plugins.java.api.JavaFileScannerContext;
33+
import org.sonar.plugins.java.api.semantic.Symbol;
3334
import org.sonar.plugins.java.api.tree.BlockTree;
3435
import org.sonar.plugins.java.api.tree.IdentifierTree;
3536
import org.sonar.plugins.java.api.tree.MethodTree;
@@ -53,6 +54,8 @@
5354
@SqaleConstantRemediation("5min")
5455
public class UnusedMethodParameterCheck extends SubscriptionBaseVisitor {
5556

57+
private static final String AUTHORIZED_ANNOTATION = "javax.enterprise.event.Observes";
58+
5659
@Override
5760
public List<Tree.Kind> nodesToVisit() {
5861
return ImmutableList.of(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR);
@@ -64,7 +67,8 @@ public void visitNode(Tree tree) {
6467
if (hasSemantic() && methodTree.block() != null && !isExcluded(methodTree)) {
6568
List<IdentifierTree> unused = Lists.newArrayList();
6669
for (VariableTree var : methodTree.parameters()) {
67-
if (var.symbol().usages().isEmpty()) {
70+
Symbol symbol = var.symbol();
71+
if (symbol.usages().isEmpty() && !symbol.metadata().isAnnotatedWith(AUTHORIZED_ANNOTATION)) {
6872
unused.add(var.simpleName());
6973
}
7074
}

java-checks/src/main/resources/org/sonar/l10n/java/rules/squid/S1172.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ <h2>Compliant Solution</h2>
1515
</pre>
1616
<h2>Exceptions</h2>
1717

18-
<p>Override and implementation methods are excluded, as are methods that are intended to be overridden.</p>
18+
<p>Override and implementation methods are excluded, as are parameters annotated with <code>@Observes</code>, and methods that are intended to be overridden.</p>
1919
<pre>
2020
@override
2121
void doSomething(int a, int b) { // no issue reported on b
@@ -37,7 +37,7 @@ <h2>Exceptions</h2>
3737
<h2>See</h2>
3838

3939
<ul>
40-
<li> MISRA C++:2008, 0-1-11</li>
41-
<li> MISRA C:2012, 2.7</li>
40+
<li> MISRA C++:2008, 0-1-11 - There shall be no unused parameters (named or unnamed) in nonvirtual functions.</li>
41+
<li> MISRA C:2012, 2.7 - There should be no unused parameters in functions</li>
4242
</ul>
4343

java-checks/src/test/files/checks/unused/UnusedMethodParameterCheck.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import javax.annotation.Nonnull;
2+
import javax.enterprise.event.Observes;
3+
14
class A extends B{
25
void doSomething(int a, int b) { // Noncompliant {{Remove this unused method parameter "b".}} [[sc=31;ec=32]]
36
compute(a);
@@ -34,7 +37,7 @@ void foo(int a) {
3437
}
3538

3639
class D extends C {
37-
void foo(int b, int a) { // Noncompliant {{Remove this unused method parameter "b".}} [[sc=16;ec=17;secondary=37]]
40+
void foo(int b, int a) { // Noncompliant {{Remove this unused method parameter "b".}} [[sc=16;ec=17;secondary=40]]
3841
System.out.println("");
3942
}
4043
}
@@ -97,5 +100,14 @@ private qiz(int arg1, int arg2) {
97100
public Supplier<String> parameterNotUsed(final Object o) {
98101
return o::toString;
99102
}
103+
}
100104

105+
class Annotations {
106+
public void foo(@Observes Object event, int arg2) { // Compliant
107+
System.out.println(arg2);
108+
}
109+
110+
public void bar(@Nonnull Object event, int arg2) { // Noncompliant {{Remove this unused method parameter "event".}} [[sc=35;ec=40]]
111+
System.out.println(arg2);
112+
}
101113
}

0 commit comments

Comments
 (0)