Skip to content
This repository was archived by the owner on Aug 20, 2025. It is now read-only.

Commit e794ae1

Browse files
committed
Fix several sonar rule violations
1 parent 421b8fa commit e794ae1

File tree

5 files changed

+109
-70
lines changed

5 files changed

+109
-70
lines changed

core/src/main/java/dev/bannmann/mandor/core/Nodes.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ public static boolean areDifferent(Node a, Node b)
6363
return !areTheSame(a, b);
6464
}
6565

66-
@SuppressWarnings({ "ReferenceEquality", "BooleanMethodIsAlwaysInverted" })
66+
@SuppressWarnings({ "ReferenceEquality", "java:S1698" })
6767
@SuppressWarningsRationale(name = "ReferenceEquality",
68-
value = "This method is intended for navigation in the syntax tree in case reference equality is needed")
69-
@SuppressWarningsRationale(name = "BooleanMethodIsAlwaysInverted", value = "We want to offer both methods.")
68+
value = "For AST nodes, reference equality is perfectly fine, arguably even the only correct way")
69+
@SuppressWarningsRationale(name = "java:S1698",
70+
value = "For AST nodes, reference equality is perfectly fine, arguably even the only correct way")
7071
public static boolean areTheSame(Node a, Node b)
7172
{
7273
return a == b;
@@ -89,12 +90,10 @@ public static String getQualifiedName(AnnotationExpr annotation, RuleContext rul
8990
}
9091
}
9192

92-
@SuppressWarnings("ReferenceEquality")
93-
@SuppressWarningsRationale("For AST nodes, reference equality is perfectly fine, arguably even the only correct way")
9493
public static boolean isDirectChild(Node child, Node parent)
9594
{
9695
return child.getParentNode()
97-
.filter(node -> node == parent)
96+
.filter(isTheSameAs(parent))
9897
.isPresent();
9998
}
10099

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package dev.bannmann.mandor.core;
22

3+
import org.jspecify.annotations.Nullable;
4+
5+
import com.github.javaparser.ast.body.CallableDeclaration;
36
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
47
import com.github.javaparser.ast.body.ConstructorDeclaration;
58
import com.github.javaparser.ast.body.FieldDeclaration;
69
import com.github.javaparser.ast.body.MethodDeclaration;
10+
import com.github.javaparser.ast.stmt.BlockStmt;
711
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
812

913
public abstract class VisitorAdapter extends VoidVisitorAdapter<RuleContext>
@@ -23,37 +27,52 @@ public void visit(FieldDeclaration n, RuleContext ruleContext)
2327
@Override
2428
public void visit(ConstructorDeclaration n, RuleContext ruleContext)
2529
{
26-
ruleContext.trackSuppressibleScope(n, () -> {
27-
n.getModifiers().forEach(p -> p.accept(this, ruleContext));
28-
n.getName().accept(this, ruleContext);
29-
n.getParameters().forEach(p -> p.accept(this, ruleContext));
30-
n.getReceiverParameter().ifPresent(l -> l.accept(this, ruleContext));
31-
n.getThrownExceptions().forEach(p -> p.accept(this, ruleContext));
32-
n.getTypeParameters().forEach(p -> p.accept(this, ruleContext));
33-
n.getAnnotations().forEach(p -> p.accept(this, ruleContext));
34-
n.getComment().ifPresent(l -> l.accept(this, ruleContext));
35-
36-
// Visit the body last so that the subclass can make use of parameters
37-
n.getBody().accept(this, ruleContext);
38-
});
30+
ruleContext.trackSuppressibleScope(n, () -> visitChildren(n, n.getBody(), ruleContext));
31+
}
32+
33+
public <T extends CallableDeclaration<T>> void visitChildren(
34+
CallableDeclaration<T> n,
35+
@Nullable BlockStmt body,
36+
RuleContext ruleContext)
37+
{
38+
n.getModifiers()
39+
.forEach(p -> p.accept(this, ruleContext));
40+
41+
n.getName()
42+
.accept(this, ruleContext);
43+
44+
n.getParameters()
45+
.forEach(p -> p.accept(this, ruleContext));
46+
47+
n.getReceiverParameter()
48+
.ifPresent(l -> l.accept(this, ruleContext));
49+
50+
n.getThrownExceptions()
51+
.forEach(p -> p.accept(this, ruleContext));
52+
53+
n.getTypeParameters()
54+
.forEach(p -> p.accept(this, ruleContext));
55+
56+
n.getAnnotations()
57+
.forEach(p -> p.accept(this, ruleContext));
58+
59+
n.getComment()
60+
.ifPresent(l -> l.accept(this, ruleContext));
61+
62+
// Visit the body last so that the subclass can make use of parameters
63+
if (body != null)
64+
{
65+
body.accept(this, ruleContext);
66+
}
3967
}
4068

4169
@Override
4270
public void visit(MethodDeclaration n, RuleContext ruleContext)
4371
{
44-
ruleContext.trackSuppressibleScope(n, () -> {
45-
n.getType().accept(this, ruleContext);
46-
n.getModifiers().forEach(p -> p.accept(this, ruleContext));
47-
n.getName().accept(this, ruleContext);
48-
n.getParameters().forEach(p -> p.accept(this, ruleContext));
49-
n.getReceiverParameter().ifPresent(l -> l.accept(this, ruleContext));
50-
n.getThrownExceptions().forEach(p -> p.accept(this, ruleContext));
51-
n.getTypeParameters().forEach(p -> p.accept(this, ruleContext));
52-
n.getAnnotations().forEach(p -> p.accept(this, ruleContext));
53-
n.getComment().ifPresent(l -> l.accept(this, ruleContext));
54-
55-
// Visit the body last so that the subclass can make use of parameters
56-
n.getBody().ifPresent(l -> l.accept(this, ruleContext));
57-
});
72+
ruleContext.trackSuppressibleScope(n,
73+
() -> visitChildren(n,
74+
n.getBody()
75+
.orElse(null),
76+
ruleContext));
5877
}
5978
}

core/src/main/java/dev/bannmann/mandor/core/rules/OrphanedSuppressionRationale.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ private void validateSuppressionExists(AnnotationExpr rationaleAnnotation, RuleC
4545
return;
4646
}
4747

48-
var suppressedNames = SuppressionAnnotations.getSuppressedNames(suppressionAnnotationOptional.get(),
49-
ruleContext);
50-
5148
if (!(rationaleAnnotation instanceof NormalAnnotationExpr normalAnnotationExpr))
5249
{
5350
/*
@@ -71,6 +68,8 @@ private void validateSuppressionExists(AnnotationExpr rationaleAnnotation, RuleC
7168
return;
7269
}
7370

71+
var suppressedNames = SuppressionAnnotations.getSuppressedNames(suppressionAnnotationOptional.get(),
72+
ruleContext);
7473
String rationaleName = rationaleNameOptional.get();
7574
if (!suppressedNames.contains(rationaleName))
7675
{

core/src/main/java/dev/bannmann/mandor/core/rules/UtilityClassMemberMarkedStatic.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,42 @@ public void visit(ClassOrInterfaceDeclaration n, RuleContext ruleContext)
4444
public void visit(FieldDeclaration n, RuleContext ruleContext)
4545
{
4646
ruleContext.trackSuppressibleScope(n, () -> {
47-
if (active && n.isStatic())
48-
{
49-
for (VariableDeclarator declarator : n.getVariables())
50-
{
51-
ruleContext.addViolation("Field %s.%s should not be declared static in %s",
52-
className,
53-
declarator.getName(), ruleContext.getCodeLocation(n));
54-
}
55-
}
47+
processFieldDeclaration(n, ruleContext);
48+
5649
super.visit(n, ruleContext);
5750
});
5851
}
5952

60-
@Override
61-
public void visit(MethodDeclaration n, RuleContext ruleContext)
53+
public void processFieldDeclaration(FieldDeclaration n, RuleContext ruleContext)
6254
{
63-
ruleContext.trackSuppressibleScope(n, () -> {
64-
if (active && n.isStatic())
55+
if (active && n.isStatic())
56+
{
57+
for (VariableDeclarator declarator : n.getVariables())
6558
{
66-
ruleContext.addViolation("Method %s.%s() should not be declared static in %s",
59+
ruleContext.addViolation("Field %s.%s should not be declared static in %s",
6760
className,
68-
n.getNameAsString(),
61+
declarator.getName(),
6962
ruleContext.getCodeLocation(n));
7063
}
71-
super.visit(n, ruleContext);
72-
});
64+
}
65+
}
66+
67+
@Override
68+
public void visit(MethodDeclaration n, RuleContext ruleContext)
69+
{
70+
processMethodDeclaration(n, ruleContext);
71+
super.visit(n, ruleContext);
72+
}
73+
74+
public void processMethodDeclaration(MethodDeclaration n, RuleContext ruleContext)
75+
{
76+
if (active && n.isStatic())
77+
{
78+
ruleContext.addViolation("Method %s.%s() should not be declared static in %s",
79+
className,
80+
n.getNameAsString(),
81+
ruleContext.getCodeLocation(n));
82+
}
7383
}
7484
}
7585

core/src/main/java/dev/bannmann/mandor/core/rules/UtilityClassMemberWithoutStaticModifier.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,26 @@ public void visit(ClassOrInterfaceDeclaration n, RuleContext ruleContext)
5252
public void visit(FieldDeclaration n, RuleContext ruleContext)
5353
{
5454
ruleContext.trackSuppressibleScope(n, () -> {
55-
if (isViolation(n))
56-
{
57-
for (VariableDeclarator declarator : n.getVariables())
58-
{
59-
ruleContext.addViolation("Field %s.%s should be made static in %s",
60-
className,
61-
declarator.getName(),
62-
ruleContext.getCodeLocation(n));
63-
}
64-
}
55+
processFieldDeclaration(n, ruleContext);
56+
6557
super.visit(n, ruleContext);
6658
});
6759
}
6860

61+
public void processFieldDeclaration(FieldDeclaration fieldDeclaration, RuleContext ruleContext)
62+
{
63+
if (isViolation(fieldDeclaration))
64+
{
65+
for (VariableDeclarator declarator : fieldDeclaration.getVariables())
66+
{
67+
ruleContext.addViolation("Field %s.%s should be made static in %s",
68+
className,
69+
declarator.getName(),
70+
ruleContext.getCodeLocation(fieldDeclaration));
71+
}
72+
}
73+
}
74+
6975
public <N extends Node & NodeWithStaticModifier<N>> boolean isViolation(N n)
7076
{
7177
return active && !n.isStatic() && classDeclaration != null && Nodes.isDirectChild(n, classDeclaration);
@@ -75,16 +81,22 @@ public <N extends Node & NodeWithStaticModifier<N>> boolean isViolation(N n)
7581
public void visit(MethodDeclaration n, RuleContext ruleContext)
7682
{
7783
ruleContext.trackSuppressibleScope(n, () -> {
78-
if (isViolation(n))
79-
{
80-
ruleContext.addViolation("Method %s.%s() should be made static in %s",
81-
className,
82-
n.getNameAsString(),
83-
ruleContext.getCodeLocation(n));
84-
}
84+
processMethodDeclaration(n, ruleContext);
85+
8586
super.visit(n, ruleContext);
8687
});
8788
}
89+
90+
public void processMethodDeclaration(MethodDeclaration methodDeclaration, RuleContext ruleContext)
91+
{
92+
if (isViolation(methodDeclaration))
93+
{
94+
ruleContext.addViolation("Method %s.%s() should be made static in %s",
95+
className,
96+
methodDeclaration.getNameAsString(),
97+
ruleContext.getCodeLocation(methodDeclaration));
98+
}
99+
}
88100
}
89101

90102
@Override

0 commit comments

Comments
 (0)