Skip to content

Reuse Random instances across test benchmark classes#56

Open
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260627-180118-86dea59b
Open

Reuse Random instances across test benchmark classes#56
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20260627-180118-86dea59b

Conversation

@sonarqube-agent

Copy link
Copy Markdown

This PR was automatically created by the Remediation Agent's Scheduled backlog remediation feature.

Why these issues? java:S2119 (Save and re-use Random) is a high-frequency, high-severity rule affecting 40+ files. These five issues represent the most impactful subset with a consistent, automatable pattern: consolidating Random instantiations from method scope to class-level fields that can be safely reused across invocations.

Fixed 5 critical SonarQube issues (java:S2119) by converting repeated Random instantiations into reusable class-level fields. Creating a new Random object on each method call is inefficient and can produce weak randomness; reusing a single instance per class improves both performance and random number quality.

View Project in SonarCloud


Fixed Issues

java:S2119 - Save and re-use this "Random". • CRITICALView issue

Location: src/main/java/org/owasp/benchmark/testcode/BenchmarkTest00023.java:47

Why is this an issue?

Creating a new Random object each time a random value is needed is inefficient and may produce numbers that are not random, depending on the JDK. For better efficiency and randomness, create a single Random, store it, and reuse it.

What changed

This hunk adds a private field random of type java.util.Random to the class, so that a single Random instance is created once and reused across method invocations. This addresses the inefficiency and potential randomness issue caused by creating a new Random object each time a method is called.

--- a/src/main/java/org/owasp/benchmark/testcode/BenchmarkTest00023.java
+++ b/src/main/java/org/owasp/benchmark/testcode/BenchmarkTest00023.java
@@ -30,0 +31,1 @@ public class BenchmarkTest00023 extends HttpServlet {
+    private java.util.Random random = new java.util.Random();
java:S2119 - Save and re-use this "Random". • CRITICALView issue

Location: src/main/java/org/owasp/benchmark/testcode/BenchmarkTest00399.java:69

Why is this an issue?

Creating a new Random object each time a random value is needed is inefficient and may produce numbers that are not random, depending on the JDK. For better efficiency and randomness, create a single Random, store it, and reuse it.

What changed

This hunk declares a private static Random field at the class level, storing a single reusable instance of java.util.Random. This addresses the inefficiency and potential randomness issue caused by creating a new Random object each time a method is invoked. By defining the Random instance as a class-level field, it can be reused across method calls instead of being recreated every time.

--- a/src/main/java/org/owasp/benchmark/testcode/BenchmarkTest00399.java
+++ b/src/main/java/org/owasp/benchmark/testcode/BenchmarkTest00399.java
@@ -30,0 +31,1 @@ public class BenchmarkTest00399 extends HttpServlet {
+    private static java.util.Random random = new java.util.Random();
java:S2119 - Save and re-use this "Random". • CRITICALView issue

Location: src/main/java/org/owasp/benchmark/testcode/BenchmarkTest01060.java:53

Why is this an issue?

Creating a new Random object each time a random value is needed is inefficient and may produce numbers that are not random, depending on the JDK. For better efficiency and randomness, create a single Random, store it, and reuse it.

What changed

This hunk declares a private static Random field at the class level, storing a single reusable instance of java.util.Random. This addresses the inefficiency and potential randomness issue caused by creating a new Random object each time a method is invoked. By defining the Random instance as a class-level field, it can be reused across method calls instead of being recreated every time.

--- a/src/main/java/org/owasp/benchmark/testcode/BenchmarkTest01060.java
+++ b/src/main/java/org/owasp/benchmark/testcode/BenchmarkTest01060.java
@@ -30,0 +31,1 @@ public class BenchmarkTest01060 extends HttpServlet {
+    private static java.util.Random random = new java.util.Random();
java:S2119 - Save and re-use this "Random". • CRITICALView issue

Location: src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02140.java:48

Why is this an issue?

Creating a new Random object each time a random value is needed is inefficient and may produce numbers that are not random, depending on the JDK. For better efficiency and randomness, create a single Random, store it, and reuse it.

What changed

This hunk declares a private static final Random field at the class level, storing a single reusable instance of java.util.Random. This addresses the inefficiency and potential randomness issue caused by creating a new Random object each time a method is invoked. By defining the Random instance as a class-level field, it can be reused across method calls rather than being recreated every time.

--- a/src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02140.java
+++ b/src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02140.java
@@ -30,0 +31,1 @@ public class BenchmarkTest02140 extends HttpServlet {
+    private static final java.util.Random random = new java.util.Random();
java:S2119 - Save and re-use this "Random". • CRITICALView issue

Location: src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02503.java:50

Why is this an issue?

Creating a new Random object each time a random value is needed is inefficient and may produce numbers that are not random, depending on the JDK. For better efficiency and randomness, create a single Random, store it, and reuse it.

What changed

This hunk declares a private static final Random field at the class level, storing a single reusable instance of java.util.Random. This addresses the inefficiency and potential randomness issue caused by creating a new Random object each time a method is invoked. By defining the Random instance as a class-level field, it can be reused across method calls rather than being recreated every time.

--- a/src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02503.java
+++ b/src/main/java/org/owasp/benchmark/testcode/BenchmarkTest02503.java
@@ -30,0 +31,1 @@ public class BenchmarkTest02503 extends HttpServlet {
+    private static final java.util.Random random = new java.util.Random();

Have a suggestion or found an issue? Share your feedback here.


SonarQube Remediation Agent uses AI. Check for mistakes.

Fixed issues:
- AZ3eW0KvPF-XYsB0KnQP for java:S2119 rule
- AZ3eW0GIPF-XYsB0KnPS for java:S2119 rule
- AZ3eW0bzPF-XYsB0KnUv for java:S2119 rule
- AZ3eW0CBPF-XYsB0KnN_ for java:S2119 rule
- AZ3eW0DHPF-XYsB0KnOY for java:S2119 rule

Generated by SonarQube Agent (task: 76b45024-70cf-4905-a430-7d2371c1a97f)
@sonarqube-agent

Copy link
Copy Markdown
Author

⚠️ This repository does not have a CODEOWNERS file. The PR has been created but has not been automatically assigned to any reviewer. To ensure PRs are reviewed promptly, consider adding a CODEOWNERS file to your repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant