Skip to content

Commit 50333fc

Browse files
authored
SONARJAVA-4943 FP on S1144 if private method is referenced by name in annotations (#4776)
1 parent ec9cb36 commit 50333fc

2 files changed

Lines changed: 262 additions & 112 deletions

File tree

java-checks-test-sources/default/src/main/java/checks/UnusedPrivateMethod.java

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@interface Observes {}
1717

1818
class UnusedPrivateMethodCheck {
19-
19+
2020
private void init(@Observes Object object, String test) {} // Noncompliant
2121
private void init(@javax.enterprise.event.Observes Object object) {} //Compliant, javax.enterprise.event.Observes is an exception to the rule
2222
private void jakartaInit(@jakarta.enterprise.event.Observes Object object) {} //Compliant, jakarta.enterprise.event.Observes is an exception to the rule
@@ -234,3 +234,112 @@ public UnusedPrivateMethodCheckMyClass<B> build() {
234234
}
235235
}
236236
}
237+
238+
class CheckAnnotations {
239+
@interface ProxyMethod {
240+
public String value();
241+
}
242+
243+
@interface MethodProvided {
244+
public String value();
245+
}
246+
247+
@interface Getter {
248+
public String getterMethod();
249+
}
250+
251+
@interface Setter {
252+
public String method();
253+
}
254+
255+
@interface ArgumentIsNotAString {
256+
public int method();
257+
}
258+
259+
abstract static class MethodReferencedInAnnotation1 {
260+
261+
private void foo1() {} // Compliant
262+
263+
@ProxyMethod("foo1")
264+
abstract void bar1();
265+
}
266+
267+
@MethodProvided(value = "foo2")
268+
abstract static class MethodReferencedInAnnotation2 {
269+
270+
private void foo2() {} // Compliant
271+
272+
@ProxyMethod("foo2")
273+
abstract void bar2();
274+
275+
private void baz2() {} // Noncompliant
276+
}
277+
278+
static class MethodReferencedInAnnotation3 {
279+
280+
@Getter(getterMethod = "foo3")
281+
int bar3;
282+
283+
private int foo3() { // Compliant
284+
return 42;
285+
}
286+
}
287+
288+
static class MethodReferencedInAnnotation4 {
289+
290+
@Setter(method = "foo4")
291+
int bar4;
292+
293+
private void foo4(int value) {} // Compliant
294+
295+
private void bar4(int value) {} // Noncompliant
296+
}
297+
298+
static class MethodReferencedInAnnotation5 {
299+
300+
@Getter(getterMethod = "foo52")
301+
@Setter(method = "foo54")
302+
int bar5;
303+
304+
private void foo51(int value) {} // Noncompliant
305+
306+
private int foo52() { // Compliant
307+
return 42;
308+
}
309+
310+
private void foo53(int value) {} // Noncompliant
311+
312+
private void foo54(int value) {} // Compliant
313+
314+
private void foo55(int value) {} // Noncompliant
315+
}
316+
317+
abstract static class Coverage1 {
318+
319+
private void foo6() {} // Noncompliant, text blocks are ignored
320+
321+
@ProxyMethod("""
322+
foo6""")
323+
abstract void notStringLitNorAssignExpr();
324+
325+
@Getter(getterMethod = """
326+
foo6""")
327+
int assignExprWithNoStringLit;
328+
}
329+
330+
@ArgumentIsNotAString(method = 42)
331+
static class Coverage2 {
332+
private void foo7() {} // Noncompliant
333+
}
334+
335+
@MethodProvided(value = "foo8")
336+
static class TN {
337+
private void foo8() {} // Compliant, method is referenced in annotation
338+
339+
private void foo8(int param) {} // Compliant, method is referenced in annotation
340+
}
341+
342+
static class FN {
343+
private void foo8() {} // Noncompliant
344+
}
345+
}

0 commit comments

Comments
 (0)