Skip to content

Commit c3f02dc

Browse files
committed
fix: Reintroduce JSpecify annotations.
JSpecify 1.0.0 has been released, so I'm hoping that googleapis#1362 is no longer a concern. (If there are remaining concerns, please do let me know.) This reverts commit 8138f46 (aka googleapis#1364) with a few changes: - This commit uses JSpecify 1.0.0 instead of 0.2.0. - As a result, it uses `org.jspecify.annotations` instead of `org.jspecify.nullness`. - It no longer touches `Structs`, since that was deleted in googleapis#1501. (JSpecify annotations were originally introduced by googleapis#1010.)
1 parent 0e0d107 commit c3f02dc

10 files changed

+30
-16
lines changed

google-cloud-logging/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@
9898
<groupId>com.google.cloud</groupId>
9999
<artifactId>google-cloud-core</artifactId>
100100
</dependency>
101+
<dependency>
102+
<groupId>org.jspecify</groupId>
103+
<artifactId>jspecify</artifactId>
104+
<version>1.0.0</version>
105+
</dependency>
101106
<dependency>
102107
<groupId>com.google.errorprone</groupId>
103108
<artifactId>error_prone_annotations</artifactId>

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.ArrayList;
3030
import java.util.Collections;
3131
import java.util.List;
32+
import org.jspecify.annotations.Nullable;
3233

3334
public final class Instrumentation {
3435
public static final String DIAGNOSTIC_INFO_KEY = "logging.googleapis.com/diagnostic";
@@ -102,7 +103,7 @@ public static Tuple<Boolean, Iterable<LogEntry>> populateInstrumentationInfo(
102103
* @return The new array of oprions containing WriteOption.OptionType.PARTIAL_SUCCESS flag set to
103104
* true
104105
*/
105-
public static WriteOption[] addPartialSuccessOption(WriteOption[] options) {
106+
public static WriteOption @Nullable [] addPartialSuccessOption(WriteOption[] options) {
106107
if (options == null) {
107108
return options;
108109
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.logging.v2.LogName;
2323
import java.util.Map;
24+
import org.jspecify.annotations.Nullable;
2425

2526
/**
2627
* Class for specifying resource name of the log to which this log entry belongs (see 'logName'
@@ -87,7 +88,7 @@ public static LogDestinationName billingAccount(String id) {
8788
}
8889

8990
/** Creates a {@code LogEntry} object for given log ID. */
90-
public LogName toLogName(String logId) {
91+
public @Nullable LogName toLogName(String logId) {
9192
if (logId == null) {
9293
return null;
9394
}
@@ -120,7 +121,7 @@ public DestinationType getDestinationType() {
120121
}
121122

122123
/** Creates a {@code LogDestinationName} object from given {@code LogName}. */
123-
public static LogDestinationName fromLogName(LogName logName) {
124+
public static @Nullable LogDestinationName fromLogName(LogName logName) {
124125
if (logName == null) {
125126
return null;
126127
}

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.HashMap;
3939
import java.util.Map;
4040
import java.util.Objects;
41+
import org.jspecify.annotations.Nullable;
4142

4243
/**
4344
* A Cloud Logging log entry. All log entries are represented via objects of this class. Log entries
@@ -368,7 +369,7 @@ public MonitoredResource getResource() {
368369
* @return timestamp in milliseconds
369370
*/
370371
@Deprecated
371-
public Long getTimestamp() {
372+
public @Nullable Long getTimestamp() {
372373
return timestamp != null ? timestamp.toEpochMilli() : null;
373374
}
374375

@@ -389,7 +390,7 @@ public Instant getInstantTimestamp() {
389390
* @return timestamp in milliseconds
390391
*/
391392
@Deprecated
392-
public Long getReceiveTimestamp() {
393+
public @Nullable Long getReceiveTimestamp() {
393394
return receiveTimestamp != null ? receiveTimestamp.toEpochMilli() : null;
394395
}
395396

@@ -437,13 +438,13 @@ public Operation getOperation() {
437438
* Returns the resource name of the trace associated with the log entry, if any. If it contains a
438439
* relative resource name, the name is assumed to be relative to `//tracing.googleapis.com`.
439440
*/
440-
public String getTrace() {
441+
public @Nullable String getTrace() {
441442
// For backwards compatibility return null when trace not set instead of "null".
442443
return trace == null ? null : trace;
443444
}
444445

445446
/** Returns the ID of the trace span associated with the log entry, if any. */
446-
public String getSpanId() {
447+
public @Nullable String getSpanId() {
447448
// For backwards compatibility return null when spanId not set instead of "null".
448449
return spanId == null ? null : spanId;
449450
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.logging.LogRecord;
3737
import java.util.logging.Logger;
3838
import java.util.logging.SimpleFormatter;
39+
import org.jspecify.annotations.Nullable;
3940

4041
/**
4142
* A logging handler that outputs logs generated with {@link java.util.logging.Logger} to Cloud
@@ -356,7 +357,7 @@ public void publish(LogRecord record) {
356357
}
357358
}
358359

359-
private MonitoredResource getMonitoredResource() {
360+
private @Nullable MonitoredResource getMonitoredResource() {
360361
Optional<WriteOption> resourceOption =
361362
stream(defaultWriteOptions)
362363
.filter(o -> o.getOptionType() == WriteOption.OptionType.RESOURCE)

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
import java.util.concurrent.ExecutionException;
101101
import java.util.concurrent.TimeUnit;
102102
import java.util.concurrent.TimeoutException;
103+
import org.jspecify.annotations.Nullable;
103104

104105
class LoggingImpl extends BaseService<LoggingOptions> implements Logging {
105106
protected static final String RESOURCE_NAME_FORMAT = "projects/%s/traces/%s";
@@ -792,7 +793,7 @@ private static WriteLogEntriesRequest writeLogEntriesRequest(
792793
return builder.build();
793794
}
794795

795-
private static LogName getLogName(
796+
private static @Nullable LogName getLogName(
796797
String projectId, String logName, LogDestinationName destination) {
797798
if (logName == null) {
798799
return null;
@@ -932,7 +933,7 @@ public void flush() {
932933
* @param resource A {@see MonitoredResource} describing environment metadata.
933934
* @return A formatted trace id string.
934935
*/
935-
private String getFormattedTrace(String traceId, MonitoredResource resource) {
936+
private @Nullable String getFormattedTrace(String traceId, MonitoredResource resource) {
936937
if (traceId == null) {
937938
return null;
938939
}

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.nio.file.Files;
2525
import java.nio.file.Paths;
2626
import java.util.function.Supplier;
27+
import org.jspecify.annotations.Nullable;
2728

2829
public final class MetadataLoader {
2930
public static final String ENV_FLEXIBLE = "flex";
@@ -67,7 +68,7 @@ public MetadataLoader(ResourceTypeEnvironmentGetter getter) {
6768
* @param label A resource metadata label of type {@see MonitoredResourceUtil.Label}
6869
* @return A string with metadata value or {@code null} if the label is not supported.
6970
*/
70-
public String getValue(MonitoredResourceUtil.Label label) {
71+
public @Nullable String getValue(MonitoredResourceUtil.Label label) {
7172
Supplier<String> lambda = labelResolvers.get(label);
7273
if (lambda != null) {
7374
return lambda.get();
@@ -198,7 +199,7 @@ private String getProjectId() {
198199
*
199200
* @return region string id
200201
*/
201-
private String getRegion() {
202+
private @Nullable String getRegion() {
202203
String loc = getter.getAttribute("instance/region");
203204
if (loc != null) {
204205
return loc.substring(loc.lastIndexOf('/') + 1);
@@ -223,7 +224,7 @@ private String getVersionId() {
223224
*
224225
* @return zone string id
225226
*/
226-
private String getZone() {
227+
private @Nullable String getZone() {
227228
String loc = getter.getAttribute("instance/zone");
228229
if (loc != null) {
229230
return loc.substring(loc.lastIndexOf('/') + 1);

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
package com.google.cloud.logging;
1818

1919
import com.google.cloud.MetadataConfig;
20+
import org.jspecify.annotations.Nullable;
2021

2122
final class ResourceTypeEnvironmentGetterImpl implements ResourceTypeEnvironmentGetter {
2223

2324
@Override
24-
public String getEnv(String name) {
25+
public @Nullable String getEnv(String name) {
2526
// handle exception thrown if a security manager exists and blocks access to the
2627
// process environment
2728
try {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Objects;
2626
import java.util.regex.Matcher;
2727
import java.util.regex.Pattern;
28+
import org.jspecify.annotations.Nullable;
2829

2930
/**
3031
* Cloud Logging sinks can be used to control the export of your logs. Each sink specifies the
@@ -477,7 +478,7 @@ LogSink.VersionFormat toPb() {
477478
return versionPb;
478479
}
479480

480-
static VersionFormat fromPb(LogSink.VersionFormat versionPb) {
481+
static @Nullable VersionFormat fromPb(LogSink.VersionFormat versionPb) {
481482
switch (versionPb) {
482483
case V1:
483484
return VersionFormat.V1;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.logging.v2.LogEntrySourceLocation;
2525
import java.io.Serializable;
2626
import java.util.Objects;
27+
import org.jspecify.annotations.Nullable;
2728

2829
/** Additional information about the source code location that produced the log entry. */
2930
public final class SourceLocation implements Serializable {
@@ -173,7 +174,7 @@ static SourceLocation fromPb(LogEntrySourceLocation sourceLocationPb) {
173174
* @return a new instance of {@link SourceLocation} populated with file name, method and line
174175
* number information.
175176
*/
176-
static SourceLocation fromCurrentContext(String... exclusionClassPaths) {
177+
static @Nullable SourceLocation fromCurrentContext(String... exclusionClassPaths) {
177178
StackTraceElement[] stackTrace = new Exception().getStackTrace();
178179

179180
for (int level = 1; level < stackTrace.length; level++) {

0 commit comments

Comments
 (0)