Skip to content

Commit 79e9d8d

Browse files
bpcreechBen Creechgcf-owl-bot[bot]
authored
fix: Apply Google Java Code Clarity suggestions (#1044)
* fix: Apply Google Java Code Clarity suggestions * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Ben Creech <bpcreech@google.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent ad38188 commit 79e9d8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+586
-639
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<differences>
3+
<difference>
4+
<differenceType>3003</differenceType>
5+
<className>com/google/cloud/logging/Instrumentation</className>
6+
</difference>
7+
<difference>
8+
<differenceType>7009</differenceType>
9+
<className>com/google/cloud/logging/Instrumentation</className>
10+
<method>Instrumentation()</method>
11+
</difference>
12+
</differences>

google-cloud-logging/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@
127127
<type>test-jar</type>
128128
<scope>test</scope>
129129
</dependency>
130+
<dependency>
131+
<groupId>com.google.guava</groupId>
132+
<artifactId>guava-testlib</artifactId>
133+
<scope>test</scope>
134+
</dependency>
130135

131136
<dependency>
132137
<groupId>com.google.api.grpc</groupId>

google-cloud-logging/src/main/java/com/google/cloud/logging/Context.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
package com.google.cloud.logging;
1818

1919
import com.google.cloud.logging.HttpRequest.RequestMethod;
20+
import com.google.common.base.Ascii;
2021
import com.google.common.base.MoreObjects;
22+
import com.google.common.base.Splitter;
23+
import com.google.common.collect.Iterables;
2124
import com.google.errorprone.annotations.CanIgnoreReturnValue;
25+
import java.util.List;
2226
import java.util.Objects;
2327
import java.util.regex.Matcher;
2428
import java.util.regex.Pattern;
@@ -125,7 +129,7 @@ public Builder setSpanId(String spanId) {
125129
@CanIgnoreReturnValue
126130
public Builder loadCloudTraceContext(String cloudTrace) {
127131
if (cloudTrace != null) {
128-
cloudTrace = cloudTrace.split(";")[0];
132+
cloudTrace = Iterables.get(Splitter.on(';').split(cloudTrace), 0);
129133
int split = cloudTrace.indexOf('/');
130134
if (split >= 0) {
131135
String traceId = cloudTrace.substring(0, split);
@@ -157,16 +161,16 @@ public Builder loadCloudTraceContext(String cloudTrace) {
157161
* the format version is not supported.
158162
*/
159163
@CanIgnoreReturnValue
160-
public Builder loadW3CTraceParentContext(String traceParent) throws IllegalArgumentException {
164+
public Builder loadW3CTraceParentContext(String traceParent) {
161165
if (traceParent != null) {
162-
Matcher validator = W3C_TRACE_CONTEXT_FORMAT.matcher(traceParent.toLowerCase());
166+
Matcher validator = W3C_TRACE_CONTEXT_FORMAT.matcher(Ascii.toLowerCase(traceParent));
163167
if (!validator.matches()) {
164168
throw new IllegalArgumentException(
165169
"Invalid format of the header value. The value does not match W3C Trace Context version \"00\"");
166170
}
167-
String[] fields = traceParent.split("-");
168-
setTraceId(fields[1]);
169-
setSpanId(fields[2]);
171+
List<String> fields = Splitter.on('-').splitToList(traceParent);
172+
setTraceId(fields.get(1));
173+
setSpanId(fields.get(2));
170174
// fields[3] contains flag(s)
171175
}
172176
return this;

google-cloud-logging/src/main/java/com/google/cloud/logging/Exclusion.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.logging.v2.LogExclusion;
2424
import com.google.protobuf.Timestamp;
2525
import java.util.Objects;
26-
import org.jspecify.nullness.Nullable;
2726

2827
/**
2928
* Specifies a set of log entries that are not to be stored in Logging. If your GCP resource
@@ -35,27 +34,21 @@
3534
public class Exclusion {
3635

3736
static final Function<LogExclusion, Exclusion> FROM_PROTOBUF_FUNCTION =
38-
new Function<LogExclusion, Exclusion>() {
39-
@Override
40-
public @Nullable Exclusion apply(LogExclusion exclusionPb) {
41-
return exclusionPb != null ? Exclusion.fromProtobuf(exclusionPb) : null;
42-
}
37+
(LogExclusion exclusionPb) -> {
38+
return exclusionPb != null ? Exclusion.fromProtobuf(exclusionPb) : null;
4339
};
4440

4541
static final Function<Exclusion, LogExclusion> TO_PROTOBUF_FUNCTION =
46-
new Function<Exclusion, LogExclusion>() {
47-
@Override
48-
public @Nullable LogExclusion apply(Exclusion exclusion) {
49-
return exclusion != null ? exclusion.toProtobuf() : null;
50-
}
42+
(Exclusion exclusion) -> {
43+
return exclusion != null ? exclusion.toProtobuf() : null;
5144
};
5245

53-
private String name;
54-
private String description;
55-
private String filter;
56-
private boolean disabled;
57-
private Timestamp createTime;
58-
private Timestamp updateTime;
46+
private final String name;
47+
private final String description;
48+
private final String filter;
49+
private final boolean disabled;
50+
private final Timestamp createTime;
51+
private final Timestamp updateTime;
5952

6053
/** A builder for {@code Exclusion} objects. */
6154
public static class Builder {
@@ -165,7 +158,7 @@ public boolean equals(Object o) {
165158
if (this == o) {
166159
return true;
167160
}
168-
if (o == null || getClass() != o.getClass()) {
161+
if (!(o instanceof Exclusion)) {
169162
return false;
170163
}
171164
Exclusion exclusion = (Exclusion) o;
@@ -253,7 +246,7 @@ LogExclusion toProtobuf() {
253246
static Exclusion fromProtobuf(LogExclusion exclusionPb) {
254247
Exclusion.Builder builder = newBuilder(exclusionPb.getName(), exclusionPb.getFilter());
255248
builder.setDisabled(exclusionPb.getDisabled());
256-
if (!exclusionPb.getDescription().equals("")) {
249+
if (!exclusionPb.getDescription().isEmpty()) {
257250
builder.setDescription(exclusionPb.getDescription());
258251
}
259252
if (exclusionPb.hasCreateTime()) {

google-cloud-logging/src/main/java/com/google/cloud/logging/HttpRequest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.google.cloud.StringEnumType;
2121
import com.google.cloud.StringEnumValue;
2222
import com.google.common.base.MoreObjects;
23-
import com.google.common.base.Strings;
2423
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2524
import java.io.Serializable;
2625
import java.util.Objects;
@@ -71,7 +70,7 @@ public RequestMethod apply(String constant) {
7170
};
7271

7372
private static final StringEnumType<RequestMethod> type =
74-
new StringEnumType<RequestMethod>(RequestMethod.class, CONSTRUCTOR);
73+
new StringEnumType<>(RequestMethod.class, CONSTRUCTOR);
7574

7675
public static final RequestMethod GET = type.createAndRegister("GET");
7776
public static final RequestMethod HEAD = type.createAndRegister("HEAD");
@@ -527,10 +526,10 @@ public static Builder newBuilder() {
527526

528527
static HttpRequest fromPb(com.google.logging.type.HttpRequest requestPb) {
529528
Builder builder = newBuilder();
530-
if (!Strings.isNullOrEmpty(requestPb.getRequestMethod())) {
529+
if (!requestPb.getRequestMethod().isEmpty()) {
531530
builder.setRequestMethod(RequestMethod.valueOf(requestPb.getRequestMethod()));
532531
}
533-
if (!Strings.isNullOrEmpty(requestPb.getRequestUrl())) {
532+
if (!requestPb.getRequestUrl().isEmpty()) {
534533
builder.setRequestUrl(requestPb.getRequestUrl());
535534
}
536535
if (requestPb.getRequestSize() != 0L) {
@@ -542,16 +541,16 @@ static HttpRequest fromPb(com.google.logging.type.HttpRequest requestPb) {
542541
if (requestPb.getResponseSize() != 0L) {
543542
builder.setResponseSize(requestPb.getResponseSize());
544543
}
545-
if (!Strings.isNullOrEmpty(requestPb.getUserAgent())) {
544+
if (!requestPb.getUserAgent().isEmpty()) {
546545
builder.setUserAgent(requestPb.getUserAgent());
547546
}
548-
if (!Strings.isNullOrEmpty(requestPb.getServerIp())) {
547+
if (!requestPb.getServerIp().isEmpty()) {
549548
builder.setServerIp(requestPb.getServerIp());
550549
}
551-
if (!Strings.isNullOrEmpty(requestPb.getRemoteIp())) {
550+
if (!requestPb.getRemoteIp().isEmpty()) {
552551
builder.setRemoteIp(requestPb.getRemoteIp());
553552
}
554-
if (!Strings.isNullOrEmpty(requestPb.getReferer())) {
553+
if (!requestPb.getReferer().isEmpty()) {
555554
builder.setReferer(requestPb.getReferer());
556555
}
557556
builder.setCacheLookup(requestPb.getCacheLookup());

google-cloud-logging/src/main/java/com/google/cloud/logging/Instrumentation.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@
2121
import com.google.cloud.Tuple;
2222
import com.google.cloud.logging.Logging.WriteOption;
2323
import com.google.cloud.logging.Payload.JsonPayload;
24-
import com.google.cloud.logging.Payload.Type;
2524
import com.google.common.collect.ImmutableMap;
2625
import com.google.common.collect.Iterables;
2726
import com.google.protobuf.ListValue;
2827
import com.google.protobuf.Struct;
2928
import com.google.protobuf.Value;
3029
import java.util.ArrayList;
31-
import java.util.Arrays;
30+
import java.util.Collections;
3231
import java.util.List;
3332
import org.jspecify.nullness.Nullable;
3433

35-
public class Instrumentation {
34+
public final class Instrumentation {
3635
public static final String DIAGNOSTIC_INFO_KEY = "logging.googleapis.com/diagnostic";
3736
public static final String INSTRUMENTATION_SOURCE_KEY = "instrumentation_source";
3837
public static final String INSTRUMENTATION_NAME_KEY = "name";
@@ -43,14 +42,14 @@ public class Instrumentation {
4342
public static final int MAX_DIAGNOSTIC_VALUE_LENGTH = 14;
4443
public static final int MAX_DIAGNOSTIC_ENTIES = 3;
4544
private static boolean instrumentationAdded = false;
46-
private static Object instrumentationLock = new Object();
45+
private static final Object instrumentationLock = new Object();
4746

4847
/**
4948
* Populates entries with instrumentation info which is added in separate log entry
5049
*
51-
* @param logEntries {Iterable<LogEntry>} The list of entries to be populated
52-
* @return {Tuple<Boolean, Iterable<LogEntry>>} containing a flag if instrumentation info was
53-
* added or not and a modified list of log entries
50+
* @param logEntries {@code Iterable<LogEntry>} The list of entries to be populated
51+
* @return {@code Tuple<Boolean, Iterable<LogEntry>>} containing a flag if instrumentation info
52+
* was added or not and a modified list of log entries
5453
*/
5554
public static Tuple<Boolean, Iterable<LogEntry>> populateInstrumentationInfo(
5655
Iterable<LogEntry> logEntries) {
@@ -61,7 +60,7 @@ public static Tuple<Boolean, Iterable<LogEntry>> populateInstrumentationInfo(
6160
for (LogEntry logEntry : logEntries) {
6261
// Check if LogEntry has a proper payload and also contains a diagnostic entry
6362
if (!isWritten
64-
&& logEntry.getPayload().getType() == Type.JSON
63+
&& logEntry.getPayload().getType() == Payload.Type.JSON
6564
&& logEntry
6665
.<Payload.JsonPayload>getPayload()
6766
.getData()
@@ -77,7 +76,7 @@ public static Tuple<Boolean, Iterable<LogEntry>> populateInstrumentationInfo(
7776
.getListValue();
7877
entries.add(createDiagnosticEntry(null, null, infoList));
7978
isWritten = true;
80-
} catch (Exception ex) {
79+
} catch (RuntimeException ex) {
8180
System.err.println("ERROR: unexpected exception in populateInstrumentationInfo: " + ex);
8281
}
8382
} else {
@@ -99,8 +98,8 @@ public static Tuple<Boolean, Iterable<LogEntry>> populateInstrumentationInfo(
9998
*/
10099
public static WriteOption @Nullable [] addPartialSuccessOption(WriteOption[] options) {
101100
if (options == null) return options;
102-
List<WriteOption> writeOptions = new ArrayList<WriteOption>();
103-
writeOptions.addAll(Arrays.asList(options));
101+
List<WriteOption> writeOptions = new ArrayList<>();
102+
Collections.addAll(writeOptions, options);
104103
// Make sure we remove all partial success flags if any exist
105104
writeOptions.removeIf(
106105
option -> option.getOptionType() == WriteOption.OptionType.PARTIAL_SUCCESS);
@@ -133,18 +132,16 @@ private static LogEntry createDiagnosticEntry(
133132
generateLibrariesList(libraryName, libraryVersion, existingLibraryList))
134133
.build()))
135134
.build();
136-
LogEntry entry =
137-
LogEntry.newBuilder(
138-
JsonPayload.of(
139-
Struct.newBuilder()
140-
.putAllFields(
141-
ImmutableMap.of(
142-
DIAGNOSTIC_INFO_KEY,
143-
Value.newBuilder().setStructValue(instrumentation).build()))
144-
.build()))
145-
.setLogName(INSTRUMENTATION_LOG_NAME)
146-
.build();
147-
return entry;
135+
return LogEntry.newBuilder(
136+
JsonPayload.of(
137+
Struct.newBuilder()
138+
.putAllFields(
139+
ImmutableMap.of(
140+
DIAGNOSTIC_INFO_KEY,
141+
Value.newBuilder().setStructValue(instrumentation).build()))
142+
.build()))
143+
.setLogName(INSTRUMENTATION_LOG_NAME)
144+
.build();
148145
}
149146

150147
private static ListValue generateLibrariesList(
@@ -171,7 +168,7 @@ private static ListValue generateLibrariesList(
171168
libraryList.addValues(
172169
Value.newBuilder().setStructValue(createInfoStruct(name, version)).build());
173170
if (libraryList.getValuesCount() == MAX_DIAGNOSTIC_ENTIES) break;
174-
} catch (Exception ex) {
171+
} catch (RuntimeException ex) {
175172
}
176173
}
177174
}
@@ -222,4 +219,6 @@ private static String truncateValue(String value) {
222219
if (Strings.isNullOrEmpty(value) || value.length() < MAX_DIAGNOSTIC_VALUE_LENGTH) return value;
223220
return value.substring(0, MAX_DIAGNOSTIC_VALUE_LENGTH) + "*";
224221
}
222+
223+
private Instrumentation() {}
225224
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy