Skip to content

Commit 78a6b1f

Browse files
SONARJAVA-6117 S8433 should not raise issue for classes without superclass declared (#5453)
1 parent 1df5586 commit 78a6b1f

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

java-checks-test-sources/default/src/main/files/non-compiling/checks/FlexibleConstructorBodyValidationCheckSample.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public LargeCoffee(int water, int milk) {
9494
public LargeCoffee(String name) {
9595
super(100, 100);
9696
this.name = name;
97-
if (!isValidName(name)) { // NonCompliant
97+
if (!isValidName(name)) { // Noncompliant
9898
throw new IllegalArgumentException();
9999
}
100100
}
@@ -166,4 +166,24 @@ public NestedIfCoffee(int water, int milk) {
166166
}
167167
}
168168
}
169+
170+
// Test when there is no superclass
171+
static class NoSuperclassCoffee {
172+
private int water;
173+
private int milk;
174+
private static final int MAX_SIZE = 500;
175+
176+
public NoSuperclassCoffee(int water, int milk) {
177+
if (water + milk > MAX_SIZE) { // Compliant: no superclass and no this(...) call
178+
throw new IllegalArgumentException();
179+
}
180+
}
181+
182+
public NoSuperclassCoffee(int water) {
183+
this(water, 0);
184+
if (water <= 0) { // Noncompliant {{Move this validation logic before the super() or this() call.}}
185+
throw new IllegalArgumentException();
186+
}
187+
}
188+
}
169189
}

java-checks/src/main/java/org/sonar/java/checks/FlexibleConstructorBodyValidationCheck.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.sonar.plugins.java.api.JavaVersionAwareVisitor;
2929
import org.sonar.plugins.java.api.semantic.MethodMatchers;
3030
import org.sonar.plugins.java.api.semantic.Symbol;
31+
import org.sonar.plugins.java.api.semantic.Type;
3132
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
3233
import org.sonar.plugins.java.api.tree.BlockTree;
3334
import org.sonar.plugins.java.api.tree.ExpressionStatementTree;
@@ -90,8 +91,9 @@ public void visitNode(Tree tree) {
9091

9192
// Get statements after the constructor call
9293
List<StatementTree> statements = body.body();
93-
if (constructorCallIndex == statements.size() - 1) {
94-
// No statements after constructor call
94+
if (constructorCallIndex == statements.size() - 1
95+
|| (constructorCallIndex == -1 && hasNoExplicitSuperClass(constructor))) {
96+
// No statements after constructor call or no superclass and no constructor call
9597
return;
9698
}
9799

@@ -129,6 +131,11 @@ private static int findConstructorCallIndex(BlockTree body) {
129131
return -1;
130132
}
131133

134+
private static boolean hasNoExplicitSuperClass(MethodTree constructor) {
135+
Type superClass = constructor.symbol().enclosingClass().superClass();
136+
return (superClass == null || superClass.is("java.lang.Object"));
137+
}
138+
132139
/**
133140
* Check if a statement is a validation statement (conditionally throws exception or calls validation method).
134141
*

0 commit comments

Comments
 (0)