Skip to content

Commit a6aa88b

Browse files
authored
Resolve DefaultCharset from Error Prone (#6177)
This commit also changes to handle BadImport, DefaultCharset, and MissingOverride as errors. See gh-6143 See gh-6154 Signed-off-by: Johnny Lim <[email protected]>
1 parent b75795b commit a6aa88b

File tree

13 files changed

+46
-29
lines changed

13 files changed

+46
-29
lines changed

build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ subprojects {
7171
tasks.withType(JavaCompile).configureEach {
7272
options.errorprone.disableWarningsInGeneratedCode = true
7373
options.errorprone.excludedPaths = ".*/build/generated/.*"
74-
options.errorprone.error("StringCaseLocaleUsage")
74+
options.errorprone.error(
75+
"BadImport",
76+
"DefaultCharset",
77+
"MissingOverride",
78+
"StringCaseLocaleUsage"
79+
)
7580

7681
if (!javaLanguageVersion.canCompileOrRun(17)) {
7782
// Error Prone does not work with JDK <17

docs/src/test/java/io/micrometer/docs/observation/messaging/ObservationMessagingIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public KafkaReceiverContext(ConsumerRecords<String, String> consumerRecord) {
179179
// This is a very naive approach that takes the first ConsumerRecord
180180
Header header = carrier.iterator().next().headers().lastHeader(key);
181181
if (header != null) {
182-
return new String(header.value());
182+
return new String(header.value(), StandardCharsets.UTF_8);
183183
}
184184
return null;
185185
});

implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/DynatraceConfigTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.junit.jupiter.api.Test;
2424

2525
import java.io.IOException;
26+
import java.nio.charset.StandardCharsets;
2627
import java.nio.file.Files;
2728
import java.nio.file.Path;
2829
import java.time.Duration;
@@ -311,7 +312,7 @@ void testFileBasedConfig() throws IOException {
311312

312313
Files.write(tempFile, ("DT_METRICS_INGEST_URL = https://your-dynatrace-ingest-url/api/v2/metrics/ingest\n"
313314
+ "DT_METRICS_INGEST_API_TOKEN = YOUR.DYNATRACE.TOKEN")
314-
.getBytes());
315+
.getBytes(StandardCharsets.UTF_8));
315316

316317
DynatraceFileBasedConfigurationProvider.getInstance()
317318
.forceOverwriteConfig(tempFile.toString(), Duration.ofMillis(50));
@@ -333,7 +334,7 @@ public DynatraceApiVersion apiVersion() {
333334

334335
Files.write(tempFile, ("DT_METRICS_INGEST_URL = https://a-different-url/api/v2/metrics/ingest\n"
335336
+ "DT_METRICS_INGEST_API_TOKEN = A.DIFFERENT.TOKEN")
336-
.getBytes());
337+
.getBytes(StandardCharsets.UTF_8));
337338

338339
await().atMost(1_000, MILLISECONDS).until(() -> config.apiToken().equals("A.DIFFERENT.TOKEN"));
339340
assertThat(config.uri()).isEqualTo("https://a-different-url/api/v2/metrics/ingest");

implementations/micrometer-registry-dynatrace/src/test/java/io/micrometer/dynatrace/v2/DynatraceExporterV2Test.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.junit.jupiter.api.Test;
3333
import org.mockito.ArgumentCaptor;
3434

35+
import java.nio.charset.StandardCharsets;
3536
import java.nio.file.Files;
3637
import java.nio.file.Path;
3738
import java.time.Duration;
@@ -760,9 +761,11 @@ public String get(String key) {
760761
final String firstUri = baseUri + "first";
761762
final String secondUri = baseUri + "second";
762763

763-
Files.write(tempFile, ("DT_METRICS_INGEST_URL = " + firstUri + "\n"
764-
+ "DT_METRICS_INGEST_API_TOKEN = YOUR.DYNATRACE.TOKEN.FIRST")
765-
.getBytes());
764+
Files
765+
.write(tempFile,
766+
("DT_METRICS_INGEST_URL = " + firstUri + "\n"
767+
+ "DT_METRICS_INGEST_API_TOKEN = YOUR.DYNATRACE.TOKEN.FIRST")
768+
.getBytes(StandardCharsets.UTF_8));
766769

767770
DynatraceFileBasedConfigurationProvider.getInstance()
768771
.forceOverwriteConfig(tempFile.toString(), Duration.ofMillis(50));
@@ -786,9 +789,10 @@ public String get(String key) {
786789
clock.add(config.step());
787790

788791
// overwrite the file content to use the second uri
789-
Files.write(tempFile, ("DT_METRICS_INGEST_URL = " + secondUri + "\n"
790-
+ "DT_METRICS_INGEST_API_TOKEN = YOUR.DYNATRACE.TOKEN.SECOND")
791-
.getBytes());
792+
Files.write(tempFile,
793+
("DT_METRICS_INGEST_URL = " + secondUri + "\n"
794+
+ "DT_METRICS_INGEST_API_TOKEN = YOUR.DYNATRACE.TOKEN.SECOND")
795+
.getBytes(StandardCharsets.UTF_8));
792796

793797
await().atMost(1_000, MILLISECONDS).until(() -> config.uri().equals(secondUri));
794798
exporter.export(Collections.singletonList(counter));

implementations/micrometer-registry-new-relic/src/test/java/io/micrometer/newrelic/NewRelicMeterRegistryTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.micrometer.newrelic.NewRelicMeterRegistryTest.MockNewRelicAgent.MockNewRelicInsights;
2525
import org.junit.jupiter.api.Test;
2626

27+
import java.nio.charset.StandardCharsets;
2728
import java.util.Arrays;
2829
import java.util.HashMap;
2930
import java.util.List;
@@ -612,7 +613,7 @@ void sendEventsWithApiProvider() {
612613

613614
apiProvider.sendEvents(apiProvider.writeGauge(gauge));
614615

615-
assertThat(new String(mockHttpClient.getRequest().getEntity())).contains(
616+
assertThat(new String(mockHttpClient.getRequest().getEntity(), StandardCharsets.UTF_8)).contains(
616617
"{\"eventType\":\"MicrometerSample\",\"value\":1,\"metricName\":\"myGauge\",\"metricType\":\"GAUGE\"}");
617618

618619
// test meterNameEventTypeEnabledConfig = true
@@ -624,7 +625,7 @@ void sendEventsWithApiProvider() {
624625

625626
apiProvider.sendEvents(apiProvider.writeGauge(gauge));
626627

627-
assertThat(new String(mockHttpClient.getRequest().getEntity()))
628+
assertThat(new String(mockHttpClient.getRequest().getEntity(), StandardCharsets.UTF_8))
628629
.contains("{\"eventType\":\"myGauge2\",\"value\":1}");
629630
}
630631

@@ -679,7 +680,7 @@ void publishWithApiClientProvider() {
679680
registry.publish();
680681

681682
// should send a batch of multiple in one json payload
682-
assertThat(new String(mockHttpClient.getRequest().getEntity())).contains(
683+
assertThat(new String(mockHttpClient.getRequest().getEntity(), StandardCharsets.UTF_8)).contains(
683684
"[{\"eventType\":\"MicrometerSample\",\"value\":2,\"metricName\":\"otherGauge\",\"metricType\":\"GAUGE\"},"
684685
+ "{\"eventType\":\"MicrometerSample\",\"value\":1,\"metricName\":\"myGauge\",\"metricType\":\"GAUGE\",\"theTag\":\"theValue\"}]");
685686
}

implementations/micrometer-registry-prometheus/src/main/java/io/micrometer/prometheusmetrics/PrometheusMeterRegistry.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.io.ByteArrayOutputStream;
4242
import java.io.IOException;
4343
import java.io.OutputStream;
44+
import java.nio.charset.StandardCharsets;
4445
import java.util.ArrayList;
4546
import java.util.List;
4647
import java.util.Set;
@@ -133,7 +134,7 @@ public String scrape(String contentType) {
133134
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
134135
try {
135136
scrape(outputStream, contentType);
136-
return outputStream.toString();
137+
return outputStream.toString(StandardCharsets.UTF_8.name());
137138
}
138139
catch (IOException e) {
139140
// This should not happen during writing a ByteArrayOutputStream
@@ -179,7 +180,7 @@ public String scrape(String contentType, @Nullable Set<String> includedNames) {
179180
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
180181
try {
181182
scrape(outputStream, contentType, includedNames);
182-
return outputStream.toString();
183+
return outputStream.toString(StandardCharsets.UTF_8.name());
183184
}
184185
catch (IOException e) {
185186
// This should not happen during writing a ByteArrayOutputStream

implementations/micrometer-registry-prometheus/src/test/java/io/micrometer/prometheusmetrics/PrometheusMeterRegistryIntegrationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.io.IOException;
3636
import java.io.OutputStream;
3737
import java.net.InetSocketAddress;
38+
import java.nio.charset.StandardCharsets;
3839
import java.time.Duration;
3940
import java.util.ArrayList;
4041
import java.util.List;
@@ -277,9 +278,9 @@ private HttpServer startHttpServer(int port) throws IOException {
277278
String response = registry.scrape(acceptHeader);
278279

279280
httpExchange.getResponseHeaders().add("Content-Type", contentType);
280-
httpExchange.sendResponseHeaders(200, response.getBytes().length);
281+
httpExchange.sendResponseHeaders(200, response.getBytes(StandardCharsets.UTF_8).length);
281282
try (OutputStream outputStream = httpExchange.getResponseBody()) {
282-
outputStream.write(response.getBytes());
283+
outputStream.write(response.getBytes(StandardCharsets.UTF_8));
283284
}
284285
});
285286
new Thread(server::start).start();

implementations/micrometer-registry-statsd/src/main/java/io/micrometer/statsd/internal/BufferingFlux.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import reactor.core.publisher.DirectProcessor;
1919
import reactor.core.publisher.Flux;
2020

21+
import java.nio.charset.StandardCharsets;
2122
import java.time.Duration;
2223
import java.util.concurrent.atomic.AtomicInteger;
2324
import java.util.concurrent.atomic.AtomicLong;
@@ -45,7 +46,7 @@ private BufferingFlux() {
4546
public static Flux<String> create(final Flux<String> source, final String delimiter, final int maxByteArraySize,
4647
final long maxMillisecondsBetweenEmits) {
4748
return Flux.defer(() -> {
48-
final int delimiterSize = delimiter.getBytes().length;
49+
final int delimiterSize = delimiter.getBytes(StandardCharsets.UTF_8).length;
4950
final AtomicInteger byteSize = new AtomicInteger();
5051
final AtomicLong lastTime = new AtomicLong();
5152

@@ -62,7 +63,7 @@ public static Flux<String> create(final Flux<String> source, final String delimi
6263
.mergeWith(heartbeat);
6364

6465
return sourceWithEmptyStringKeepAlive.bufferUntil(line -> {
65-
final int bytesLength = line.getBytes().length;
66+
final int bytesLength = line.getBytes(StandardCharsets.UTF_8).length;
6667
final long now = System.currentTimeMillis();
6768
// Update last time to now if this is the first time
6869
lastTime.compareAndSet(0, now);

micrometer-core/src/main/java/io/micrometer/core/ipc/http/HttpSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public String toString() {
120120
printed.append("<no request body>");
121121
}
122122
else {
123-
printed.append(new String(entity));
123+
printed.append(new String(entity, StandardCharsets.UTF_8));
124124
}
125125
return printed.toString();
126126
}

micrometer-core/src/test/java/io/micrometer/core/instrument/binder/tomcat/TomcatMetricsTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import javax.servlet.http.HttpServletRequest;
4545
import javax.servlet.http.HttpServletResponse;
4646
import java.io.IOException;
47+
import java.nio.charset.StandardCharsets;
4748
import java.util.Arrays;
4849
import java.util.Collection;
4950
import java.util.Collections;
@@ -150,7 +151,7 @@ void whenTomcatMetricsBoundBeforeTomcatStarted_mbeanMetricsRegisteredEventually(
150151
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
151152
IOUtils.toString(req.getInputStream());
152153
sleep();
153-
resp.getOutputStream().write("yes".getBytes());
154+
resp.getOutputStream().write("yes".getBytes(StandardCharsets.UTF_8));
154155
}
155156
};
156157

@@ -183,7 +184,7 @@ void whenTomcatMetricsBoundAfterTomcatStarted_mbeanMetricsRegisteredImmediately(
183184
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
184185
IOUtils.toString(req.getInputStream());
185186
sleep();
186-
resp.getOutputStream().write("yes".getBytes());
187+
resp.getOutputStream().write("yes".getBytes(StandardCharsets.UTF_8));
187188
}
188189
};
189190

@@ -217,15 +218,15 @@ void whenMultipleServlets_thenRegisterMetricsForAllServlets() throws Exception {
217218
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
218219
IOUtils.toString(req.getInputStream());
219220
sleep();
220-
resp.getOutputStream().write("yes".getBytes());
221+
resp.getOutputStream().write("yes".getBytes(StandardCharsets.UTF_8));
221222
}
222223
}, new HttpServlet() {
223224
@Override
224225
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
225226
throws ServletException, IOException {
226227
IOUtils.toString(req.getInputStream());
227228
sleep();
228-
resp.getOutputStream().write("hi".getBytes());
229+
resp.getOutputStream().write("hi".getBytes(StandardCharsets.UTF_8));
229230
}
230231
});
231232

@@ -277,15 +278,15 @@ else if ("servlet1".equals(m.getId().getTag("name"))) {
277278
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
278279
IOUtils.toString(req.getInputStream());
279280
sleep();
280-
resp.getOutputStream().write("yes".getBytes());
281+
resp.getOutputStream().write("yes".getBytes(StandardCharsets.UTF_8));
281282
}
282283
}, new HttpServlet() {
283284
@Override
284285
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
285286
throws ServletException, IOException {
286287
IOUtils.toString(req.getInputStream());
287288
sleep();
288-
resp.getOutputStream().write("hi".getBytes());
289+
resp.getOutputStream().write("hi".getBytes(StandardCharsets.UTF_8));
289290
}
290291
});
291292

0 commit comments

Comments
 (0)