Skip to content

Commit 676c998

Browse files
authored
Enable StringCharset Error Prone Check (#37767)
1 parent b42c992 commit 676c998

6 files changed

Lines changed: 39 additions & 31 deletions

File tree

buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,6 @@ class BeamModulePlugin implements Plugin<Project> {
15781578
"NarrowCalculation",
15791579
"NullableTypeParameter",
15801580
"NullableWildcard",
1581-
"StringCharset",
15821581
"SuperCallToObjectMethod",
15831582
"UnnecessaryLongToIntConversion",
15841583
"UnusedVariable",

examples/java/src/main/java/org/apache/beam/examples/complete/game/injector/Injector.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package org.apache.beam.examples.complete.game.injector;
1919

20+
import static java.nio.charset.StandardCharsets.UTF_8;
21+
2022
import com.google.api.services.pubsub.Pubsub;
2123
import com.google.api.services.pubsub.model.PublishRequest;
2224
import com.google.api.services.pubsub.model.PubsubMessage;
@@ -327,7 +329,7 @@ public static void publishData(int numMessages, int delayInMillis) throws IOExce
327329
for (int i = 0; i < Math.max(1, numMessages); i++) {
328330
Long currTime = System.currentTimeMillis();
329331
String message = generateEvent(currTime, delayInMillis);
330-
PubsubMessage pubsubMessage = new PubsubMessage().encodeData(message.getBytes("UTF-8"));
332+
PubsubMessage pubsubMessage = new PubsubMessage().encodeData(message.getBytes(UTF_8));
331333
pubsubMessage.setAttributes(
332334
ImmutableMap.of(
333335
GameConstants.TIMESTAMP_ATTRIBUTE,
@@ -350,7 +352,7 @@ public static void publishDataToFile(String fileName, int numMessages, int delay
350352
PrintWriter out =
351353
new PrintWriter(
352354
new OutputStreamWriter(
353-
new BufferedOutputStream(new FileOutputStream(fileName, true)), "UTF-8"));
355+
new BufferedOutputStream(new FileOutputStream(fileName, true)), UTF_8));
354356

355357
try {
356358
for (int i = 0; i < Math.max(1, numMessages); i++) {

sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/JsonSchemaConversionTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.beam.sdk.schemas;
1919

20+
import static java.nio.charset.StandardCharsets.UTF_8;
2021
import static org.hamcrest.MatcherAssert.assertThat;
2122
import static org.hamcrest.Matchers.containsInAnyOrder;
2223
import static org.hamcrest.Matchers.containsString;
@@ -189,7 +190,7 @@ public void testBeamSchemaToJsonSchemaValidationFailsWithNonNullProperty() {
189190
public void testBasicJsonSchemaToBeamSchema() throws IOException {
190191
try (InputStream inputStream =
191192
getClass().getResourceAsStream("/json-schema/basic_json_schema.json")) {
192-
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), "UTF-8");
193+
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), UTF_8);
193194
Schema parsedSchema = JsonUtils.beamSchemaFromJsonSchema(stringJsonSchema);
194195

195196
assertThat(
@@ -209,7 +210,7 @@ public void testBasicJsonSchemaToBeamSchema() throws IOException {
209210
public void testNestedStructsJsonSchemaToBeamSchema() throws IOException {
210211
try (InputStream inputStream =
211212
getClass().getResourceAsStream("/json-schema/nested_arrays_objects_json_schema.json")) {
212-
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), "UTF-8");
213+
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), UTF_8);
213214
Schema parsedSchema = JsonUtils.beamSchemaFromJsonSchema(stringJsonSchema);
214215

215216
assertThat(parsedSchema.getFieldNames(), containsInAnyOrder("fruits", "vegetables"));
@@ -230,7 +231,7 @@ public void testNestedStructsJsonSchemaToBeamSchema() throws IOException {
230231
public void testArrayNestedArrayObjectJsonSchemaToBeamSchema() throws IOException {
231232
try (InputStream inputStream =
232233
getClass().getResourceAsStream("/json-schema/array_nested_array_json_schema.json")) {
233-
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), "UTF-8");
234+
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), UTF_8);
234235
Schema parsedSchema = JsonUtils.beamSchemaFromJsonSchema(stringJsonSchema);
235236

236237
assertThat(parsedSchema.getFieldNames(), containsInAnyOrder("complexMatrix"));
@@ -252,7 +253,7 @@ public void testObjectNestedObjectArrayJsonSchemaToBeamSchema() throws IOExcepti
252253
try (InputStream inputStream =
253254
getClass()
254255
.getResourceAsStream("/json-schema/object_nested_object_and_array_json_schema.json")) {
255-
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), "UTF-8");
256+
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), UTF_8);
256257
Schema parsedSchema = JsonUtils.beamSchemaFromJsonSchema(stringJsonSchema);
257258

258259
assertThat(parsedSchema.getFieldNames(), containsInAnyOrder("classroom"));
@@ -285,7 +286,7 @@ public void testObjectNestedObjectArrayJsonSchemaToBeamSchema() throws IOExcepti
285286
public void testArrayWithNestedRefsBeamSchema() throws IOException {
286287
try (InputStream inputStream =
287288
getClass().getResourceAsStream("/json-schema/ref_with_ref_json_schema.json")) {
288-
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), "UTF-8");
289+
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), UTF_8);
289290
Schema parsedSchema = JsonUtils.beamSchemaFromJsonSchema(stringJsonSchema);
290291

291292
assertEquals("vegetables", Iterables.getOnlyElement(parsedSchema.getFieldNames()));
@@ -327,7 +328,7 @@ public void testArrayWithNestedRefsBeamSchema() throws IOException {
327328
public void testUnsupportedTupleArrays() throws IOException {
328329
try (InputStream inputStream =
329330
getClass().getResourceAsStream("/json-schema/unsupported_tuple_arrays.json")) {
330-
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), "UTF-8");
331+
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), UTF_8);
331332

332333
IllegalArgumentException thrownException =
333334
assertThrows(
@@ -347,7 +348,7 @@ public void testUnsupportedTupleArrays() throws IOException {
347348
public void testUnsupportedNestedTupleArrays() throws IOException {
348349
try (InputStream inputStream =
349350
getClass().getResourceAsStream("/json-schema/unsupported_nested_tuple_array.json")) {
350-
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), "UTF-8");
351+
String stringJsonSchema = new String(ByteStreams.toByteArray(inputStream), UTF_8);
351352

352353
IllegalArgumentException thrownException =
353354
assertThrows(

sdks/java/io/kafka/src/test/java/org/apache/beam/sdk/io/kafka/KafkaDlqTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package org.apache.beam.sdk.io.kafka;
1919

20+
import static java.nio.charset.StandardCharsets.UTF_8;
21+
2022
import java.util.Arrays;
2123
import java.util.Collections;
2224
import java.util.List;
@@ -69,9 +71,9 @@ public void testKafkaErrorFnSuccess() throws Exception {
6971
try {
7072
messages =
7173
Arrays.asList(
72-
"{\"name\":\"a\"}".getBytes("UTF8"),
73-
"{\"name\":\"b\"}".getBytes("UTF8"),
74-
"{\"name\":\"c\"}".getBytes("UTF8"));
74+
"{\"name\":\"a\"}".getBytes(UTF_8),
75+
"{\"name\":\"b\"}".getBytes(UTF_8),
76+
"{\"name\":\"c\"}".getBytes(UTF_8));
7577
} catch (Exception e) {
7678
}
7779
PCollection<byte[]> input = p.apply(Create.of(messages));
@@ -93,9 +95,9 @@ public void testKafkaErrorFnFailure() throws Exception {
9395
try {
9496
messagesWithError =
9597
Arrays.asList(
96-
"{\"error\":\"a\"}".getBytes("UTF8"),
97-
"{\"error\":\"b\"}".getBytes("UTF8"),
98-
"{\"error\":\"c\"}".getBytes("UTF8"));
98+
"{\"error\":\"a\"}".getBytes(UTF_8),
99+
"{\"error\":\"b\"}".getBytes(UTF_8),
100+
"{\"error\":\"c\"}".getBytes(UTF_8));
99101
} catch (Exception e) {
100102
}
101103
PCollection<byte[]> input = p.apply(Create.of(messagesWithError));

sdks/java/io/kafka/src/test/java/org/apache/beam/sdk/io/kafka/KafkaIOExternalTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,13 @@ public void testKafkaRecordToExternalKafkaRecord() throws Exception {
185185
assertEquals(12345, byteArrayKafkaRecord.timestamp);
186186
assertEquals(KafkaTimestampType.LOG_APPEND_TIME.id, byteArrayKafkaRecord.timestampTypeId);
187187
assertEquals(KafkaTimestampType.LOG_APPEND_TIME.name, byteArrayKafkaRecord.timestampTypeName);
188-
assertEquals("dummyKey", new String(byteArrayKafkaRecord.key, "UTF-8"));
189-
assertEquals("dummyValue", new String(byteArrayKafkaRecord.value, "UTF-8"));
188+
assertEquals("dummyKey", new String(byteArrayKafkaRecord.key, StandardCharsets.UTF_8));
189+
assertEquals("dummyValue", new String(byteArrayKafkaRecord.value, StandardCharsets.UTF_8));
190190
assertEquals(1, byteArrayKafkaRecord.headers.size());
191191
assertEquals("dummyHeaderKey", byteArrayKafkaRecord.headers.get(0).key);
192-
assertEquals("dummyHeaderVal", new String(byteArrayKafkaRecord.headers.get(0).value, "UTF-8"));
192+
assertEquals(
193+
"dummyHeaderVal",
194+
new String(byteArrayKafkaRecord.headers.get(0).value, StandardCharsets.UTF_8));
193195
}
194196

195197
@Test
@@ -212,7 +214,9 @@ public void testKafkaRecordToExternalKafkaRecordWithNullKeyAndValue() throws Exc
212214
assertNull(byteArrayKafkaRecord.value);
213215
assertEquals(1, byteArrayKafkaRecord.headers.size());
214216
assertEquals("dummyHeaderKey", byteArrayKafkaRecord.headers.get(0).key);
215-
assertEquals("dummyHeaderVal", new String(byteArrayKafkaRecord.headers.get(0).value, "UTF-8"));
217+
assertEquals(
218+
"dummyHeaderVal",
219+
new String(byteArrayKafkaRecord.headers.get(0).value, StandardCharsets.UTF_8));
216220
}
217221

218222
@Test

sdks/java/io/kafka/src/test/java/org/apache/beam/sdk/io/kafka/KafkaWriteSchemaTransformProviderTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
*/
1818
package org.apache.beam.sdk.io.kafka;
1919

20+
import static java.nio.charset.StandardCharsets.UTF_8;
2021
import static org.apache.beam.sdk.io.kafka.KafkaWriteSchemaTransformProvider.getRowToRawBytesFunction;
2122
import static org.junit.Assert.assertEquals;
2223

23-
import java.io.UnsupportedEncodingException;
2424
import java.util.Arrays;
2525
import java.util.Collections;
2626
import java.util.List;
@@ -114,15 +114,15 @@ public class KafkaWriteSchemaTransformProviderTest {
114114
RAW_ROWS =
115115
Arrays.asList(
116116
Row.withSchema(BEAM_RAW_SCHEMA)
117-
.withFieldValue("payload", "a".getBytes("UTF8"))
117+
.withFieldValue("payload", "a".getBytes(UTF_8))
118118
.build(),
119119
Row.withSchema(BEAM_RAW_SCHEMA)
120-
.withFieldValue("payload", "b".getBytes("UTF8"))
120+
.withFieldValue("payload", "b".getBytes(UTF_8))
121121
.build(),
122122
Row.withSchema(BEAM_RAW_SCHEMA)
123-
.withFieldValue("payload", "c".getBytes("UTF8"))
123+
.withFieldValue("payload", "c".getBytes(UTF_8))
124124
.build());
125-
} catch (UnsupportedEncodingException e) {
125+
} catch (Exception e) {
126126
throw new RuntimeException(e);
127127
}
128128
}
@@ -146,9 +146,9 @@ public class KafkaWriteSchemaTransformProviderTest {
146146
public void testKafkaErrorFnSuccess() throws Exception {
147147
List<KV<byte[], byte[]>> msg =
148148
Arrays.asList(
149-
KV.of(new byte[1], "{\"name\":\"a\"}".getBytes("UTF8")),
150-
KV.of(new byte[1], "{\"name\":\"b\"}".getBytes("UTF8")),
151-
KV.of(new byte[1], "{\"name\":\"c\"}".getBytes("UTF8")));
149+
KV.of(new byte[1], "{\"name\":\"a\"}".getBytes(UTF_8)),
150+
KV.of(new byte[1], "{\"name\":\"b\"}".getBytes(UTF_8)),
151+
KV.of(new byte[1], "{\"name\":\"c\"}".getBytes(UTF_8)));
152152

153153
PCollection<Row> input = p.apply(Create.of(ROWS));
154154
Schema errorSchema = ErrorHandling.errorSchema(BEAMSCHEMA);
@@ -168,9 +168,9 @@ public void testKafkaErrorFnSuccess() throws Exception {
168168
public void testKafkaErrorFnRawSuccess() throws Exception {
169169
List<KV<byte[], byte[]>> msg =
170170
Arrays.asList(
171-
KV.of(new byte[1], "a".getBytes("UTF8")),
172-
KV.of(new byte[1], "b".getBytes("UTF8")),
173-
KV.of(new byte[1], "c".getBytes("UTF8")));
171+
KV.of(new byte[1], "a".getBytes(UTF_8)),
172+
KV.of(new byte[1], "b".getBytes(UTF_8)),
173+
KV.of(new byte[1], "c".getBytes(UTF_8)));
174174

175175
PCollection<Row> input = p.apply(Create.of(RAW_ROWS));
176176
Schema errorSchema = ErrorHandling.errorSchema(BEAM_RAW_SCHEMA);

0 commit comments

Comments
 (0)