Skip to content

Commit a933906

Browse files
authored
Fix Spotbugs (#35313)
* Fix Spotbugs * Fix checkstyle
1 parent 297ba99 commit a933906

30 files changed

Lines changed: 68 additions & 22 deletions

File tree

.test-infra/mock-apis/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
applyJavaNature(
2424
exportJavadoc: false,
2525
publish: false,
26+
enableSpotbugs: false
2627
)
2728

2829
description = "Apache Beam :: Test Infra :: Mock APIs"

examples/java/src/main/java/org/apache/beam/examples/KafkaStreaming.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,16 @@ public void run() {
172172
// A class that randomly selects a name with random amount of points
173173
static class RandomUserScoreGeneratorFn extends DoFn<Object, KV<String, Integer>> {
174174
private static final int MAX_SCORE = 100;
175+
private static final Random RANDOM = new Random();
175176

176177
@ProcessElement
177178
public void processElement(ProcessContext c) {
178179
c.output(generate());
179180
}
180181

181182
public KV<String, Integer> generate() {
182-
Random random = new Random();
183-
String randomName = NAMES[random.nextInt(NAMES.length)];
184-
int randomScore = random.nextInt(MAX_SCORE) + 1;
183+
String randomName = NAMES[RANDOM.nextInt(NAMES.length)];
184+
int randomScore = RANDOM.nextInt(MAX_SCORE) + 1;
185185
return KV.of(randomName, randomScore);
186186
}
187187
}

examples/java/src/main/java/org/apache/beam/examples/complete/TrafficRoutes.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,12 @@ public static void main(String[] args) throws IOException {
405405

406406
private static Double tryParseAvgSpeed(String[] inputItems) {
407407
try {
408-
return Double.parseDouble(tryParseString(inputItems, 9));
409-
} catch (NumberFormatException | NullPointerException e) {
408+
String parsed = tryParseString(inputItems, 9);
409+
if (parsed == null) {
410+
return null;
411+
}
412+
return Double.parseDouble(parsed);
413+
} catch (NumberFormatException e) {
410414
return null;
411415
}
412416
}

examples/java/src/main/java/org/apache/beam/examples/complete/datatokenization/utils/SchemasUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument;
2222

2323
import com.google.api.services.bigquery.model.TableSchema;
24+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2425
import java.io.IOException;
2526
import java.io.InputStream;
2627
import java.io.Reader;
@@ -87,6 +88,7 @@ private void parseJson(String jsonSchema) throws UnsupportedOperationException {
8788
jsonBeamSchema = BigQueryHelpers.toJsonString(schema.getFields());
8889
}
8990

91+
@SuppressFBWarnings("DCN_NULLPOINTER_EXCEPTION")
9092
private void validateSchemaTypes(TableSchema bigQuerySchema) {
9193
try {
9294
beamSchema = fromTableSchema(bigQuerySchema);

examples/java/src/main/java/org/apache/beam/examples/cookbook/TriggerExample.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,16 @@ public static class InsertDelays extends DoFn<String, String> {
501501
// MIN_DELAY and MAX_DELAY in minutes.
502502
private static final int MIN_DELAY = 1;
503503
private static final int MAX_DELAY = 100;
504+
private transient Random random;
505+
506+
@Setup
507+
public void setup() {
508+
this.random = new Random();
509+
}
504510

505511
@ProcessElement
506512
public void processElement(ProcessContext c) throws Exception {
507513
Instant timestamp = Instant.now();
508-
Random random = new Random();
509514
if (random.nextDouble() < THRESHOLD) {
510515
int range = MAX_DELAY - MIN_DELAY;
511516
int delayInMinutes = random.nextInt(range) + MIN_DELAY;

examples/java/src/main/java/org/apache/beam/examples/subprocess/configuration/SubProcessConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
*/
1818
package org.apache.beam.examples.subprocess.configuration;
1919

20+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2021
import java.io.Serializable;
2122

2223
/**
2324
* Configuration file used to setup the Process kernel for execution of the external library Values
2425
* are copied from the Options to all them to be Serializable.
2526
*/
2627
@SuppressWarnings({"serial", "nullness"}) // TODO(https://github.com/apache/beam/issues/20497)
28+
@SuppressFBWarnings("PA_PUBLIC_PRIMITIVE_ATTRIBUTE") // TODO(#35312)
2729
public class SubProcessConfiguration implements Serializable {
2830

2931
// Source GCS directory where the C++ library is located gs://bucket/tests

examples/kotlin/src/main/java/org/apache/beam/examples/kotlin/cookbook/TriggerExample.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ object TriggerExample {
486486

487487
@ProcessElement
488488
@Throws(Exception::class)
489+
@SuppressFBWarnings("DMI_RANDOM_USED_ONLY_ONCE")
489490
fun processElement(c: ProcessContext) {
490491
var timestamp = Instant.now()
491492
val random = Random()

it/common/src/main/java/org/apache/beam/it/common/utils/ResourceManagerUtils.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ public static String generateResourceId(
9797
+ localDateTime.format(timeFormat);
9898
}
9999

100+
private static final Random RANDOM = new Random();
101+
100102
/** Generates random letter for padding. */
101103
public static char generatePadding() {
102-
Random random = new Random();
103-
return (char) ('a' + random.nextInt(26));
104+
return (char) ('a' + RANDOM.nextInt(26));
104105
}
105106

106107
/**
@@ -205,18 +206,15 @@ public static String generatePassword(
205206
RandomStringUtils.randomAlphanumeric(minLength, maxLength - numSpecial).toUpperCase());
206207
for (int i = 0; i < numSpecial && specialChars != null; i++) {
207208
password.insert(
208-
new Random().nextInt(password.length()),
209-
specialChars.get(new Random().nextInt(specialChars.size())));
209+
RANDOM.nextInt(password.length()), specialChars.get(RANDOM.nextInt(specialChars.size())));
210210
}
211211
for (int i = 0; i < numLower; i++) {
212212
password.insert(
213-
new Random().nextInt(password.length()),
214-
RandomStringUtils.randomAlphabetic(1).toLowerCase());
213+
RANDOM.nextInt(password.length()), RandomStringUtils.randomAlphabetic(1).toLowerCase());
215214
}
216215
for (int i = 0; i < numUpper; i++) {
217216
password.insert(
218-
new Random().nextInt(password.length()),
219-
RandomStringUtils.randomAlphabetic(1).toUpperCase());
217+
RANDOM.nextInt(password.length()), RandomStringUtils.randomAlphabetic(1).toUpperCase());
220218
}
221219
return password.toString();
222220
}

runners/core-java/src/main/java/org/apache/beam/runners/core/metrics/ExecutionStateSampler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull;
2121

22+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2223
import java.io.Closeable;
2324
import java.util.Set;
2425
import java.util.concurrent.CancellationException;
@@ -51,6 +52,7 @@ protected ExecutionStateSampler(MillisProvider clock) {
5152
this.clock = clock;
5253
}
5354

55+
@SuppressFBWarnings("MS_EXPOSE_REP") // singleton
5456
public static ExecutionStateSampler instance() {
5557
return INSTANCE;
5658
}

runners/flink/src/main/java/org/apache/beam/runners/flink/metrics/FileReporter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void open(MetricConfig config) {
6565
}
6666

6767
@Override
68+
@SuppressFBWarnings("DCN_NULLPOINTER_EXCEPTION")
6869
public void notifyOfRemovedMetric(Metric metric, String metricName, MetricGroup group) {
6970
final String name = group.getMetricIdentifier(metricName, this);
7071
super.notifyOfRemovedMetric(metric, metricName, group);

0 commit comments

Comments
 (0)