Skip to content

Commit 737c864

Browse files
committed
SONARJAVA-5123 S2119: extend rule to be applied for all Random subclasses
1 parent d2d23f7 commit 737c864

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package checks;
22

3+
import org.apache.commons.lang.math.JVMRandom;
4+
5+
import java.security.SecureRandom;
36
import java.util.Random;
47

58
public class ReuseRandomCheckSample {
@@ -18,9 +21,13 @@ void func(long seed, Random param) {
1821
Random localVar2 = new Random(seed); // Compliant for Random(long seed)
1922
Object localVar3 = new Object();
2023

24+
SecureRandom secureRandom = new SecureRandom(); // Noncompliant {{Save and re-use this "Random".}}
25+
JVMRandom jvmRandom = new JVMRandom(); // Noncompliant {{Save and re-use this "Random".}}
26+
2127
staticField = new Random();
2228
field = new Random();
2329
this.field = new Random();
30+
field = new SecureRandom();
2431

2532
field = localVar1 = new Random();
2633
field = (localVar1 = new Random());
@@ -37,6 +44,7 @@ void func(long seed, Random param) {
3744
int usedDirectly = new Random().nextInt(); // Noncompliant
3845
// ^^^^^^
3946
(new Random()).nextInt(); // Noncompliant
47+
(new SecureRandom()).nextInt(); // Noncompliant
4048
}
4149

4250
public static void main(String[] args) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public List<Kind> nodesToVisit() {
4343
@Override
4444
protected MethodMatchers getMethodInvocationMatchers() {
4545
return MethodMatchers.create()
46-
.ofTypes("java.util.Random").constructor().addWithoutParametersMatcher().build();
46+
.ofSubTypes("java.util.Random").constructor().addWithoutParametersMatcher().build();
4747
}
4848

4949
@Override

0 commit comments

Comments
 (0)